Java : BufferedOutputStream with Examples

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


Summary

The class implements a buffered output stream. By setting up such an output stream, an application can write bytes to the underlying output stream without necessarily causing a call to the underlying system for each byte written.

Class diagram

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

try (final var os = new BufferedOutputStream(Files.newOutputStream(file))) {
    os.write(10);
    os.write(20);
    os.write(30);
}

final var ret = Files.readAllBytes(file);
System.out.println(Arrays.toString(ret)); // [10, 20, 30]

Fields

protected byte[] buf

The internal buffer where 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 number of valid bytes 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 FilterOutputStream

out

Please see the link below.

Constructors

BufferedOutputStream (OutputStream out)

Creates a new buffered output stream to write data to the specified underlying output stream.

final var out = new ByteArrayOutputStream();
try (final var os = new BufferedOutputStream(out)) {
    os.write(10);
    os.write(20);
    os.write(30);
}

System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30]

BufferedOutputStream (OutputStream out, int size)

Creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size.

class MyOutputStream extends BufferedOutputStream {
    MyOutputStream(OutputStream out) {
        super(out);
    }

    MyOutputStream(OutputStream out, int size) {
        super(out, size);
    }

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

try (final var os = new MyOutputStream(new ByteArrayOutputStream())) {
    System.out.println(os.getBufferSize()); // 8192
}

try (final var os = new MyOutputStream(new ByteArrayOutputStream(), 32)) {
    System.out.println(os.getBufferSize()); // 32
}

Methods

void flush ()

Flushes this buffered output stream.

final var out = new ByteArrayOutputStream();

try (final var os = new BufferedOutputStream(out)) {

    os.write(10);
    System.out.println(Arrays.toString(out.toByteArray())); // []

    os.flush();
    System.out.println(Arrays.toString(out.toByteArray())); // [10]

    os.write(20);
    System.out.println(Arrays.toString(out.toByteArray())); // [10]

    os.flush();
    System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]

    os.write(30);
    System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]
}

System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30]

void write (byte[] b, int off, int len)

Writes len bytes from the specified byte array starting at offset off to this buffered output stream.

final var out = new ByteArrayOutputStream();

try (final var os = new BufferedOutputStream(out)) {

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

    os.write(b, 0, 2);
    os.flush();
    System.out.println(Arrays.toString(out.toByteArray())); // [10, 20]

    os.write(b, 2, 3);
    os.flush();
    System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30, 40, 50]
}

void write (int b)

Writes the specified byte to this buffered output stream.

final var out = new ByteArrayOutputStream();
try (final var os = new BufferedOutputStream(out)) {
    os.write(10);
    os.write(20);
    os.write(30);
}

System.out.println(Arrays.toString(out.toByteArray())); // [10, 20, 30]

Methods declared in FilterOutputStream

close, write

Please see the link below.

Methods declared in OutputStream

nullOutputStream

Please see the link below.


Related posts

To top of page