Java InputStream to String inputStreamToString(InputStream s)

Here you can find the source of inputStreamToString(InputStream s)

Description

input Stream To String

License

Apache License

Declaration

public static String inputStreamToString(InputStream s) 

Method Source Code

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

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.UnsupportedEncodingException;

public class Main {
    public static String inputStreamToString(InputStream s) {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        try {//from  w  w w. ja  v a  2 s.  co  m
            while ((length = s.read(buffer)) != -1) {
                result.write(buffer, 0, length);
            }
        } catch (IOException e1) {
            e1.printStackTrace();
            return null;
        }
        // StandardCharsets.UTF_8.name() > JDK 7
        try {
            return result.toString("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related

  1. inputStreamToString(InputStream is)
  2. inputStreamToString(InputStream is)
  3. inputStreamToString(InputStream is)
  4. inputStreamToString(InputStream is)
  5. inputStreamToString(InputStream is)
  6. inputStreamToString(InputStream stream)
  7. InputStreamToStringArray(InputStream input_stream)
  8. inputStreamToStringBuffer(InputStream stream)
  9. inputStreamToStringBuilder(final InputStream inputStream, final String charset)