Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Java Miscellaneous

Advanced Java

Java APIs & Frameworks

Java Useful Resources

Java - ByteArrayInputStream



The ByteArrayInputStream class allows a buffer in the memory to be used as an InputStream. The input source is a byte array.

ByteArrayInputStream class provides the following constructors.

Sr.No. Constructor and Description
1

ByteArrayInputStream(byte [] a)

This constructor accepts a byte array as a parameter.

2

ByteArrayInputStream(byte [] a, int off, int len)

This constructor takes an array of bytes, and two integer values, where off is the first byte to be read and len is the number of bytes to be read.

Once you have ByteArrayInputStream object in hand then there is a list of helper methods which can be used to read the stream or to do other operations on the stream.

Sr.No. Method & Description
1

public int read()

This method reads the next byte of data from the InputStream. Returns an int as the next byte of data. If it is the end of the file, then it returns -1.

2

public int read(byte[] r, int off, int len)

This method reads upto len number of bytes starting from off from the input stream into an array. Returns the total number of bytes read. If it is the end of the file, -1 will be returned.

3

public int available()

Gives the number of bytes that can be read from this file input stream. Returns an int that gives the number of bytes to be read.

4

public void mark(int read)

This sets the current marked position in the stream. The parameter gives the maximum limit of bytes that can be read before the marked position becomes invalid.

5

public long skip(long n)

Skips ā€˜nā€™ number of bytes from the stream. This returns the actual number of bytes skipped.

Example

Following is the example to demonstrate ByteArrayInputStream and ByteArrayOutputStream.

import java.io.*;
public class ByteStreamTest {

   public static void main(String args[])throws IOException {
      ByteArrayOutputStream bOutput = new ByteArrayOutputStream(12);

      while( bOutput.size()!= 10 ) {
         // Gets the inputs from the user
         bOutput.write("hello".getBytes()); 
      }
      byte b [] = bOutput.toByteArray();
      System.out.println("Print the content");
      
      for(int x = 0 ; x < b.length; x++) {
         // printing the characters
         System.out.print((char)b[x]  + "   "); 
      }
      System.out.println("   ");
      
      int c;
      ByteArrayInputStream bInput = new ByteArrayInputStream(b);
      System.out.println("Converting characters to Upper case " );
      
      for(int y = 0 ; y < 1; y++) {
         while(( c = bInput.read())!= -1) {
            System.out.println(Character.toUpperCase((char)c));
         }
         bInput.reset(); 
      }
   }
}

Following is the sample run of the above program −

Output

Print the content
h   e   l   l   o   h   e   l   l   o      
Converting characters to Upper case 
H
E
L
L
O
H
E
L
L
O
java_files_io.htm
Advertisements