ByteArrayInputStream read Example
ByteArrayInputStream class read example. This example shows you how to use read method.
ByteArrayInputStream class read example. public int read(byte[],int,int) Reads up to len bytes of data into an array of bytes from this input stream. If pos equals count, then -1 is returned to indicate end of file.
Here is the code
/**
* @ # Read.java
* A class repersenting use to read method
* of ByteArrayInputStream class in java.io package
* version 03 June 2008
* author Rose India
*/
import java.io.*;
public class Read {
public static void main(String args[]) throws Exception {
byte b[] = {1, 2, 3, 4, 5};
ByteArrayInputStream baStream = new ByteArrayInputStream(b);
if (baStream.markSupported()) {
baStream.mark(b.length);
}
baStream.reset();
System.out.println("Byte of data from input stream.");
//Returns the number of remaining bytes
while (baStream.available() != 0) {
//Read ByteArrayInputStream
System.out.println(baStream.read(b,1,3));
}
//Closing ByteArrayInputStream
baStream.close();
}
} |
Output
Byte of data from input stream.
3
2 |
|