Java ByteArrayInputStream Class

Java ByteArrayInputStream Class

The ByteArrayInputStream is composed of two words: ByteArray and InputStream. As the name suggests, it can be used to read byte array as input stream.

Java ByteArrayInputStream class contains an internal buffer which is used to read byte array as stream. In this stream, the data is read from a byte array.

The buffer of ByteArrayInputStream automatically grows according to data.


Java ByteArrayInputStream class declaration

Let’s see the declaration for Java.io.ByteArrayInputStream class:

  1. public class ByteArrayInputStream extends InputStream

Java ByteArrayInputStream class constructors

Constructor Description
ByteArrayInputStream(byte[] ary) Creates a new byte array input stream which uses ary as its buffer array.
ByteArrayInputStream(byte[] ary, int offset, int len) Creates a new byte array input stream which uses ary as its buffer array that can read up to specified len bytes of data from an array.

Java ByteArrayInputStream class methods

Methods Description
int available() It is used to return the number of remaining bytes that can be read from the input stream.
int read() It is used to read the next byte of data from the input stream.
int read(byte[] ary, int off, int len) It is used to read up to len bytes of data from an array of bytes in the input stream.
boolean markSupported() It is used to test the input stream for mark and reset method.
long skip(long x) It is used to skip the x bytes of input from the input stream.
void mark(int readAheadLimit) It is used to set the current marked position in the stream.
void reset() It is used to reset the buffer of a byte array.
void close() It is used for closing a ByteArrayInputStream.

Example of Java ByteArrayInputStream

Let’s see a simple example of java ByteArrayInputStream class to read byte array as input stream.

  1. package com.javatpoint;
  2. import java.io.*;
  3. public class ReadExample {
  4.   public static void main(String[] args) throws IOException {
  5.     byte[] buf = { 35363738 };
  6.     // Create the new byte array input stream
  7.     ByteArrayInputStream byt = new ByteArrayInputStream(buf);
  8.     int k = 0;
  9.     while ((k = byt.read()) != –1) {
  10.       //Conversion of a byte into character
  11.       char ch = (char) k;
  12.       System.out.println(“ASCII value of Character is:” + k + “; Special character is: “ + ch);
  13.     }
  14.   }
  15. }

Output:

ASCII value of Character is:35; Special character is: #
ASCII value of Character is:36; Special character is: $
ASCII value of Character is:37; Special character is: %
ASCII value of Character is:38; Special character is: &

The ByteArrayInputStream is composed of two words: ByteArray and InputStream. As the name suggests, it can be used to read byte array as input stream.

Java ByteArrayInputStream class contains an internal buffer which is used to read byte array as stream. In this stream, the data is read from a byte array.

The buffer of ByteArrayInputStream automatically grows according to data.


Java ByteArrayInputStream class declaration

Let’s see the declaration for Java.io.ByteArrayInputStream class:

  1. public class ByteArrayInputStream extends InputStream

Java ByteArrayInputStream class constructors

Constructor Description
ByteArrayInputStream(byte[] ary) Creates a new byte array input stream which uses ary as its buffer array.
ByteArrayInputStream(byte[] ary, int offset, int len) Creates a new byte array input stream which uses ary as its buffer array that can read up to specified len bytes of data from an array.

Java ByteArrayInputStream class methods

Methods Description
int available() It is used to return the number of remaining bytes that can be read from the input stream.
int read() It is used to read the next byte of data from the input stream.
int read(byte[] ary, int off, int len) It is used to read up to len bytes of data from an array of bytes in the input stream.
boolean markSupported() It is used to test the input stream for mark and reset method.
long skip(long x) It is used to skip the x bytes of input from the input stream.
void mark(int readAheadLimit) It is used to set the current marked position in the stream.
void reset() It is used to reset the buffer of a byte array.
void close() It is used for closing a ByteArrayInputStream.

 

Example of Java ByteArrayInputStream

Let’s see a simple example of java ByteArrayInputStream class to read byte array as input stream.

  1. package com.javatpoint;
  2. import java.io.*;
  3. public class ReadExample {
  4.   public static void main(String[] args) throws IOException {
  5.     byte[] buf = { 35363738 };
  6.     // Create the new byte array input stream
  7.     ByteArrayInputStream byt = new ByteArrayInputStream(buf);
  8.     int k = 0;
  9.     while ((k = byt.read()) != –1) {
  10.       //Conversion of a byte into character
  11.       char ch = (char) k;
  12.       System.out.println(“ASCII value of Character is:” + k + “; Special character is: “ + ch);
  13.     }
  14.   }
  15. }

Output:

ASCII value of Character is:35; Special character is: #
ASCII value of Character is:36; Special character is: $
ASCII value of Character is:37; Special character is: %
ASCII value of Character is:38; Special character is: &
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Java ByteArrayInputStream Class The ByteArrayInputStream is composed of two words: ByteArray and InputStream. As the name suggests, it can be used to read byte array as input stream. Java ByteArrayInputStream class contains an internal buffer which is used to read byte array as stream. In this stream, the data is read from a byte array. The buffer […]

Tags:

....
...