Skip to content

Instantly share code, notes, and snippets.

@bhasudha
Created June 4, 2013 22:24
Show Gist options
  • Save bhasudha/5710154 to your computer and use it in GitHub Desktop.
Save bhasudha/5710154 to your computer and use it in GitHub Desktop.
Netty 3.x unbounded DynamicChannelBuffer growth. See http://stackoverflow.com/questions/12134212/trim-dynamicbuffer-to-maintain-size for more information.
@Override
public void ensureWritableBytes(int minWritableBytes) {
if (minWritableBytes <= writableBytes()) {
return;
}
int newCapacity;
if (capacity() == 0) {
newCapacity = 1;
} else {
newCapacity = capacity();
}
int minNewCapacity = writerIndex() + minWritableBytes;
while (newCapacity < minNewCapacity) {
newCapacity <<= 1;
// Check if we exceeded the maximum size of 2gb if this is the case then
// newCapacity == 0
//
// https://github.com/netty/netty/issues/258
if (newCapacity == 0) {
throw new IllegalStateException("Maximum size of 2gb exceeded");
}
}
ChannelBuffer newBuffer = factory().getBuffer(order(), newCapacity);
newBuffer.writeBytes(buffer, 0, writerIndex());
buffer = newBuffer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment