[#1628] Fix bug in ReadOnlyByteByteBufferBuf where get operations threw ReadOnlyBufferException

This commit is contained in:
Norman Maurer 2013-07-22 07:12:05 +02:00
parent feb8d101bd
commit f478fcd3b0
1 changed files with 23 additions and 2 deletions

View File

@ -214,12 +214,33 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf {
@Override
public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
throw new ReadOnlyBufferException();
ensureAccessible();
if (length == 0) {
return this;
}
if (buffer.hasArray()) {
out.write(buffer.array(), index + buffer.arrayOffset(), length);
} else {
byte[] tmp = new byte[length];
ByteBuffer tmpBuf = internalNioBuffer();
tmpBuf.clear().position(index);
tmpBuf.get(tmp);
out.write(tmp);
}
return this;
}
@Override
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
throw new ReadOnlyBufferException();
ensureAccessible();
if (length == 0) {
return 0;
}
ByteBuffer tmpBuf = internalNioBuffer();
tmpBuf.clear().position(index).limit(index + length);
return out.write(tmpBuf);
}
@Override