Java InputStreamReader Read loadAsText(InputStream in, String encoding, int bufferSize)

Here you can find the source of loadAsText(InputStream in, String encoding, int bufferSize)

Description

load As Text

License

Open Source License

Declaration

public static String loadAsText(InputStream in, String encoding, int bufferSize) throws IOException 

Method Source Code

//package com.java2s;

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

public class Main {
    public static String loadAsText(InputStream in, String encoding) throws IOException {
        return loadAsText(in, encoding, 4096);
    }/*from www.j a v  a2 s .  c  om*/

    public static String loadAsText(InputStream in, String encoding, int bufferSize) throws IOException {
        InputStreamReader reader = new InputStreamReader(in, encoding);
        char[] buffer = new char[bufferSize];
        int offset = 0;
        for (;;) {
            int remain = buffer.length - offset;
            if (remain <= 0) {
                char[] newBuffer = new char[buffer.length * 2];
                System.arraycopy(buffer, 0, newBuffer, 0, offset);
                buffer = newBuffer;
                remain = buffer.length - offset;
            }
            int numRead = reader.read(buffer, offset, remain);
            if (numRead == -1) {
                break;
            }
            offset += numRead;
        }
        return new String(buffer, 0, offset);
    }
}

Related

  1. load(File file, String encoding)
  2. load(final File file, final Class clazz)
  3. load(String fileName)
  4. readFile(Class cl, String filename)
  5. readFile(Class cls, String filename)
  6. readFile(ClassLoader classloader, String filename)
  7. readFile(File f)