Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DirectBufferOutputStream fails with insufficient capacity exception #150

Closed
terzerm opened this issue Jul 26, 2018 · 0 comments
Closed

DirectBufferOutputStream fails with insufficient capacity exception #150

terzerm opened this issue Jul 26, 2018 · 0 comments

Comments

@terzerm
Copy link

terzerm commented Jul 26, 2018

Writing a byte array to DirectBufferOutputStream fails when attempting to make use of the full capacity of the buffer. The reason is a bug in the capacity check in

    public void write(final byte[] srcBytes, final int srcOffset, final int length)
    {
        final long resultingOffset = position + ((long)length);
        if (resultingOffset >= this.length)
        {
            throw new IllegalStateException("insufficient capacity in the buffer");
        }

        buffer.putBytes(offset + position, srcBytes, srcOffset, length);
        position += length;
    }

The second line should be

        if (resultingOffset > this.length)

The following unit test reproduces the problem:

    @Test
    public void shouldWriteByteArray() throws IOException
    {
        final byte[] data = new byte[2];
        final MutableDirectBuffer buffer = new UnsafeBuffer(data);
        final DirectBufferOutputStream outputStream = new DirectBufferOutputStream(buffer);

        final byte[] source = { 1, 2 };
        outputStream.write(source);

        assertArrayEquals(source, data);
    }
terzerm pushed a commit to terzerm/agrona that referenced this issue Jul 26, 2018
… byte array to DirectBufferOutputStream
mjpt777 added a commit that referenced this issue Jul 26, 2018
…th write operations and provide non-exception throwing version of write. Issue #150.
terzerm pushed a commit to terzerm/agrona that referenced this issue Jul 26, 2018
… byte array to DirectBufferOutputStream
@mjpt777 mjpt777 closed this as completed Jul 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants