- Java.io package classes
- 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 extras
- Java.io - Interfaces
- Java.io - Exceptions
- Java.io package Useful Resources
- Java.io - Discussion
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