Resolved issue: NETTY-258 (Make DynamicChannelBuffer.ensureWritableBytes() public)

* Added ChannelBuffer.ensureWritableBytes(int)
This commit is contained in:
Trustin Lee 2009-12-16 05:02:23 +00:00
parent 45d36f96b9
commit 47498eb159
4 changed files with 54 additions and 21 deletions

View File

@ -116,6 +116,12 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
readerIndex = 0;
}
public void ensureWritableBytes(int writableBytes) {
if (writableBytes > writableBytes()) {
throw new IndexOutOfBoundsException();
}
}
public short getUnsignedByte(int index) {
return (short) (getByte(index) & 0xFF);
}

View File

@ -413,6 +413,28 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
*/
void discardReadBytes();
/**
* Makes sure the number of {@linkplain #writableBytes() the writable bytes}
* is equal to or greater than the specified value. If there is enough
* writable bytes in this buffer, this method returns with no side effect.
* Otherwise:
* <ul>
* <li>a non-dynamic buffer will throw an {@link IndexOutOfBoundsException}.</li>
* <li>a dynamic buffer will expand its capacity so that the number of the
* {@link #writableBytes() writable bytes} becomes equal to or greater
* than the specified value. The expansion involves the reallocation of
* the internal buffer and consequently memory copy.</li>
* </ul>
*
* @param writableBytes
* the expected minimum number of writable bytes
* @throws IndexOutOfBoundsException
* if {@linkplain #writableBytes() the writable bytes} of this
* buffer is less than the specified value and the capacity of the
* buffer cannot be expanded
*/
void ensureWritableBytes(int writableBytes);
/**
* Gets a byte at the specified absolute {@code index} in this buffer.
* This method does not modify {@code readerIndex} or {@code writerIndex} of

View File

@ -64,6 +64,28 @@ public class DynamicChannelBuffer extends AbstractChannelBuffer {
buffer = factory.getBuffer(order(), estimatedLength);
}
@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;
}
ChannelBuffer newBuffer = factory().getBuffer(order(), newCapacity);
newBuffer.writeBytes(buffer, 0, writerIndex());
buffer = newBuffer;
}
public ChannelBufferFactory factory() {
return factory;
}
@ -271,25 +293,4 @@ public class DynamicChannelBuffer extends AbstractChannelBuffer {
public String toString(int index, int length, String charsetName) {
return buffer.toString(index, length, charsetName);
}
private void ensureWritableBytes(int requestedBytes) {
if (requestedBytes <= writableBytes()) {
return;
}
int newCapacity;
if (capacity() == 0) {
newCapacity = 1;
} else {
newCapacity = capacity();
}
int minNewCapacity = writerIndex() + requestedBytes;
while (newCapacity < minNewCapacity) {
newCapacity <<= 1;
}
ChannelBuffer newBuffer = factory().getBuffer(order(), newCapacity);
newBuffer.writeBytes(buffer, 0, writerIndex());
buffer = newBuffer;
}
}

View File

@ -95,6 +95,10 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
throw new UnreplayableOperationException();
}
public void ensureWritableBytes(int writableBytes) {
throw new UnreplayableOperationException();
}
public ChannelBuffer duplicate() {
throw new UnreplayableOperationException();
}