Java Utililty Methods InputStream to String

List of utility methods to do InputStream to String

Description

The list of methods to do InputStream to String are organized into topic(s).

Method

String[]asArrayString(InputStream response)
as Array String
List<String> lines = new ArrayList<>();
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response))) {
    String line = null;
    while ((line = bufferedReader.readLine()) != null) {
        lines.add(line);
    return lines.toArray(new String[lines.size()]);
} catch (IOException e) {
...
StringgetInputStreamAsString(final InputStream is, final String encoding)
get Input Stream As String
final Scanner s = new Scanner(is, encoding).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
StringgetInputStreamAsString(InputStream inStream)
get Input Stream As String
StringBuffer text = new StringBuffer();
InputStreamReader in = new InputStreamReader(inStream, "Unicode");
char buffer[] = new char[4096];
int bytes_read;
while ((bytes_read = in.read(buffer)) != -1)
    text.append(new String(buffer, 0, bytes_read));
return text.toString();
StringgetInputStreamAsString(InputStream stream)
get Input Stream As String
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
    br = new BufferedReader(new InputStreamReader(stream));
    char[] cbuf = new char[1024];
    int len;
    while ((len = br.read(cbuf)) > 0) {
        sb.append(cbuf, 0, len);
...
StringgetInputStreamContent(InputStream i, String encoding)
get Input Stream Content
if (i != null) {
    BufferedReader pBr;
    try {
        pBr = new BufferedReader(new InputStreamReader(i, encoding));
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
        return null;
    StringBuffer pSb = new StringBuffer();
    int a = -1;
    try {
        while ((a = pBr.read()) != -1) {
            pSb.append((char) a);
    } catch (IOException e) {
        e.printStackTrace();
    return pSb.toString();
return null;
StringgetInputStreamContent(InputStream inputStream)
Get InputStream to String
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer result = new StringBuffer();
String line;
while ((line = bufferReader.readLine()) != null) {
    result.append(line).append(NL);
bufferReader.close();
return result.toString();
...
StringgetInputStreamContents(InputStream inputStream)
get Input Stream Contents
int read;
StringBuilder sb = new StringBuilder();
while ((read = inputStream.read()) != -1) {
    sb.append((char) read);
inputStream.close();
return sb.toString();
StringgetInputStreamContents(InputStream inputStream)
Utility method to load the contents of an InputStream as a String
int chunksize = 512;
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); 
StringBuffer output = new StringBuffer();
char[] buff = new char[chunksize];
int len = reader.read(buff);
while (len > 0) {
    output.append(buff, 0, len);
    len = reader.read(buff);
...
StringgetStreamAsString(final InputStream stream)
Gets a stream as String.
BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
String line = null;
StringBuilder result = new StringBuilder();
while ((line = br.readLine()) != null) {
    result.append(line);
    result.append("\n");
stream.close();
...
StringgetStreamAsString(InputStream input, String charset)
get Stream As String
StringBuilder sb = new StringBuilder();
BufferedReader bf = null;
try {
    bf = new BufferedReader(new InputStreamReader(input, charset));
    String str;
    while ((str = bf.readLine()) != null) {
        sb.append(str);
    return sb.toString();
} finally {
    if (bf != null) {
        bf.close();
        bf = null;