Created
August 13, 2014 05:40
-
-
Save normanmaurer/581acebc8b74f14bc8bb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Override | |
| public ByteBuf capacity(int newCapacity) { | |
| ensureAccessible(); | |
| if (newCapacity < 0 || newCapacity > maxCapacity()) { | |
| throw new IllegalArgumentException("newCapacity: " + newCapacity); | |
| } | |
| int readerIndex = readerIndex(); | |
| int writerIndex = writerIndex(); | |
| int oldCapacity = capacity; | |
| if (newCapacity > oldCapacity) { | |
| ByteBuffer oldBuffer = buffer; | |
| ByteBuffer newBuffer = allocateDirect(newCapacity); | |
| oldBuffer.position(0).limit(oldBuffer.capacity()); | |
| newBuffer.position(0).limit(oldBuffer.capacity()); | |
| newBuffer.put(oldBuffer); | |
| newBuffer.clear(); | |
| setByteBuffer(newBuffer); | |
| } else if (newCapacity < oldCapacity) { | |
| ByteBuffer oldBuffer = buffer; | |
| ByteBuffer newBuffer = allocateDirect(newCapacity); | |
| if (readerIndex < newCapacity) { | |
| if (writerIndex > newCapacity) { | |
| writerIndex(writerIndex = newCapacity); | |
| } | |
| oldBuffer.position(readerIndex).limit(writerIndex); | |
| newBuffer.position(readerIndex).limit(writerIndex); | |
| newBuffer.put(oldBuffer); | |
| newBuffer.clear(); | |
| } else { | |
| setIndex(newCapacity, newCapacity); | |
| } | |
| setByteBuffer(newBuffer); | |
| } | |
| return this; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment