Java InputStream to String inputStream2String(InputStream in)

Here you can find the source of inputStream2String(InputStream in)

Description

This method converts an input stream into a string.

License

Open Source License

Parameter

Parameter Description
in to convert

Return

string

Declaration

public static String inputStream2String(InputStream in) 

Method Source Code


//package com.java2s;
/*//w  w w  . ja  v  a2s. c o  m
 * Copyright (C) 2007 ETH Zurich
 *
 * This file is part of Accada (www.accada.org).
 *
 * Accada is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software Foundation.
 *
 * Accada 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 Accada; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

import java.io.IOException;
import java.io.InputStream;

public class Main {
    /**
     * This method converts an input stream into a string.
     * 
     * @param in to convert
     * @return string
     */
    public static String inputStream2String(InputStream in) {

        try {

            StringBuffer buf = new StringBuffer();
            while (in.available() > 0) {
                int i = in.read();
                buf.append((char) i);
            }
            return buf.toString();

        } catch (IOException e) {
            return null;
        }

    }
}

Related

  1. getStreamContentAsString(InputStream is)
  2. getStreamContentAsString(InputStream is, String encoding)
  3. getStreamContents(final InputStream is)
  4. inputStream2String(InputStream in)
  5. inputStream2String(InputStream in)
  6. InputStream2String(InputStream in, String encoding)
  7. InputStream2String(InputStream in_st, String charset)
  8. inputStream2String(InputStream inputstream)
  9. inputStream2String(InputStream inStream)