Skip to content

NullPointerException in TemporaryBuffer.write() when buffer is closed masks underlying errors #239

@pstreef

Description

@pstreef

Problem

TemporaryBuffer.write() throws NullPointerException instead of IOException when called on a closed buffer. This can mask the original error in scenarios where error cleanup code attempts to write to the buffer.

Root cause

After close() or destroy() is called, blocks is set to null. The write(int b) and write(byte[] b, int off, int len) methods don't check for this before accessing blocks:

// write(int b) calls last() which does:
blocks.get(blocks.size() - 1)  // NPE if blocks is null

Impact

When this NPE occurs during error handling, it propagates as a RuntimeException and can mask the original error that triggered the cleanup. This makes debugging difficult since users see an NPE stack trace instead of the actual failure cause.

Proposed fix

Add defensive null checks in both write() methods to throw IOException instead:

if (blocks == null) {
    throw new IOException("Buffer is closed and cannot accept additional data");
}

IOException is the expected exception type for I/O operations on closed streams, and callers typically handle it appropriately during cleanup.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions