Java - ByteArrayInputStream read()



Description

The Java ByteArrayInputStream read() method returns the number of bytes left to be read from this input stream. It returns the value as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, then -1 is returned. This read method cannot block.

Declaration

Following is the declaration for java.io.ByteArrayInputStream.read() method −

public int read()

Parameters

NA

Return Value

The value returns the next byte of data, or -1 if the end of the stream has been reached.

Exception

NA

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. In while loop, we're reading the stream into an int using read() method and then its value is printed by casting into a char.

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);
         
         int b =0;
         
         // read till the end of the stream
         while((b = bais.read())!=-1) {
            
            // convert byte to character
            char c = (char)b;
            
            // print
            System.out.println("byte :"+b+"; char : "+ c);
            
         }
         System.out.print(bais.read()+" Reached the end");
         
      } 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 −

byte :65; char : A
byte :66; char : B
byte :67; char : C
byte :68; char : D
byte :69; char : E
-1 Reached the end

Example 2

The following example shows the usage of Java ByteArrayInputStream read() 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. In if loop, we're checking if stream contains any byte using read() 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);
         
         int b =0;
         
         // read the stream
         if((b = bais.read())!=-1) {
            // convert byte to character
            char c = (char)b;
            
            // print
            System.out.println("byte :"+b+"; 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 −

byte stream is empty
java_bytearrayinputstream.htm
Advertisements