Java Text File Read by Charset readString(final InputStream input, final Charset charset)

Here you can find the source of readString(final InputStream input, final Charset charset)

Description

read String

License

Open Source License

Declaration

public static String readString(final InputStream input, final Charset charset) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;

public class Main {
    public static String readString(final InputStream input, final Charset charset) throws IOException {
        final byte[] bytes = readBytes(input, 4096);
        return new String(bytes, charset);
    }/*from   w ww.jav a 2  s .  co  m*/

    public static byte[] readBytes(final InputStream input, final int bufferSize) throws IOException {
        final ByteArrayOutputStream output = new ByteArrayOutputStream(bufferSize);
        bufferCopy(input, output, bufferSize);
        return output.toByteArray();

    }

    public static void bufferCopy(final InputStream input, final OutputStream output, final int bufferSize)
            throws IOException {
        final byte[] buffer = new byte[bufferSize];
        int count;
        while ((count = input.read(buffer)) != -1) {
            output.write(buffer, 0, count);
        }
    }
}

Related

  1. readStreamAsString(final InputStream iStream, Charset iCharset)
  2. readStreamToString(InputStream stream, Charset encoding)
  3. readString(DataInput input, int length, Charset charset)
  4. readString(final InputStream in, final Charset charset)
  5. readString(final InputStream in, final Charset charset)
  6. readString(InputStream in, int numBytes, Charset encoding)
  7. readString(InputStream in, String charset)
  8. readStringFromStream(InputStream stream, Charset charset)
  9. readTextFile(final String fileNamePath, final String charsetName)