Java : BufferedInputStream with Examples

BufferedInputStream (Java SE 21 & JDK 21) with Examples.
You will find code examples on most BufferedInputStream methods.


Summary

A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created, an internal buffer array is created.

Class diagram

final var file = Path.of("R:", "java-work", "aaa.data");
System.out.println(file); // R:\java-work\aaa.data

Files.write(file, new byte[]{10, 20, 30});

try (final var is = new BufferedInputStream(Files.newInputStream(file))) {
    System.out.println(is.read()); // 10
    System.out.println(is.read()); // 20
    System.out.println(is.read()); // 30
    System.out.println(is.read()); // -1
}

Fields

protected byte[] buf

The internal buffer array where the data is stored.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

protected int count

The index one greater than the index of the last valid byte in the buffer.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

protected int marklimit

The maximum read ahead allowed after a call to the mark method before subsequent calls to the reset method fail.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

protected int markpos

The value of the pos field at the time the last mark method was called.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

protected int pos

The current position in the buffer.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

Fields declared in FilterInputStream

in

Please see the link below.

Constructors

BufferedInputStream (InputStream in)

Creates a BufferedInputStream and saves its argument, the input stream in, for later use.

final byte[] buf = {10, 20, 30};
try (final var is = new BufferedInputStream(new ByteArrayInputStream(buf))) {
    System.out.println(is.read()); // 10
    System.out.println(is.read()); // 20
    System.out.println(is.read()); // 30
    System.out.println(is.read()); // -1
}

BufferedInputStream (InputStream in, int size)

Creates a BufferedInputStream with the specified buffer size, and saves its argument, the input stream in, for later use.

Please see also : BufferedInputStream(InputStream in)

class MyInputStream extends BufferedInputStream {
    MyInputStream(InputStream in) {
        super(in);
    }

    MyInputStream(InputStream in, int size) {
        super(in, size);
    }

    int getBufferSize() {
        return buf.length;
    }
}

final byte[] buf = {10, 20, 30};
try (final var is = new MyInputStream(new ByteArrayInputStream(buf))) {
    System.out.println(is.getBufferSize()); // 8192
}

try (final var is = new MyInputStream(new ByteArrayInputStream(buf), 32)) {
    System.out.println(is.getBufferSize()); // 32
}

Methods

int available ()

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.

final byte[] buf = {10, 20, 30};
try (final var is = new BufferedInputStream(new ByteArrayInputStream(buf))) {
    System.out.println(is.available()); // 3
    System.out.println(is.read()); // 10

    System.out.println(is.available()); // 2
    System.out.println(is.read()); // 20

    System.out.println(is.available()); // 1
    System.out.println(is.read()); // 30

    System.out.println(is.available()); // 0
    System.out.println(is.read()); // -1
}

void close ()

Closes this input stream and releases any system resources associated with the stream.

final var file = Path.of("R:", "java-work", "aaa.data");
Files.write(file, new byte[]{10, 20, 30});

try (final var is = new BufferedInputStream(Files.newInputStream(file))) {
    final var ret = is.readAllBytes();
    System.out.println(Arrays.toString(ret)); // [10, 20, 30]
}
// An example without a try-with-resources statement.
final var file = Path.of("R:", "java-work", "aaa.data");
Files.write(file, new byte[]{10, 20, 30});

final var is = new BufferedInputStream(Files.newInputStream(file));
try {
    final var ret = is.readAllBytes();
    System.out.println(Arrays.toString(ret)); // [10, 20, 30]
} finally {
    is.close();
}

void mark (int readlimit)

See the general contract of the mark method of InputStream.

final byte[] buf = {10, 20, 30, 40, 50};

try (final var is = new BufferedInputStream(new ByteArrayInputStream(buf))) {
    System.out.println(is.read()); // 10
    System.out.println(is.read()); // 20

    is.mark(4);

    System.out.println(is.read()); // 30
    System.out.println(is.read()); // 40
    System.out.println(is.read()); // 50
    System.out.println(is.read()); // -1

    is.reset();

    System.out.println(is.read()); // 30
    System.out.println(is.read()); // 40
    System.out.println(is.read()); // 50
    System.out.println(is.read()); // -1
}

boolean markSupported ()

Tests if this input stream supports the mark and reset methods.

final byte[] buf = {10, 20, 30};

try (final var is = new BufferedInputStream(new ByteArrayInputStream(buf))) {
    System.out.println(is.markSupported()); // true
}

int read ()

See the general contract of the read method of InputStream.

final byte[] buf = {10, 20, 30};
try (final var is = new BufferedInputStream(new ByteArrayInputStream(buf))) {
    System.out.println(is.read()); // 10
    System.out.println(is.read()); // 20
    System.out.println(is.read()); // 30
    System.out.println(is.read()); // -1
}

int read (byte[] b, int off, int len)

Reads bytes from this byte-input stream into the specified byte array, starting at the given offset.

final byte[] buf = {10, 20, 30};

try (final var is = new BufferedInputStream(new ByteArrayInputStream(buf))) {
    final var b1 = new byte[3];

    System.out.println(is.read(b1, 0, 1)); // 1
    System.out.println(Arrays.toString(b1)); // [10, 0, 0]

    final var b2 = new byte[3];

    System.out.println(is.read(b2, 0, 2)); // 2
    System.out.println(Arrays.toString(b2)); // [20, 30, 0]

    final var b3 = new byte[3];

    System.out.println(is.read(b3, 0, 3)); // -1
    System.out.println(Arrays.toString(b3)); // [0, 0, 0]
}

try (final var is = new BufferedInputStream(new ByteArrayInputStream(buf))) {
    final var b1 = new byte[3];

    System.out.println(is.read(b1, 2, 1)); // 1
    System.out.println(Arrays.toString(b1)); // [0, 0, 10]

    final var b2 = new byte[3];

    System.out.println(is.read(b2, 1, 2)); // 2
    System.out.println(Arrays.toString(b2)); // [0, 20, 30]

    final var b3 = new byte[3];

    System.out.println(is.read(b3, 0, 3)); // -1
    System.out.println(Arrays.toString(b3)); // [0, 0, 0]
}

void reset ()

See the general contract of the reset method of InputStream.

final byte[] buf = {10, 20, 30, 40, 50};

try (final var is = new BufferedInputStream(new ByteArrayInputStream(buf))) {
    System.out.println(is.read()); // 10
    System.out.println(is.read()); // 20

    is.mark(4);

    System.out.println(is.read()); // 30
    System.out.println(is.read()); // 40
    System.out.println(is.read()); // 50
    System.out.println(is.read()); // -1

    is.reset();

    System.out.println(is.read()); // 30
    System.out.println(is.read()); // 40
    System.out.println(is.read()); // 50
    System.out.println(is.read()); // -1
}

long skip (long n)

See the general contract of the skip method of InputStream.

final byte[] buf = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};

try (final var is = new BufferedInputStream(new ByteArrayInputStream(buf))) {
    System.out.println(is.read()); // 10

    System.out.println(is.skip(1)); // 1
    System.out.println(is.read()); // 30

    System.out.println(is.skip(2)); // 2
    System.out.println(is.read()); // 60

    System.out.println(is.skip(3)); // 3
    System.out.println(is.read()); // 100

    System.out.println(is.skip(1)); // 0
    System.out.println(is.read()); // -1
}

Methods declared in FilterInputStream

read

Please see the link below.

Methods declared in InputStream

nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo

Please see the link below.


Related posts

To top of page