Byte array input stream of Java IO class library ByteArrayInputStream

1, Introduction to byte array input stream

ByteArrayInputStream is a byte array input stream, inherited from InputStream. Its internal part contains a buffer, which is a byte array. The buffer array is used to store byte data read from the stream. The internal part of ByteArrayInputStream defines a counter pos, which is used to locate the next byte to be read by the read() method.

2, ByteArrayInputStream source code analysis

1 - member variable

class ByteArrayInputStream extends InputStream {

    //Internal buffered array to hold byte input stream data
    protected byte buf[];

    //Index of bytes to be read
    protected int pos;

    //Byte input stream Tag Index
    protected int mark = 0;

    //The number of valid bytes in the buffer, equal to the next subscript of the last valid byte in the buffer
    protected int count;

2. Construction method

    public ByteArrayInputStream(byte buf[]) {
        this.buf = buf;
        this.pos = 0;
        this.count = buf.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;

ByteArrayInputStream has two constructors. ByteArrayInputStream(byte buf []) initializes some member variables of ByteArrayInputStream by passing in a byte array buf to generate a byte array input stream. ByteArrayInputStream(byte buf[], int offset, int length) initializes the current byte stream by passing in offset and byte array length. The second construction The function additionally marks the start of the read and provides mark rollback.

3 - member method

    public synchronized int read() {
        return (pos < count) ? (buf[pos++] & 0xff) : -1;

    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;

    public synchronized long skip(long n) {
        long k = count - pos;
        if (n < k) {
            k = n < 0 ? 0 : n;

        pos += k;
        return k;

    public synchronized int available() {
        return count - pos;

    public boolean markSupported() {
        return true;

    public void mark(int readAheadLimit) {
        mark = pos;

    public synchronized void reset() {
        pos = mark;

    public void close() throws IOException {

The logic of each member method of byte array input stream is very simple. It is based on the operation of internal buffer array buf and various tag bits pos, mark and count in the array. Here, it will not be analyzed.

Posted on Tue, 04 Feb 2020 08:50:00 -0800 by beezza