Java InputStream to String getStreamAsString(InputStream is, String encoding)

Here you can find the source of getStreamAsString(InputStream is, String encoding)

Description

Copies the contents of the given stream to a string.

License

BSD License

Parameter

Parameter Description
is the stream to copy to a string
encoding the character encoding of the input stream

Exception

Parameter Description
FileNotFoundException an exception
IOException an exception

Return

contents of the stream as a string

Declaration

public static String getStreamAsString(InputStream is, String encoding)
        throws FileNotFoundException, IOException 

Method Source Code


//package com.java2s;
/*//ww w .  j  a  v  a2 s.c om
 [The "BSD licence"]
 Copyright (c) 2015, John Snyders
 All rights reserved.
    
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
 3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
    
 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import java.io.*;

public class Main {
    protected static final int BLKSIZE = 8192;

    /**
     * Copies the contents of the given stream to a string.
     * @param is the stream to copy to a string
     * @param encoding the character encoding of the input stream
     * @return contents of the stream as a string
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static String getStreamAsString(InputStream is, String encoding)
            throws FileNotFoundException, IOException {
        Reader r = null;
        if (encoding == null) {
            r = new InputStreamReader(is);
        } else {
            r = new InputStreamReader(is, encoding);
        }
        try {
            StringWriter w = new StringWriter();
            char[] buffer = new char[BLKSIZE];

            int len;
            while ((len = r.read(buffer)) != -1) {
                w.write(buffer, 0, len);
            }

            return w.toString();
        } finally {
            r.close();
        }
    }
}

Related

  1. getInputStreamContent(InputStream inputStream)
  2. getInputStreamContents(InputStream inputStream)
  3. getInputStreamContents(InputStream inputStream)
  4. getStreamAsString(final InputStream stream)
  5. getStreamAsString(InputStream input, String charset)
  6. getStreamAsString(InputStream stream)
  7. getStreamAsString(InputStream stream, String charset)
  8. getStreamContent(InputStream is)
  9. getStreamContent(InputStream stream, int bufferSize)