Java ByteArrayInputStream Class


In Java language, ByteArrayInputStream class contains buffer in the memory which is used to read byte array as the input stream.

Syntax:

public class ByteArrayInputStream extends InputStream  

ByteArrayInputStream class constructors

S.No Constructor Description
1 ByteArrayInputStream(byte [] buf) ByteArrayInputStream which uses buf as its buffer array.
2 ByteArrayInputStream(byte[] buf, int offset, int len) ByteArrayInputStream which uses buf as its buffer array, where off and len are the first byte and specified number of bytes to be read.

ByteArrayInputStream class Methods

S.No Method Description
1 public void mark(int read) This method is used to sets the current marked position in the stream.
2 public int available() This method is used to return the number of bytes that can be read from this file input stream.
3 public int read() This method is used to reads the next byte of data from the InputStream.
4 public int read(byte[] ar, int off, int len) This method is used to read up to len bytes of data from an array of bytes in the input stream.
5 public long skip(long n) This method is used to skips ‘n’ number of bytes from the stream
6 void close() This method is used for closing a ByteArrayInputStream.
7 void reset() This method is used to reset the buffer of a byte array.

Example:

import java.io.*;
public class ByteArrayInputStreamDemo{
    public static void main(String args[]){  
        byte[] b = { 33,34,35};  
        int c;
        ByteArrayInputStream bInput = new ByteArrayInputStream(b);
        while(( c = bInput.read())!= -1) {
            System.out.println("Special character is:" +(char)c);
         }
		bInput.reset(); 
    }
} 

Output

 Special character is:!
Special character is:"
Special character is:#



Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.