Java InputStream to String inputStreamToStringBuilder(final InputStream inputStream, final String charset)

Here you can find the source of inputStreamToStringBuilder(final InputStream inputStream, final String charset)

Description

Copies the stream into the string builder.

License

Open Source License

Parameter

Parameter Description
inputStream a parameter

Exception

Parameter Description
IOException an exception

Declaration

private static StringBuilder inputStreamToStringBuilder(final InputStream inputStream, final String charset)
        throws IOException 

Method Source Code

//package com.java2s;
/**/*from w w w.  j a  va 2  s  .c om*/
 *
 *  BibSonomy-Web-Common - A blue social bookmark and publication sharing system.
 *
 *  Copyright (C) 2006 - 2011 Knowledge & Data Engineering Group,
 *                            University of Kassel, Germany
 *                            http://www.kde.cs.uni-kassel.de/
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

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

public class Main {
    private static final String NEWLINE = "\n";
    private static final int MAX_CONTENT_LENGTH = 1 * 1024 * 1024;

    /** Copies the stream into the string builder.
     * 
     * @param inputStream
     * @return
     * @throws IOException
     */
    private static StringBuilder inputStreamToStringBuilder(final InputStream inputStream, final String charset)
            throws IOException {
        final InputStreamReader in;
        /*
         * set charset
         */
        if (charset == null || charset.trim().equals(""))
            in = new InputStreamReader(inputStream);
        else
            in = new InputStreamReader(inputStream, charset);
        /*
         * use buffered reader (we always assume to have text)
         */
        final BufferedReader buf = new BufferedReader(in);
        final StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = buf.readLine()) != null && sb.length() + line.length() < MAX_CONTENT_LENGTH) {
            sb.append(line).append(NEWLINE);
        }
        buf.close();

        return sb;
    }
}

Related

  1. inputStreamToString(InputStream is)
  2. inputStreamToString(InputStream s)
  3. inputStreamToString(InputStream stream)
  4. InputStreamToStringArray(InputStream input_stream)
  5. inputStreamToStringBuffer(InputStream stream)
  6. inputStreamToStringBuilder(InputStream in, int minimumCapacity)
  7. inputStreamToText(InputStream inputStream)
  8. loadText(InputStream in)
  9. loadTextFile(InputStream stream, String encoding, int maxSize, boolean finish)