Introduction to Bytearrayinputstream, source analysis and examples (including InputStream)
We use Bytearrayinputstream to open the "input stream" to the byte type of learning prologue.
In this chapter, we will first introduce the Bytearrayinputstream, then take a deep look at its source, and finally use the example to master its usage.
Bytearrayinputstream Introduction
Bytearrayinputstream is a byte array input stream. It inherits from InputStream.
It contains an internal buffer that contains the bytes read from the stream; In layman's terms, its internal buffer is a byte array, and the Bytearrayinputstream essence is implemented by byte arrays.
As we all know, InputStream provides an external interface to read byte data through the read (), while the interior of the Bytearrayinputstream defines a counter that is used to track the next byte to be read by the read () method.
InputStream Function List
Constructor
InputStream ()
int available () void close ()
void mark (int readlimit)
Boolean marksupported ()
int read (byte[] buffer
abstract int read ()
int Read ( byte[] buffer, int offset, int length)
synchronized void Reset ()
long Skip (long ByteCount)
Bytearrayinputstream Function List
Constructor
Bytearrayinputstream (byte[] buf)
bytearrayinputstream (byte[] buf, int offset, int length)
synchronized int available ()
void Close ()
synchronized void mark (int readlimit)
Boolean marksupported ()
synchronized int read ()
synchronized int read (byte[] buffer, int offset, int length)
synchronized void Reset ()
synchronized long Skip (long ByteCount)
InputStream and Bytearrayinputstream Source analysis
InputStream is the bytearrayinputstream of the parent class, we first look at the source of InputStream, and then learn Bytearrayinputstream source code.
1. Inputstream.java source Analysis (based on jdk1.7.40)
Package java.io; Public abstract class InputStream implements Closeable {//can SKIP size private static final int Max_skip_buffe
R_size = 2048;
Reads the next byte of data from the input stream.
public abstract int Read () throws IOException;
Reads data from the input stream into a byte array.
public int read (byte b[]) throws IOException {return read (b, 0, b.length);
///Read the maximum Len data byte from this input stream into the byte array. public int read (byte b[], int off, int len) throws IOException {if (b = = null) {throw new Nullpointe
Rexception ();
else if (Off < 0 | | Len < 0 | | len > B.length-off) {throw new indexoutofboundsexception ();
else if (len = = 0) {return 0;
int c = Read ();
if (c = = 1) {return-1;
} B[off] = (byte) c;
int i = 1;
try {for (; i < Len; i++) {c = read ();
if (c = = 1) { Break
} B[off + i] = (byte) c;
} catch (IOException ee) {} return i;
}//Skip n bytes in the input stream public long skip (long N) throws IOException {long remaining = n;
int nr;
if (n <= 0) {return 0;
int size = (int) math.min (max_skip_buffer_size, remaining);
byte[] Skipbuffer = new Byte[size];
while (Remaining > 0) {nr = read (Skipbuffer, 0, (int) math.min (size, remaining));
if (NR < 0) {break;
} remaining-= NR;
return n-remaining;
public int available () throws IOException {return 0;
public void Close () throws IOException {} public synchronized void mark (int readlimit) {} Public synchronized void Reset () throws IOException {throw new IOException ("mark/rESET not supported ");
public boolean marksupported () {return false; }
}