Here you can find the source of readString(final InputStream in, final Charset charset)
Parameter | Description |
---|---|
in | the input stream to read |
charset | the character set of the string |
Parameter | Description |
---|---|
IOException | if an I/O error occurs |
public static String readString(final InputStream in, final Charset charset) throws IOException
//package com.java2s; import java.io.*; import java.nio.charset.Charset; public class Main { /**//from w w w .jav a 2 s . c o m * Reads a string from an input stream. This method uses the JVM's default character set. This method will not * close the InputStream. It is the responsibility of the calling code to cleanup after itself. * * @param in the input stream to read * @return the string * @throws IOException if an I/O error occurs */ public static String readString(final InputStream in) throws IOException { return readString(in, Charset.defaultCharset()); } /** * Reads a string from an input stream. This method will not close the InputStream. It is the responsibility of * the calling code to cleanup after itself. * * @param in the input stream to read * @param encoding the encoding of the string * @return the string * @throws IOException if an I/O error occurs */ public static String readString(final InputStream in, final String encoding) throws IOException { return readString(in, Charset.forName(encoding)); } /** * Reads a string from an input stream. This method will not close the InputStream. It is this responsibility of * the calling code to cleanup after itself. * * @param in the input stream to read * @param charset the character set of the string * @return the string * @throws IOException if an I/O error occurs */ public static String readString(final InputStream in, final Charset charset) throws IOException { return readString(new InputStreamReader(new BufferedInputStream(in), charset)); } /** * Reads a string from a reader. This method will not close the Reader. It is the responsibility of the calling * code to cleanup after itself. * * @param reader the reader to read * @return the string * @throws IOException if an I/O error occurs */ public static String readString(final Reader reader) throws IOException { final StringBuilder sb = new StringBuilder(); final char[] b = new char[2048]; // Could be anything from 2KB to 8KB in size depending on characters int n; while ((n = reader.read(b)) >= 0) { sb.append(b, 0, n); } return sb.toString(); } }