Added messages to all IndexOutOfBoundsExceptions

I need to implement this to help myself finish more future pull requests which, so far, are plagued by these exceptions with no information available.
This commit is contained in:
Cruz Julian Bishop 2012-05-23 21:22:56 +10:00
parent 03f890a882
commit 71c1a2575d
8 changed files with 85 additions and 41 deletions

View File

@ -42,7 +42,8 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
@Override
public void readerIndex(int readerIndex) {
if (readerIndex < 0 || readerIndex > writerIndex) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Invalid readerIndex: "
+ readerIndex + " - Maximum is " + writerIndex);
}
this.readerIndex = readerIndex;
}
@ -55,7 +56,8 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
@Override
public void writerIndex(int writerIndex) {
if (writerIndex < readerIndex || writerIndex > capacity()) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Invalid writerIndex: "
+ writerIndex + " - Maximum is " + readerIndex + " or " + capacity());
}
this.writerIndex = writerIndex;
}
@ -63,7 +65,9 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
@Override
public void setIndex(int readerIndex, int writerIndex) {
if (readerIndex < 0 || readerIndex > writerIndex || writerIndex > capacity()) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Invalid indexes: readerIndex is "
+ readerIndex + ", writerIndex is "
+ writerIndex + ", capacity is " + capacity());
}
this.readerIndex = readerIndex;
this.writerIndex = writerIndex;
@ -129,7 +133,8 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
@Override
public void ensureWritableBytes(int writableBytes) {
if (writableBytes > writableBytes()) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Writable bytes exceeded: Got "
+ writableBytes + ", maximum is " + writableBytes());
}
}
@ -190,7 +195,8 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
@Override
public void getBytes(int index, ChannelBuffer dst, int length) {
if (length > dst.writableBytes()) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to be read: Need "
+ length + ", maximum is " + dst.writableBytes());
}
getBytes(index, dst, dst.writerIndex(), length);
dst.writerIndex(dst.writerIndex() + length);
@ -229,7 +235,8 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
@Override
public void setBytes(int index, ChannelBuffer src, int length) {
if (length > src.readableBytes()) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to write: Need "
+ length + ", maximum is " + src.readableBytes());
}
setBytes(index, src, src.readerIndex(), length);
src.readerIndex(src.readerIndex() + length);
@ -271,7 +278,8 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
@Override
public byte readByte() {
if (readerIndex == writerIndex) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Readable byte limit exceeded: "
+ readerIndex);
}
return getByte(readerIndex ++);
}
@ -391,7 +399,8 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
@Override
public void readBytes(ChannelBuffer dst, int length) {
if (length > dst.writableBytes()) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to be read: Need "
+ length + ", maximum is " + dst.writableBytes());
}
readBytes(dst, dst.writerIndex(), length);
dst.writerIndex(dst.writerIndex() + length);
@ -432,7 +441,8 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
public void skipBytes(int length) {
int newReaderIndex = readerIndex + length;
if (newReaderIndex > writerIndex) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Readable bytes exceeded - Need "
+ newReaderIndex + ", maximum is " + writerIndex);
}
readerIndex = newReaderIndex;
}
@ -505,7 +515,8 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
@Override
public void writeBytes(ChannelBuffer src, int length) {
if (length > src.readableBytes()) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to write - Need "
+ length + ", maximum is " + src.readableBytes());
}
writeBytes(src, src.readerIndex(), length);
src.readerIndex(src.readerIndex() + length);
@ -697,7 +708,8 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
*/
protected void checkReadableBytes(int minimumReadableBytes) {
if (readableBytes() < minimumReadableBytes) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Not enough readable bytes - Need "
+ minimumReadableBytes + ", maximum is " + readableBytes());
}
}
}

View File

@ -130,7 +130,8 @@ public class BigEndianHeapChannelBuffer extends HeapChannelBuffer {
@Override
public ChannelBuffer copy(int index, int length) {
if (index < 0 || length < 0 || index + length > array.length) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to copy - Need "
+ (index + length) + ", maximum is " + array.length);
}
byte[] copiedArray = new byte[length];

View File

@ -143,7 +143,8 @@ public class ByteBufferBackedChannelBuffer extends AbstractChannelBuffer {
try {
data.limit(index + length).position(index);
} catch (IllegalArgumentException e) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to read - Need "
+ (index + length) + ", maximum is " + data.limit());
}
data.get(dst, dstIndex, length);
}
@ -155,7 +156,8 @@ public class ByteBufferBackedChannelBuffer extends AbstractChannelBuffer {
try {
data.limit(index + bytesToCopy).position(index);
} catch (IllegalArgumentException e) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to read - Need "
+ (index + bytesToCopy) + ", maximum is " + data.limit());
}
dst.put(data);
}
@ -351,7 +353,8 @@ public class ByteBufferBackedChannelBuffer extends AbstractChannelBuffer {
try {
src = (ByteBuffer) buffer.duplicate().position(index).limit(index + length);
} catch (IllegalArgumentException e) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to read - Need "
+ (index + length));
}
ByteBuffer dst = buffer.isDirect() ? ByteBuffer.allocateDirect(length) : ByteBuffer.allocate(length);

View File

@ -69,7 +69,8 @@ public class ChannelBufferInputStream extends InputStream implements DataInput {
throw new IllegalArgumentException("length: " + length);
}
if (length > buffer.readableBytes()) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to be read - Needs "
+ length + ", maximum is " + buffer.readableBytes());
}
this.buffer = buffer;
@ -237,10 +238,11 @@ public class ChannelBufferInputStream extends InputStream implements DataInput {
private void checkAvailable(int fieldSize) throws IOException {
if (fieldSize < 0) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("fieldSize cannot be a negative number");
}
if (fieldSize > available()) {
throw new EOFException();
throw new EOFException("fieldSize is too long! Length is " + fieldSize
+ ", but maximum is " + available());
}
}
}

View File

@ -55,7 +55,8 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
}
if (index + length > capacity()) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to decompose - Need "
+ (index + length) + ", capacity is " + capacity());
}
int componentId = componentId(index);
@ -228,7 +229,9 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
public void getBytes(int index, byte[] dst, int dstIndex, int length) {
int componentId = componentId(index);
if (index > capacity() - length || dstIndex > dst.length - length) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to read - Needs "
+ (index + length) + ", maximum is " + capacity() + " or "
+ dst.length);
}
int i = componentId;
@ -250,7 +253,8 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
int limit = dst.limit();
int length = dst.remaining();
if (index > capacity() - length) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to be read - Needs "
+ (index + length) + ", maximum is " + capacity());
}
int i = componentId;
@ -274,7 +278,9 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) {
int componentId = componentId(index);
if (index > capacity() - length || dstIndex > dst.capacity() - length) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to be read - Needs "
+ (index + length) + " or " + (dstIndex + length) + ", maximum is "
+ capacity() + " or " + dst.capacity());
}
int i = componentId;
@ -307,7 +313,8 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
throws IOException {
int componentId = componentId(index);
if (index > capacity() - length) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to be read - needs "
+ (index + length) + ", maximum of " + capacity());
}
int i = componentId;
@ -388,7 +395,9 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
public void setBytes(int index, byte[] src, int srcIndex, int length) {
int componentId = componentId(index);
if (index > capacity() - length || srcIndex > src.length - length) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to read - needs "
+ (index + length) + " or " + (srcIndex + length) + ", maximum is "
+ capacity() + " or " + src.length);
}
int i = componentId;
@ -410,7 +419,8 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
int limit = src.limit();
int length = src.remaining();
if (index > capacity() - length) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to be written - Needs "
+ (index + length) + ", maximum is " + capacity());
}
int i = componentId;
@ -434,7 +444,9 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) {
int componentId = componentId(index);
if (index > capacity() - length || srcIndex > src.capacity() - length) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to be written - Needs "
+ (index + length) + " or " + (srcIndex + length) + ", maximum is "
+ capacity() + " or " + src.capacity());
}
int i = componentId;
@ -455,7 +467,8 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
throws IOException {
int componentId = componentId(index);
if (index > capacity() - length) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to write - Needs "
+ (index + length) + ", maximum is " + capacity());
}
int i = componentId;
@ -494,7 +507,8 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
throws IOException {
int componentId = componentId(index);
if (index > capacity() - length) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to write - Needs "
+ (index + length) + ", maximum is " + capacity());
}
int i = componentId;
@ -531,7 +545,8 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
public ChannelBuffer copy(int index, int length) {
int componentId = componentId(index);
if (index > capacity() - length) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to copy - Needs "
+ (index + length) + ", maximum is " + capacity());
}
ChannelBuffer dst = factory().getBuffer(order(), length);
@ -564,7 +579,9 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
return ChannelBuffers.EMPTY_BUFFER;
}
} else if (index < 0 || index > capacity() - length) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Invalid index: " + index
+ " - Bytes needed: " + (index + length) + ", maximum is "
+ capacity());
} else if (length == 0) {
return ChannelBuffers.EMPTY_BUFFER;
}
@ -599,7 +616,8 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
public ByteBuffer[] toByteBuffers(int index, int length) {
int componentId = componentId(index);
if (index + length > capacity()) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to convert - Needs"
+ (index + length) + ", maximum is " + capacity());
}
List<ByteBuffer> buffers = new ArrayList<ByteBuffer>(components.length);
@ -642,7 +660,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
}
}
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Invalid index: " + index + ", maximum: " + indices.length);
}
@Override

View File

@ -130,7 +130,8 @@ public class LittleEndianHeapChannelBuffer extends HeapChannelBuffer {
@Override
public ChannelBuffer copy(int index, int length) {
if (index < 0 || length < 0 || index + length > array.length) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Copy could not be completed. Bytes needed: "
+ (index + length) + ", maximum: " + array.length);
}
byte[] copiedArray = new byte[length];

View File

@ -38,11 +38,13 @@ public class SlicedChannelBuffer extends AbstractChannelBuffer implements Wrappe
public SlicedChannelBuffer(ChannelBuffer buffer, int index, int length) {
if (index < 0 || index > buffer.capacity()) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Invalid index of " + index
+ ", maximum is " + buffer.capacity());
}
if (index + length > buffer.capacity()) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Invalid combined index of "
+ (index + length) + ", maximum is " + buffer.capacity());
}
this.buffer = buffer;
@ -245,7 +247,8 @@ public class SlicedChannelBuffer extends AbstractChannelBuffer implements Wrappe
private void checkIndex(int index) {
if (index < 0 || index >= capacity()) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Invalid index: " + index
+ ", maximum is " + capacity());
}
}
@ -255,10 +258,11 @@ public class SlicedChannelBuffer extends AbstractChannelBuffer implements Wrappe
"length is negative: " + length);
}
if (startIndex < 0) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("startIndex cannot be negative");
}
if (startIndex + length > capacity()) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Index too big - Bytes needed: "
+ (startIndex + length) + ", maximum is " + capacity());
}
}
}

View File

@ -37,7 +37,8 @@ public class TruncatedChannelBuffer extends AbstractChannelBuffer implements Wra
public TruncatedChannelBuffer(ChannelBuffer buffer, int length) {
if (length > buffer.capacity()) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Length is too large, got "
+ length + " but can't go higher than " + buffer.capacity());
}
this.buffer = buffer;
@ -239,7 +240,8 @@ public class TruncatedChannelBuffer extends AbstractChannelBuffer implements Wra
private void checkIndex(int index) {
if (index < 0 || index >= capacity()) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Invalid index of " + index
+ ", maximum is " + capacity());
}
}
@ -249,7 +251,8 @@ public class TruncatedChannelBuffer extends AbstractChannelBuffer implements Wra
"length is negative: " + length);
}
if (index + length > capacity()) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Invalid index of "
+ (index + length) + ", maximum is " + capacity());
}
}
}