Java File Read via ByteBuffer readInputStreamToString(InputStream inputStream)

Here you can find the source of readInputStreamToString(InputStream inputStream)

Description

read Input Stream To String

License

Apache License

Declaration

public static String readInputStreamToString(InputStream inputStream) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.io.InputStream;

import java.nio.ByteBuffer;

public class Main {
    public static String readInputStreamToString(InputStream inputStream) {
        return readInputStreamToString(inputStream, "UTF-8");
    }/* w ww. j  a v  a2s.  co m*/

    public static String readInputStreamToString(InputStream inputStream, String encoding) {
        String rs = null;
        try {
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 1024 * 1024);
            int length = -1;
            int bufferSize = 128;
            byte[] buffer = new byte[bufferSize];
            while ((length = inputStream.read(buffer)) != -1) {
                if (length != bufferSize) {
                    System.arraycopy(buffer, 0, buffer, 0, length);
                }
                byteBuffer.put(buffer);
            }
            rs = new String(byteBuffer.array(), encoding);
        } catch (IOException e) {

        } finally {
            // try {
            // inputStream.close();
            // } catch (IOException e1) {
            // e1.printStackTrace();
            // }
            try {
                inputStream.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        return rs;
    }
}

Related

  1. readFully(final Reader input, final char[] buffer, final int offset, final int length)
  2. readFully(InputStream in)
  3. readFullyAndClose(InputStream input)
  4. readHeaderArea(InputStream in)
  5. readInputStream(InputStream input)
  6. readInt(byte[] bytes, int index)
  7. readInt(byte[] bytes, int offset, java.nio.ByteOrder byteorder)
  8. readInt(byte[] in)
  9. readInt(ByteArrayInputStream bin)