
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - ByteArrayInputStream read() method
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 - Using ByteArrayInputStream read() method
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.
ByteArrayInputStreamDemo.java
package com.tutorialspoint; import java.io.ByteArrayInputStream; import java.io.IOException; public class ByteArrayInputStreamDemo { 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 - Using ByteArrayInputStream read() method
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.
ByteArrayInputStreamDemo.java
package com.tutorialspoint; import java.io.ByteArrayInputStream; import java.io.IOException; public class ByteArrayInputStreamDemo { 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
Example - Using ByteArrayInputStream read() method
The following example shows the usage of Java ByteArrayInputStream read() method.
ByteArrayInputStreamDemo.java
package com.tutorialspoint; import java.io.ByteArrayInputStream; public class ByteArrayInputStreamDemo { public static void main(String[] args) { // Create a byte array with data byte[] data = {72, 101, 108, 108, 111}; // Corresponds to 'H', 'e', 'l', 'l', 'o' // Create a ByteArrayInputStream ByteArrayInputStream inputStream = new ByteArrayInputStream(data); System.out.println("Reading bytes from ByteArrayInputStream:"); int byteData; // Read bytes one at a time until the end of the stream while ((byteData = inputStream.read()) != -1) { // Convert the byte to a character and print it System.out.print((char) byteData); } } }
Output
Let us compile and run the above program, this will produce the following result −
Reading bytes from ByteArrayInputStream: Hello
Explanation
Initialization− A ByteArrayInputStream is created with a byte array containing ASCII values for the string "Hello" (72 = 'H', 101 = 'e', etc.).
-
Reading Bytes−
The read() method is called in a loop to read one byte at a time from the stream.
Each byte is returned as an integer, and it is cast to a char to display the corresponding character.
The loop continues until read() returns -1, indicating the end of the stream.
Output− The program prints the characters corresponding to the byte values in the input array.
Key Points
The read() method reads one byte at a time and advances the stream's position by one byte.
When the end of the stream is reached, read() returns -1.
This method is commonly used in loops to process the contents of an input stream byte by byte.
You can combine read() with other methods like available() to manage the reading process more efficiently.