Java.io.ByteArrayInputStream class in Java
java.io.ByteArrayInputStream class contains all the buffers, containing bytes to be read from the Input Stream. There is no IO exception in case of ByteArrayInputStream class methods. Methods of this class can be called even after closing the Stream, there is no effect of it on the class methods.
Declaration :
public class ByteArrayInputStream extends InputStream
Fields
- protected byte[] buf: An array of bytes that was provided by the creator of the stream.
- protected int count: The index one greater than the last valid character in the input stream buffer.
- protected int mark: The currently marked position in the stream.
- protected int pos: This is the index of the next character to read from the input stream buffer.
Constructors :
- ByteArrayInputStream(byte[] buffer) :creates ByteArrayInputStream to use buffer array – “buffer”.
- ByteArrayInputStream(byte[] buf, int offset, int length) :creates ByteArrayInputStream that uses some part of “buffer” i.e. buffer array
Methods:
- mark(int arg) : Java.io.ByteArrayInputStream.mark(int arg) marks the current position of the input stream. It sets readlimit i.e. maximum number of bytes that can be read before mark position becomes invalid.
Syntax :public void mark(int arg) Parameters : arg : integer specifying the read limit of the input Stream Return : void
- read() : java.io.ByteArrayInputStream.read() reads next byte of data from the Input Stream. The value byte is returned in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. Method doesn’t block
Syntax :public int read() Parameters : ------ Return : Reads next data else, -1 i.e. when end of file is reached. Exception : -> IOException : If I/O error occurs.
- close() : java.io.ByteArrayInputStream.close() closes the input stream and releases system resources associated with this stream to Garbage Collector.
Syntax :public void close() Parameters : ------ Return : void Exception : -> IOException : If I/O error occurs.
- read(byte[] buffer, int offset, int maxlen) : Java.io.ByteArrayInputStream.read(byte[] buffer, int offset, int maxlen) reads “buffer” byte array of Input Stream starting from position “offset” to maxlen.
Syntax :public int read(byte[] buffer, int offset, int maxlen) Parameters : arg : array whose number of bytes to be read offset : starting position in buffer from where to read maxlen : maximum no. of bytes to be read Return : reads number of bytes and return to the buffer else, -1 i.e. when end of file is reached. Exception : -> IOException : If I/O error occurs. -> NullPointerException : if arg is null.
- reset() : Java.io.ByteArrayInputStream.reset() is invoked by mark() method. It repositions the input stream to the marked position.
Syntax :public void reset() Parameters : ---- Return : void Exception : -> IOException : If I/O error occurs.
- markSupported() : Java.io.ByteArrayInputStream.markSupported() method tests if this input stream supports the mark and reset methods. The markSupported method of ByteArrayInputStreamInputStream returns true always
Syntax :public boolean markSupported() Parameters : ------- Return : true if input stream supports the mark() and reset() method else,false
- skip(long arg) : Java.io.ByteArrayInputStream.skip(long arg) skips arg bytes in the input stream.
Syntax :public long skip(long arg) Parameters : arg : no. of bytes to be skipped Return : skip bytes. Exception : -> IOException : If I/O error occurs.
- available() : Java.io.ByteArrayInputStream.available() tells total no. of bytes from the Input Stream to be read
Syntax :public int available() Parameters : ----------- Return : total no. of bytes to be read Exception : -----------
Java Program explaining ByteArrayInputStream Class methods :
// Java program illustrating the working of ByteArrayInputStream method // mark(), read(), skip(), available() // markSupported(), close(), reset() import java.io.*; public class NewClass { public static void main(String[] args) throws Exception { byte[] buffer = {71, 69, 69, 75, 83}; ByteArrayInputStream geek = null; try { geek = new ByteArrayInputStream(buffer); // Use of available() method : telling the no. of bytes to be read int number = geek.available(); System.out.println("Use of available() method : " + number); // Use of read() method : reading and printing Characters one by one System.out.println("\nChar : "+(char)geek.read()); System.out.println("Char : "+(char)geek.read()); System.out.println("Char : "+(char)geek.read()); // Use of mark() : geek.mark(0); // Use of skip() : it results in skiping 'k' from "GEEKS" geek.skip(1); System.out.println("skip() method comes to play"); System.out.println("mark() method comes to play"); System.out.println("Char : "+(char)geek.read()); // Use of markSupported boolean check = geek.markSupported(); System.out.println("\nmarkSupported() : " + check); if(geek.markSupported()) { // Use of reset() method : repositioning the stram to marked positions. geek.reset(); System.out.println("\nreset() invoked"); System.out.println("Char : "+(char)geek.read()); System.out.println("Char : "+(char)geek.read()); } else { System.out.println("reset() method not supported."); } System.out.println("geek.markSupported() supported reset() : "+check); } catch(Exception excpt) { // in case of I/O error excpt.printStackTrace(); } finally { // releasing the resources back to the GarbageCollector when closes if(geek!=null) { // Use of close() : closing the file and releasing resources geek.close(); } } } } |
Output :
Use of available() method : 5 Char : G Char : E Char : E skip() method comes to play mark() method comes to play Char : S markSupported() : true reset() invoked Char : K Char : S geek.markSupported() supported reset() : true
Next Article: io.ByteArrayOutputStream() Class in Java
This article is contributed by Mohit Gupta 🙂. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Difference between Abstract Class and Concrete Class in Java
- Using predefined class name as Class or Variable name in Java
- Java.lang.Class class in Java | Set 2
- Java.lang.Class class in Java | Set 1
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java.util.concurrent.Phaser class in Java with Examples




