Java.io - SPLessons

Java.io ByteArrayInputStream

Home > Lesson > Chapter 6
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io ByteArrayInputStream

Java.io ByteArrayInputStream

shape Introduction

Generally the input stream acts as a source to the data like file or connection. The Java.io ByteArrayInputStream is the class used to reading the bytes into internal buffer from the array as bytes. Through the Java.io ByteArrayInputStream the array bytes are enclose to change into a stream.

shape Description

In case of Java.io ByteArrayInputStream the methods that are called after closing the stream only when no I/O exceptions occurred. The byte array input stream is subclass to the class called input stream.

Class Declaration

shape Declaration

Java.io ByteArrayInputStream class is declared below: Public class ByteArrayInputStream extends InputStream

shape Fields

Java.io/ByteArrayInputStream class fields are:
  • Protected int mark : The field represents the presently position that have been marked in the stream.
  • Protected int Count : The field represents to the index to count input stream buffer after the last character.
  • Protected byte [] bu : The field represents to the internal buffer where actually the data  stored.
  • Protected int pos : The field represents to the position of input stream to read that coming character.

Class Constructors

shape Table

Constructor Description
ByteArrayInputStream (byte[] buf) The function of this constructor is to create the byte array input stream and reads into the internal buffer called array.
ByteArrayInputStream (byte[] buf, int offset, int len) The function of this constructor is to create the byte array input stream with represented size of buffer.

Class Methods

shape Table

Method Description
void close () The function of this method is to close the present stream and associated to input stream systems resources are released without any impact.
Long skip (long m) The function of this method is to skip the text or characters in this stream of ' m ' bytes.
Boolean markSupported () The function of this method is to check the represented mark method is supported by this stream or not
int read(byte[] b, int off, int n) By using this method it reads the data up to 'n' bytes from the input stream into an array of bytes.

Inherited Methods

shape Description

From the following classes , methods are inherited to the byte arrray input stream class.
  • Java.io.object
  • Java.io.InputStream

shape Examples

Usage of void close() method. [c]import java.io.ByteArrayInputStream; import java.io.IOException; public class ByteArrayInputStream { public static void main(String[] args) throws IOException { byte[] buf = {66, 69, 68, 67}; ByteArrayInputStream bais = null; try{ // creating new byte array input stream bais = new ByteArrayInputStream(buf); // close appeal before read() and available() bais.close(); int count =0; // read till to the end of the stream while((count = bais.available())>0) { // To convert byte into character char c = (char)bais.read(); // print number of bytes that are available System.out.print("available bytes : "+ count); // print characters which are reads form the byte array System.out.println(" read byte : "+c); } }catch(Exception e) { // if I/O error occurs e.printStackTrace(); }finally { if(bais!=null) bais.close(); } } }[/c] Output Following is the result will be displayed. [c] available bytes : 4 read byte : B available bytes : 3 read byte : E available bytes : 2 read byte : D available bytes : 1 read byte : C [/c] Usage of Boolean markSupported() method. [c]import java.io.ByteArrayInputStream; import java.io.IOException; public class ByteArrayInputStream { public static void main(String[] args) throws IOException { byte[] buf = {65, 66, 67, 68, 69}; ByteArrayInputStream bais = null; try{ // creating a new byte array input stream bais = new ByteArrayInputStream(buf); // It checks whether mark() and reset() methods supported boolean isMarkSupported = bais.markSupported(); System.out.println("mark supported : "+isMarkSupported); System.out.println("proof:"); // print bytes System.out.println("Byte read "+ bais.read()); System.out.println("Byte read "+ bais.read()); System.out.println("Byte read "+ bais.read()); System.out.println("Mark() invocation method"); // To mark() invocation bais.mark(0); System.out.println("Byte read "+ bais.read()); System.out.println("Byte read "+ bais.read()); System.out.println("Reset() invocation method"); // To reset() invocation bais.reset(); System.out.println("Byte read "+ bais.read()); System.out.println("Byte read "+ bais.read()); }catch(Exception e) { // if I/O error occurs e.printStackTrace(); }finally { if(bais!=null) bais.close(); } } }[/c] Output Following is the result will be displayed. [c]mark supported : true proof: Byte read 65 Byte read 66 Byte read 67 Mark() invocation method Byte read 68 Byte read 69 Reset() invocation method Byte read 68 Byte read 69 [/c]

Summary

shape Key Points

  • Generally the input stream acts as a source to the data like file or connection.
  • The byte array input stream is the class used to reading the bytes into internal buffer from the array as bytes.
  • Through the byte array input stream the array bytes are enclose to change into a stream.