Java Reader to String toString(InputStream in, String charSet)

Here you can find the source of toString(InputStream in, String charSet)

Description

Will close stream

License

Apache License

Parameter

Parameter Description
in a parameter
charSet a parameter

Declaration

public static String toString(InputStream in, String charSet) 

Method Source Code

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

import java.io.Closeable;

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

import java.io.OutputStream;

import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.zip.ZipFile;

public class Main {
    /**//  w  w  w .  j  a  v a 2  s  .c  o  m
     * 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 in
     * @param in
     * @return
     */
    public static String toString(InputStream in) {
        return inputStreamToString_force(in, null);
    }

    /**
     * Will close stream
     * @param in
     * @param charSet
     * @return
     */
    public static String toString(InputStream in, String charSet) {
        return inputStreamToString_force(in, charSet);
    }

    /**
     * Close zip file
     * @param zipFile
     */
    public static void close(ZipFile zipFile) {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
                // When the file is closed already, 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
            }
        }
    }

    public static void close(Socket sk) {
        if (sk != null) {
            try {
                sk.close();
            } catch (IOException e) {
                // When the file is closed already, can ignore this exception
                e.printStackTrace();
            }
        }
    }

    public static void close(ServerSocket ssk) {
        if (ssk != null) {
            try {
                ssk.close();
            } catch (IOException e) {
                // When the file is closed already, can ignore this exception
                //               e.printStackTrace();
            }
        }
    }

    /**
     * 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 String inputStreamToString_force(InputStream in) {
        return inputStreamToString_force(in, null);
    }

    /**
     * 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;
        }
    }

    public static void flush(OutputStream out) {
        if (out != null) {
            try {
                out.flush();
            } catch (IOException e) {
                //
            }
        }
    }

    /**
     * Reads in whole input stream and returns as a string
     * @param in The input stream to read in, will be closed 
     *             by this method at finish
     * @return the result string
     * @throws IOException
     */
    public static String inputStreamToString(InputStream in) throws IOException {
        return inputStreamToString(in, null);
    }

    /**
     * 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);
        }
    }
}

Related

  1. toString(InputStream in, String charSet)