Java ByteArrayInputStream Class
In Java language, ByteArrayInputStream class contains buffer in the memory which is used to read byte array as the input stream.
Syntax:
public class ByteArrayInputStream extends InputStream
ByteArrayInputStream class constructors
| S.No | Constructor | Description |
|---|---|---|
| 1 | ByteArrayInputStream(byte [] buf) | ByteArrayInputStream which uses buf as its buffer array. |
| 2 | ByteArrayInputStream(byte[] buf, int offset, int len) | ByteArrayInputStream which uses buf as its buffer array, where off and len are the first byte and specified number of bytes to be read. |
ByteArrayInputStream class Methods
| S.No | Method | Description |
|---|---|---|
| 1 | public void mark(int read) | This method is used to sets the current marked position in the stream. |
| 2 | public int available() | This method is used to return the number of bytes that can be read from this file input stream. |
| 3 | public int read() | This method is used to reads the next byte of data from the InputStream. |
| 4 | public int read(byte[] ar, int off, int len) | This method is used to read up to len bytes of data from an array of bytes in the input stream. |
| 5 | public long skip(long n) | This method is used to skips ‘n’ number of bytes from the stream |
| 6 | void close() | This method is used for closing a ByteArrayInputStream. |
| 7 | void reset() | This method is used to reset the buffer of a byte array. |
Example:
import java.io.*;
public class ByteArrayInputStreamDemo{
public static void main(String args[]){
byte[] b = { 33,34,35};
int c;
ByteArrayInputStream bInput = new ByteArrayInputStream(b);
while(( c = bInput.read())!= -1) {
System.out.println("Special character is:" +(char)c);
}
bInput.reset();
}
}
Output
Special character is:! Special character is:" Special character is:#
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com