Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make sure CompositeChanneBuffer does not throw a UnsupportedOperation…

…Exception if discardReadBytes() discard the whole content of the buffer. See #325
  • Loading branch information...
commit d8cb20499f40f10953f8084aa2a7c63e5ed866e3 1 parent 67fd293
@normanmaurer normanmaurer authored
View
9 src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java
@@ -631,7 +631,14 @@ public void discardReadBytes() {
final int bytesToMove = capacity() - localReaderIndex;
List<ChannelBuffer> list = decompose(localReaderIndex, bytesToMove);
-
+
+ // If the list is empty we need to assign a new one because
+ // we get a List that is immutable.
+ //
+ // See https://github.com/netty/netty/issues/325
+ if (list.isEmpty()) {
+ list = new ArrayList<ChannelBuffer>();
+ }
// Add a new buffer so that the capacity of this composite buffer does
// not decrease due to the discarded components.
// XXX Might create too many components if discarded by small amount.
View
8 src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java
@@ -1704,4 +1704,12 @@ public void testHashCode() {
assertFalse(set.contains(elemB));
assertEquals(0, set.size());
}
+
+ // Test case for https://github.com/netty/netty/issues/325
+ @Test
+ public void testDiscardAllReadBytes() {
+ buffer.writerIndex(buffer.capacity());
+ buffer.readerIndex(buffer.writerIndex());
+ buffer.discardReadBytes();
+ }
}

2 comments on commit d8cb204

@fredericBregier
Collaborator

Maybe just changing the decompose method in line 54 instead (or both) ?
from
return Collections.emptyList();
to
return new ArrayList(1);

Note (1) since in the next method, one element will be added, while () => 10 elements preallocated.

@normanmaurer
Collaborator

@fredericBregier I only changed it in discardReadBytes() because using Collectons.emptyList() will give us some optimization if we only need a read-only representation (like in slice(..)).

But using ArrayList(1) makes sense

Please sign in to comment.
Something went wrong with that request. Please try again.