Convert ByteArrayInputStream to String in Java

Description

The following code shows how to convert ByteArrayInputStream to String.

Example


//  w w  w. j a  v  a2s.co m
import java.io.ByteArrayInputStream;

public class Main {
  public static String toString(ByteArrayInputStream is) {
    int size = is.available();
    char[] theChars = new char[size];
    byte[] bytes = new byte[size];

    is.read(bytes, 0, size);
    for (int i = 0; i < size;)
      theChars[i] = (char) (bytes[i++] & 0xff);

    return new String(theChars);
  }
}




















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip