Here you can find the source of toString(InputStream in, String charSet)
Parameter | Description |
---|---|
in | a parameter |
charSet | a parameter |
public static String toString(InputStream in, String charSet)
//package com.java2s; //License from project: Open Source License import java.io.*; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class Main { /**//from w ww . j a va 2 s. c o m * Will close stream * @param in * @param charSet * @return */ public static String toString(InputStream in, String charSet) { return inputStreamToString_force(in, charSet); } /** * Reads in whole input stream and returns as a string * @param reader The input reader to read in, will be closed * by this method at finish * @return the result string * @throws IOException */ public static String toString(Reader reader) { try { StringBuilder sb = new StringBuilder(); char[] buffer = new char[4096]; for (int read; (read = reader.read(buffer)) > -1;) { sb.append(buffer, 0, read); } return sb.toString(); } catch (IOException e) { throw new RuntimeException(e); } finally { close(reader); } } /** * Will close stream * @param in * @param charSet * @return */ public static String inputStreamToString_force(InputStream in, String charSet) { try { return inputStreamToString(in, charSet); } catch (IOException e) { return null; } } /** * Close streams (in or out) * @param stream */ public static void close(Closeable stream) { if (stream != null) { try { if (stream instanceof Flushable) { ((Flushable) stream).flush(); } stream.close(); } catch (IOException e) { // When the stream is closed or interupted, can ignore this exception } } } public static void close(Connection conn) { if (conn != null) { try { conn.close(); } catch (SQLException e) { // When the conn is closed or interupted, can ignore this exception } } } public static void close(ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { // When the file is closed already, can ignore this exception } } } public static void close(PreparedStatement ps) { if (ps != null) { try { ps.close(); } catch (SQLException e) { // When the file is closed already, can ignore this exception } } } /** * Reads in whole input stream and returns as a string<br> * Will close stream * @param in The input stream to read in, will be closed * by this method at finish * @param charSet charset to convert the input bytes into string * @return the result string * @throws IOException */ public static String inputStreamToString(InputStream in, String charSet) throws IOException { InputStreamReader inputStreamReader = null; try { inputStreamReader = charSet == null ? new InputStreamReader(in) : new InputStreamReader(in, charSet); return toString(inputStreamReader); } catch (UnsupportedEncodingException e1) { throw new RuntimeException(e1); } finally { close(in); } } }