1. ByteArrayInputStream stream definition
API Note: ByteArrayInputStream contains an internal buffer that contains bytes that can be read from the stream, the internal counter tracks the next byte provided by the read method, closing the ByteArrayInputStream stream is invalid, and methods that call classes after closing the stream will not produce exceptions
2. ByteArrayInputStream stream instance domain
/** * Byte array buffer, buf[0] to buf[count-1] is a byte that can be read from the stream, buf[pos] is the next byte read */ protected byte buf[]; /** *Read Byte Index */ protected int pos; /** * The position of the current tag in the stream, which is 0 by default, can be set as a new tag point by the mark method, and then as a tag point by the reset method * Read data from marker point * * @since JDK1.1 */ protected int mark = 0; /** * Index end position + 1, not greater than buffer length */ protected int count;
3. ByteArrayInputStream Flow Constructor
/** * Creates a ByteArrayInputStream stream using the specified byte array, which is the buffer of the stream. * The current position index pos has an initial value of 0, and the index end position count is the length of the buf */ public ByteArrayInputStream(byte buf[]) { this.buf = buf; this.pos = 0; this.count = buf.length; } /** * Create a ByteArrayInputStream stream with the specified array * Target array is the buffer array of the stream * Buffer current start position variable value is off * The index end position of the buffer is the minimum of buf.length and off+length */ public ByteArrayInputStream(byte buf[], int offset, int length) { this.buf = buf; this.pos = offset; this.count = Math.min(offset + length, buf.length); this.mark = offset; }
4. ByteArrayInputStream Flow Method
1) read(): Reads the next byte from this input stream and returns -1 when the stream reaches the end
/** * Read the next byte from this input stream and return * When the stream reaches the end, it returns -1 * Note & 0xff is a byte complement operation, not to be ignored for the moment */ public synchronized int read() { return (pos < count) ? (buf[pos++] & 0xff) : -1; }
2) read(byte b[], int off, int len): Reads up to len bytes from the input stream into the target array, returning the number of bytes actually read
/** * Reads up to len bytes from the input stream into the target array, returning the actual number of bytes read * When the number of remaining characters in the buffer is less than len bytes, the number of remaining characters in the read buffer * Read len bytes when the number of remaining characters is greater than len bytes */ public synchronized int read(byte b[], int off, int len) { if (b == null) { throw new NullPointerException(); } else if (off < 0 || len < 0 || len > b.length - off) { throw new IndexOutOfBoundsException(); } if (pos >= count) { return -1; } int avail = count - pos; if (len > avail) { len = avail; } if (len <= 0) { return 0; } System.arraycopy(buf, pos, b, off, len); pos += len; return len; }
3) close(): Close stream is invalid, there will be no exception when calling other methods after closing
/** * Closing stream is invalid, there will be no exception when calling other methods after closing */ public void close() throws IOException { }
5. Role of ByteArrayInputStream Flow
The specific role of the stream is not understood for the time being, and it is not clear when the stream will be used because the actual project is not yet used, so its functions can be understood first