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.
public class ByteArrayInputStream extends InputStream
- 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.
- 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
- 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.
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
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.
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.
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.
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
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.
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
public int available() Parameters : ----------- Return : total no. of bytes to be read Exception : -----------
Java Program explaining ByteArrayInputStream Class methods :
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 write.geeksforgeeks.org or mail your article to email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.