The ByteArrayInputStream class in Java is a byte array input stream that allows us to read byte arrays in byte form.
This article will introduce the usage, features and code examples of ByteArrayInputStream in detail, and display the running results.
1. Overview of ByteArrayInputStream
definition :
- The ByteArrayInputStream class is located in the package and is a subclass of the InputStream class.
- It uses a byte array as an input source, allowing easy reading of the byte array.
Features
- (1) Memory operation: ByteArrayInputStream operates in memory and does not need to interact with external file systems.
- (2) Reusable: The same byte array can be read repeatedly without affecting the original data.
- (3) Random access: The random access function can be achieved by setting marks and reset methods.
2. ByteArrayInputStream construction method
ByteArrayInputStream provides the following two constructors:
ByteArrayInputStream(byte[] buf)
- Parameters: buf is a byte array, as input source.
- Function: Create a ByteArrayInputStream object, using buf as its buffer array.
ByteArrayInputStream(byte[] buf, int offset, int length)
- Parameters: buf is a byte array, offset is the initial position of the buffer array, and length is the number of bytes read from the initial position.
- Function: Create a ByteArrayInputStream object, use buf as its buffer array, and read length bytes from the offset position.
3. Common methods of ByteArrayInputStream
int read()
- Function: Read the next byte of data from the input stream.
- Return value: Returns the read byte, if the end of the stream has reached, returns -1.
int read(byte[] b, int off, int len)
- Function: Read up to len bytes of data from the input stream into the byte array b and starts to store it from the off position.
- Return value: Returns the actual number of bytes read, and -1 if the end of the stream has been reached.
int available()
- Function: Returns the estimated number of bytes remaining in the input stream.
- Return value: the number of bytes remaining.
void mark(int readlimit)
- Function: Set a mark at the current read position, readlimit represents the maximum number of bytes that can be read before the mark position fails.
void reset()
- Function: Reset the read position of the input stream to the last marked position.
long skip(long n)
- Function: Skip and discard n bytes in the input stream.
- Return value: The number of bytes actually skipped.
4. Code examples
Here is an example using ByteArrayInputStream:
import ; import ; public class ByteArrayInputStreamExample { public static void main(String[] args) { byte[] buf = { 65, 66, 67, 68, 69 }; // Byte array ByteArrayInputStream bais = new ByteArrayInputStream(buf); int data; while ((data = ()) != -1) { ((char) data); // Output: ABCDE } // Reset the input stream (); // Read bytes of the specified length byte[] b = new byte[3]; (b, 0, 3); for (byte c : b) { ((char) c); // Output: ABC } // Skip two bytes (2); // Read the remaining bytes while ((data = ()) != -1) { ((char) data); // Output: E } } }
Running results:
ABCDEABCE
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.