Consistent exception messages
This commit is contained in:
parent
f670a7c923
commit
d0afe6cad0
@ -68,8 +68,8 @@ public abstract class AbstractByteBuf implements ByteBuf {
|
|||||||
@Override
|
@Override
|
||||||
public ByteBuf readerIndex(int readerIndex) {
|
public ByteBuf readerIndex(int readerIndex) {
|
||||||
if (readerIndex < 0 || readerIndex > writerIndex) {
|
if (readerIndex < 0 || readerIndex > writerIndex) {
|
||||||
throw new IndexOutOfBoundsException("Invalid readerIndex: "
|
throw new IndexOutOfBoundsException(String.format(
|
||||||
+ readerIndex + " - Maximum is " + writerIndex);
|
"readerIndex: %d (expected: 0 <= readerIndex <= writerIndex(%d))", readerIndex, writerIndex));
|
||||||
}
|
}
|
||||||
this.readerIndex = readerIndex;
|
this.readerIndex = readerIndex;
|
||||||
return this;
|
return this;
|
||||||
@ -83,8 +83,9 @@ public abstract class AbstractByteBuf implements ByteBuf {
|
|||||||
@Override
|
@Override
|
||||||
public ByteBuf writerIndex(int writerIndex) {
|
public ByteBuf writerIndex(int writerIndex) {
|
||||||
if (writerIndex < readerIndex || writerIndex > capacity()) {
|
if (writerIndex < readerIndex || writerIndex > capacity()) {
|
||||||
throw new IndexOutOfBoundsException("Invalid writerIndex: "
|
throw new IndexOutOfBoundsException(String.format(
|
||||||
+ writerIndex + " - Maximum is " + readerIndex + " or " + capacity());
|
"writerIndex: %d (expected: readerIndex(%d) <= writerIndex <= capacity(%d))",
|
||||||
|
writerIndex, readerIndex, capacity()));
|
||||||
}
|
}
|
||||||
this.writerIndex = writerIndex;
|
this.writerIndex = writerIndex;
|
||||||
return this;
|
return this;
|
||||||
@ -93,9 +94,9 @@ public abstract class AbstractByteBuf implements ByteBuf {
|
|||||||
@Override
|
@Override
|
||||||
public ByteBuf setIndex(int readerIndex, int writerIndex) {
|
public ByteBuf setIndex(int readerIndex, int writerIndex) {
|
||||||
if (readerIndex < 0 || readerIndex > writerIndex || writerIndex > capacity()) {
|
if (readerIndex < 0 || readerIndex > writerIndex || writerIndex > capacity()) {
|
||||||
throw new IndexOutOfBoundsException("Invalid indexes: readerIndex is "
|
throw new IndexOutOfBoundsException(String.format(
|
||||||
+ readerIndex + ", writerIndex is "
|
"readerIndex: %d, writerIndex: %d (expected: 0 <= readerIndex <= writerIndex <= capacity(%d))",
|
||||||
+ writerIndex + ", capacity is " + capacity());
|
readerIndex, writerIndex, capacity()));
|
||||||
}
|
}
|
||||||
this.readerIndex = readerIndex;
|
this.readerIndex = readerIndex;
|
||||||
this.writerIndex = writerIndex;
|
this.writerIndex = writerIndex;
|
||||||
@ -227,8 +228,8 @@ public abstract class AbstractByteBuf implements ByteBuf {
|
|||||||
|
|
||||||
if (minWritableBytes > maxCapacity - writerIndex) {
|
if (minWritableBytes > maxCapacity - writerIndex) {
|
||||||
throw new IndexOutOfBoundsException(String.format(
|
throw new IndexOutOfBoundsException(String.format(
|
||||||
"writerIndex(%d) + minWritableBytes(%d) exceeds maxCapacity(%d)",
|
"writerIndex(%d) + minWritableBytes(%d) exceeds maxCapacity(%d): %s",
|
||||||
writerIndex, minWritableBytes, maxCapacity));
|
writerIndex, minWritableBytes, maxCapacity, this));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Normalize the current capacity to the power of 2.
|
// Normalize the current capacity to the power of 2.
|
||||||
@ -371,9 +372,14 @@ public abstract class AbstractByteBuf implements ByteBuf {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ByteBuf getBytes(int index, ByteBuf dst, int length) {
|
public ByteBuf getBytes(int index, ByteBuf dst, int length) {
|
||||||
|
checkIndex(index, length);
|
||||||
|
if (dst == null) {
|
||||||
|
throw new NullPointerException("dst");
|
||||||
|
}
|
||||||
|
|
||||||
if (length > dst.writableBytes()) {
|
if (length > dst.writableBytes()) {
|
||||||
throw new IndexOutOfBoundsException("Too many bytes to be read: Need "
|
throw new IndexOutOfBoundsException(String.format(
|
||||||
+ length + ", maximum is " + dst.writableBytes());
|
"length(%d) exceeds dst.writableBytes(%d) where dst is: %s", length, dst.writableBytes(), dst));
|
||||||
}
|
}
|
||||||
getBytes(index, dst, dst.writerIndex(), length);
|
getBytes(index, dst, dst.writerIndex(), length);
|
||||||
dst.writerIndex(dst.writerIndex() + length);
|
dst.writerIndex(dst.writerIndex() + length);
|
||||||
@ -418,10 +424,15 @@ public abstract class AbstractByteBuf implements ByteBuf {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ByteBuf setBytes(int index, ByteBuf src, int length) {
|
public ByteBuf setBytes(int index, ByteBuf src, int length) {
|
||||||
if (length > src.readableBytes()) {
|
checkIndex(index, length);
|
||||||
throw new IndexOutOfBoundsException("Too many bytes to write: Need "
|
if (src == null) {
|
||||||
+ length + ", maximum is " + src.readableBytes());
|
throw new NullPointerException("src");
|
||||||
}
|
}
|
||||||
|
if (length > src.readableBytes()) {
|
||||||
|
throw new IndexOutOfBoundsException(String.format(
|
||||||
|
"length(%d) exceeds src.readableBytes(%d) where src is: %s", length, src.readableBytes(), src));
|
||||||
|
}
|
||||||
|
|
||||||
setBytes(index, src, src.readerIndex(), length);
|
setBytes(index, src, src.readerIndex(), length);
|
||||||
src.readerIndex(src.readerIndex() + length);
|
src.readerIndex(src.readerIndex() + length);
|
||||||
return this;
|
return this;
|
||||||
@ -432,10 +443,8 @@ public abstract class AbstractByteBuf implements ByteBuf {
|
|||||||
if (length == 0) {
|
if (length == 0) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
if (length < 0) {
|
|
||||||
throw new IllegalArgumentException(
|
checkIndex(index, length);
|
||||||
"length must be 0 or greater than 0.");
|
|
||||||
}
|
|
||||||
|
|
||||||
int nLong = length >>> 3;
|
int nLong = length >>> 3;
|
||||||
int nBytes = length & 7;
|
int nBytes = length & 7;
|
||||||
@ -464,8 +473,7 @@ public abstract class AbstractByteBuf implements ByteBuf {
|
|||||||
@Override
|
@Override
|
||||||
public byte readByte() {
|
public byte readByte() {
|
||||||
if (readerIndex == writerIndex) {
|
if (readerIndex == writerIndex) {
|
||||||
throw new IndexOutOfBoundsException("Readable byte limit exceeded: "
|
throw new IndexOutOfBoundsException("readerIndex(" + readerIndex + ") == writerIndex(" + writerIndex + ')');
|
||||||
+ readerIndex);
|
|
||||||
}
|
}
|
||||||
return getByte(readerIndex ++);
|
return getByte(readerIndex ++);
|
||||||
}
|
}
|
||||||
@ -590,8 +598,8 @@ public abstract class AbstractByteBuf implements ByteBuf {
|
|||||||
@Override
|
@Override
|
||||||
public ByteBuf readBytes(ByteBuf dst, int length) {
|
public ByteBuf readBytes(ByteBuf dst, int length) {
|
||||||
if (length > dst.writableBytes()) {
|
if (length > dst.writableBytes()) {
|
||||||
throw new IndexOutOfBoundsException("Too many bytes to be read: Need "
|
throw new IndexOutOfBoundsException(String.format(
|
||||||
+ length + ", maximum is " + dst.writableBytes());
|
"length(%d) exceeds dst.writableBytes(%d) where dst is: %s", length, dst.writableBytes(), dst));
|
||||||
}
|
}
|
||||||
readBytes(dst, dst.writerIndex(), length);
|
readBytes(dst, dst.writerIndex(), length);
|
||||||
dst.writerIndex(dst.writerIndex() + length);
|
dst.writerIndex(dst.writerIndex() + length);
|
||||||
@ -636,8 +644,9 @@ public abstract class AbstractByteBuf implements ByteBuf {
|
|||||||
public ByteBuf skipBytes(int length) {
|
public ByteBuf skipBytes(int length) {
|
||||||
int newReaderIndex = readerIndex + length;
|
int newReaderIndex = readerIndex + length;
|
||||||
if (newReaderIndex > writerIndex) {
|
if (newReaderIndex > writerIndex) {
|
||||||
throw new IndexOutOfBoundsException("Readable bytes exceeded - Need "
|
throw new IndexOutOfBoundsException(String.format(
|
||||||
+ newReaderIndex + ", maximum is " + writerIndex);
|
"length: %d (expected: readerIndex(%d) + length <= writerIndex(%d))",
|
||||||
|
length, readerIndex, writerIndex));
|
||||||
}
|
}
|
||||||
readerIndex = newReaderIndex;
|
readerIndex = newReaderIndex;
|
||||||
return this;
|
return this;
|
||||||
@ -729,8 +738,8 @@ public abstract class AbstractByteBuf implements ByteBuf {
|
|||||||
@Override
|
@Override
|
||||||
public ByteBuf writeBytes(ByteBuf src, int length) {
|
public ByteBuf writeBytes(ByteBuf src, int length) {
|
||||||
if (length > src.readableBytes()) {
|
if (length > src.readableBytes()) {
|
||||||
throw new IndexOutOfBoundsException("Too many bytes to write - Need "
|
throw new IndexOutOfBoundsException(String.format(
|
||||||
+ length + ", maximum is " + src.readableBytes());
|
"length(%d) exceeds src.readableBytes(%d) where src is: %s", length, src.readableBytes(), src));
|
||||||
}
|
}
|
||||||
writeBytes(src, src.readerIndex(), length);
|
writeBytes(src, src.readerIndex(), length);
|
||||||
src.readerIndex(src.readerIndex() + length);
|
src.readerIndex(src.readerIndex() + length);
|
||||||
@ -781,10 +790,9 @@ public abstract class AbstractByteBuf implements ByteBuf {
|
|||||||
if (length == 0) {
|
if (length == 0) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
if (length < 0) {
|
|
||||||
throw new IllegalArgumentException(
|
checkIndex(writerIndex, length);
|
||||||
"length must be 0 or greater than 0.");
|
|
||||||
}
|
|
||||||
int nLong = length >>> 3;
|
int nLong = length >>> 3;
|
||||||
int nBytes = length & 7;
|
int nBytes = length & 7;
|
||||||
for (int i = nLong; i > 0; i --) {
|
for (int i = nLong; i > 0; i --) {
|
||||||
@ -968,7 +976,7 @@ public abstract class AbstractByteBuf implements ByteBuf {
|
|||||||
|
|
||||||
protected final void checkIndex(int index, int fieldLength) {
|
protected final void checkIndex(int index, int fieldLength) {
|
||||||
checkUnfreed();
|
checkUnfreed();
|
||||||
if (index < 0 || index > capacity() - fieldLength) {
|
if (index < 0 || index > capacity() - fieldLength || fieldLength < 0) {
|
||||||
throw new IndexOutOfBoundsException(String.format(
|
throw new IndexOutOfBoundsException(String.format(
|
||||||
"index: %d, length: %d (expected: range(0, %d))", index, fieldLength, capacity()));
|
"index: %d, length: %d (expected: range(0, %d))", index, fieldLength, capacity()));
|
||||||
}
|
}
|
||||||
@ -981,9 +989,10 @@ public abstract class AbstractByteBuf implements ByteBuf {
|
|||||||
*/
|
*/
|
||||||
protected final void checkReadableBytes(int minimumReadableBytes) {
|
protected final void checkReadableBytes(int minimumReadableBytes) {
|
||||||
checkUnfreed();
|
checkUnfreed();
|
||||||
if (readableBytes() < minimumReadableBytes) {
|
if (readerIndex > writerIndex - minimumReadableBytes) {
|
||||||
throw new IndexOutOfBoundsException("Not enough readable bytes - Need "
|
throw new IndexOutOfBoundsException(String.format(
|
||||||
+ minimumReadableBytes + ", maximum is " + readableBytes());
|
"readerIndex(%d) + length(%d) exceeds writerIndex(%d): %s",
|
||||||
|
readerIndex, minimumReadableBytes, writerIndex, this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -276,7 +276,6 @@ public final class Unpooled {
|
|||||||
@Override
|
@Override
|
||||||
public void free() {
|
public void free() {
|
||||||
// do nothing
|
// do nothing
|
||||||
// TODO: Maybe throw an UnsupportedOperationException
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -243,29 +243,31 @@ final class UnpooledDirectByteBuf extends AbstractByteBuf {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
|
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
|
||||||
checkUnfreed();
|
checkIndex(index, length);
|
||||||
ByteBuffer tmpBuf = internalNioBuffer();
|
if (dst == null) {
|
||||||
try {
|
throw new NullPointerException("dst");
|
||||||
tmpBuf.clear().position(index).limit(index + length);
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
throw new IndexOutOfBoundsException("Too many bytes to read - Need " +
|
|
||||||
(index + length) + ", maximum is " + buffer.limit());
|
|
||||||
}
|
}
|
||||||
|
if (dstIndex < 0 || dstIndex > dst.length - length) {
|
||||||
|
throw new IndexOutOfBoundsException(String.format(
|
||||||
|
"dstIndex: %d, length: %d (expected: range(0, %d))", dstIndex, length, dst.length));
|
||||||
|
}
|
||||||
|
|
||||||
|
ByteBuffer tmpBuf = internalNioBuffer();
|
||||||
|
tmpBuf.clear().position(index).limit(index + length);
|
||||||
tmpBuf.get(dst, dstIndex, length);
|
tmpBuf.get(dst, dstIndex, length);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ByteBuf getBytes(int index, ByteBuffer dst) {
|
public ByteBuf getBytes(int index, ByteBuffer dst) {
|
||||||
checkUnfreed();
|
checkIndex(index);
|
||||||
|
if (dst == null) {
|
||||||
|
throw new NullPointerException("dst");
|
||||||
|
}
|
||||||
|
|
||||||
int bytesToCopy = Math.min(capacity() - index, dst.remaining());
|
int bytesToCopy = Math.min(capacity() - index, dst.remaining());
|
||||||
ByteBuffer tmpBuf = internalNioBuffer();
|
ByteBuffer tmpBuf = internalNioBuffer();
|
||||||
try {
|
|
||||||
tmpBuf.clear().position(index).limit(index + bytesToCopy);
|
tmpBuf.clear().position(index).limit(index + bytesToCopy);
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
throw new IndexOutOfBoundsException("Too many bytes to read - Need " +
|
|
||||||
(index + bytesToCopy) + ", maximum is " + buffer.limit());
|
|
||||||
}
|
|
||||||
dst.put(tmpBuf);
|
dst.put(tmpBuf);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -332,12 +332,7 @@ final class UnpooledHeapByteBuf extends AbstractByteBuf {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ByteBuf copy(int index, int length) {
|
public ByteBuf copy(int index, int length) {
|
||||||
checkUnfreed();
|
checkIndex(index, length);
|
||||||
if (index < 0 || length < 0 || index + length > array.length) {
|
|
||||||
throw new IndexOutOfBoundsException("Too many bytes to copy - Need "
|
|
||||||
+ (index + length) + ", maximum is " + array.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
byte[] copiedArray = new byte[length];
|
byte[] copiedArray = new byte[length];
|
||||||
System.arraycopy(array, index, copiedArray, 0, length);
|
System.arraycopy(array, index, copiedArray, 0, length);
|
||||||
return new UnpooledHeapByteBuf(alloc(), copiedArray, maxCapacity());
|
return new UnpooledHeapByteBuf(alloc(), copiedArray, maxCapacity());
|
||||||
|
Loading…
Reference in New Issue
Block a user