Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Source/com/drew/lang/SequentialByteArrayReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public byte getByte() throws IOException
@Override
public byte[] getBytes(int count) throws IOException
{
if (count < 0) {
throw new IllegalArgumentException("Requested byte count must not be negative");
}
if ((long)_index + count > _bytes.length) {
throw new EOFException("End of data reached.");
}
Expand All @@ -84,6 +87,12 @@ public byte[] getBytes(int count) throws IOException
@Override
public void getBytes(@NotNull byte[] buffer, int offset, int count) throws IOException
{
if (count < 0) {
throw new IllegalArgumentException("Requested byte count must not be negative");
}
if (offset < 0) {
throw new IllegalArgumentException("Buffer offset must not be negative");
}
if ((long)_index + count > _bytes.length) {
throw new EOFException("End of data reached.");
}
Expand Down
25 changes: 25 additions & 0 deletions Tests/com/drew/lang/SequentialByteArrayReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import org.junit.Test;

import java.io.IOException;

/**
* @author Drew Noakes https://drewnoakes.com
*/
Expand All @@ -34,6 +36,29 @@ public void testConstructWithNullStreamThrows()
new SequentialByteArrayReader(null);
}

@Test(expected = IllegalArgumentException.class)
public void testGetBytesWithNegativeCountThrows() throws IOException
{
SequentialByteArrayReader reader = new SequentialByteArrayReader(new byte[10]);
reader.getBytes(-1);
}

@Test(expected = IllegalArgumentException.class)
public void testGetBytesWithNegativeOffsetThrows() throws IOException
{
SequentialByteArrayReader reader = new SequentialByteArrayReader(new byte[10]);
byte[] buffer = new byte[5];
reader.getBytes(buffer, -1, 5);
}

@Test(expected = IllegalArgumentException.class)
public void testGetBytesWithNegativeCountInOverloadThrows() throws IOException
{
SequentialByteArrayReader reader = new SequentialByteArrayReader(new byte[10]);
byte[] buffer = new byte[5];
reader.getBytes(buffer, 0, -1);
}

@Override
protected SequentialReader createReader(byte[] bytes)
{
Expand Down