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 committed by Trustin Lee
parent 5852d82ad6
commit f6f246cac0
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;
@ -135,7 +139,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());
}
}
@ -196,7 +201,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);
@ -235,7 +241,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);
@ -277,7 +284,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 ++);
}
@ -397,7 +405,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);
@ -438,7 +447,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;
}
@ -511,7 +521,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);
@ -703,7 +714,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

@ -144,7 +144,8 @@ public class ByteBufferBackedChannelBuffer extends AbstractChannelBuffer {
try {
tmpBuf.clear().position(index).limit(index + length);
} catch (IllegalArgumentException e) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to read - Need "
+ (index + length) + ", maximum is " + data.limit());
}
tmpBuf.get(dst, dstIndex, length);
}
@ -155,7 +156,8 @@ public class ByteBufferBackedChannelBuffer extends AbstractChannelBuffer {
try {
tmpBuf.clear().position(index).limit(index + bytesToCopy);
} catch (IllegalArgumentException e) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to read - Need "
+ (index + bytesToCopy) + ", maximum is " + data.limit());
}
dst.put(tmpBuf);
}
@ -307,7 +309,8 @@ public class ByteBufferBackedChannelBuffer extends AbstractChannelBuffer {
try {
src = (ByteBuffer) tmpBuf.clear().position(index).limit(index + length);
} catch (IllegalArgumentException e) {
throw new IndexOutOfBoundsException();
throw new IndexOutOfBoundsException("Too many bytes to read - Need "
+ (index + length));
}
ByteBuffer dst = src.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

@ -53,7 +53,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);
@ -226,7 +227,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;
@ -248,7 +251,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;
@ -272,7 +276,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;
@ -302,7 +308,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;
@ -383,7 +390,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;
@ -405,7 +414,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;
@ -429,7 +439,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;
@ -450,7 +462,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;
@ -489,7 +502,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;
@ -526,7 +540,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);
@ -559,7 +574,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;
}
@ -594,7 +611,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);
@ -637,7 +655,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());
}
}
}