Java - ByteArrayInputStream read()



Description

The Java ByteArrayInputStream read(byte[] b, int off, int len) method reads len bytes of data from this input stream into an array of bytes. The read() method doesn't block.

Declaration

Following is the declaration for java.io.ByteArrayInputStream.read(byte[] b, int off, int len) method −

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

Parameters

  • b − data is read into this buffer

  • off − Offset to start at destination array b

  • len − maximum number of bytes read

Return Value

Number of bytes read into the buffer. Returns -1 if the stream has reached it's end.

Exception

  • NullPointerException − If b is null.

  • IndexOutOfBoundsException − If len is greater than input stream's length after offset, off is negative, or len is negative.

Example 1

The following example shows the usage of Java ByteArrayInputStream read() method. We've created a variable buf as byte[] and initialized with few bytes. We've created a ByteArrayInputStream reference and then initialized it with buf variable. We've created a buffer of 4 bytes and read the byte array stream into the buffer using read() method. Then buffer is iterated to print the bytes read.

import java.io.ByteArrayInputStream;
import java.io.IOException;
public class ByteStreamTest {
   public static void main(String[] args) throws IOException {
      byte[] buf = {65, 66, 67, 68, 69};
      ByteArrayInputStream bais = null;
      
      try {
         // create new byte array input stream
         bais = new ByteArrayInputStream(buf);
      
         // create buffer
         byte[] b = new byte[4];
         int num = bais.read(b, 2, 2);
         
         // number of bytes read
         System.out.println("Bytes read: "+num);
         
         // for each byte in a buffer
         for (byte s :b) {
         
            // covert byte to char
            char c = (char)s;
            
            // prints byte
            System.out.print(s);
            
            if(s == 0)
               
               // if byte is 0
               System.out.println(": Null");
            else
               
               // if byte is not 0
               System.out.println(": "+c);
         }
         
      } catch(Exception e) {
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         if(bais!=null)
            bais.close();
      }   
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Bytes read: 2
0: Null
0: Null
65: A
66: B

Example 2

The following example shows the usage of Java ByteArrayInputStream available() method. We've created a variable buf as byte[] and initialized with empty array. We've created a ByteArrayInputStream reference and then initialized it with buf variable. We've created a buffer of 4 bytes and read the byte array stream into the buffer using read() method. In if loop, we're checking if stream contains any byte using available() method and then its result is printed.

import java.io.ByteArrayInputStream;
import java.io.IOException;
public class ByteStreamTest {
   public static void main(String[] args) throws IOException {
      byte[] buf = {};
      ByteArrayInputStream bais = null;
      
      try {
         // create new byte array input stream
         bais = new ByteArrayInputStream(buf);
         
         // create buffer
         byte[] b = new byte[4];
         int num = bais.read(b, 2, 2);
         
         // number of bytes read
         System.out.println("Bytes read: "+num);
         int value = 0;
         // read the stream
         if((value = bais.read())!=-1) {
            // convert byte to character
            char c = (char)value;
            
            // print
            System.out.println("byte :"+value+"; char : "+ c);
         }else{
            System.out.print("byte stream is empty");
         } 
      } catch(Exception e) {
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         if(bais!=null)
            bais.close();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Bytes read: -1
byte stream is empty
java_bytearrayinputstream.htm
Advertisements