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

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

Description

get Stream Content As String

License

Open Source License

Declaration

static public String getStreamContentAsString(InputStream is, String encoding) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedInputStream;

import java.io.ByteArrayOutputStream;

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

import java.io.Reader;

public class Main {
    static public String getStreamContentAsString(InputStream is, String encoding) throws IOException {
        return new String(getStreamContentAsBytes(is), encoding);
    }//from   w w w .j av a 2  s.  c  o m

    static public String getStreamContentAsString(Reader in) throws IOException {
        char[] data = new char[4912];
        int available = -1;
        StringBuilder b = new StringBuilder();
        while ((available = in.read(data)) > -1) {
            b.append(data, 0, available);
        }
        return b.toString();
    }

    static public byte[] getStreamContentAsBytes(InputStream is) throws IOException {
        BufferedInputStream buffer = new BufferedInputStream(is);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] data = new byte[4912];
        int available = -1;
        while ((available = buffer.read(data)) > -1) {
            output.write(data, 0, available);
        }
        is.close();
        return output.toByteArray();
    }

    static public byte[] getStreamContentAsBytes(InputStream is, int maxRead) throws IOException {
        BufferedInputStream buffer = new BufferedInputStream(is);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] data = new byte[4912];
        int available = -1, read = 0;
        while ((available = buffer.read(data)) > -1 && read < maxRead) {
            if (maxRead - read < available)
                available = maxRead - read;
            output.write(data, 0, available);
            read += available;
        }
        return output.toByteArray();
    }
}

Related

  1. getStreamAsString(InputStream stream)
  2. getStreamAsString(InputStream stream, String charset)
  3. getStreamContent(InputStream is)
  4. getStreamContent(InputStream stream, int bufferSize)
  5. getStreamContentAsString(InputStream is)
  6. getStreamContents(final InputStream is)
  7. inputStream2String(InputStream in)
  8. inputStream2String(InputStream in)
  9. inputStream2String(InputStream in)