Skip to content

Commit

Permalink
474068 - Update WebSocket Extension for permessage-deflate draft-22
Browse files Browse the repository at this point in the history
+ Copying inflated byte buffers
+ Simplifying Accumulator of buffer/chunks
+ Removing references to frame compression extensions
  • Loading branch information
joakime committed Jul 31, 2015
1 parent 2a40955 commit bf81ac9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

public class ByteAccumulator
{
private final List<Chunk> chunks = new ArrayList<>();
private final List<byte[]> chunks = new ArrayList<>();
private final int maxSize;
private int length = 0;

Expand All @@ -36,13 +36,17 @@ public ByteAccumulator(int maxOverallBufferSize)
this.maxSize = maxOverallBufferSize;
}

public void addChunk(byte buf[], int offset, int length)
public void copyChunk(byte buf[], int offset, int length)
{
if (this.length + length > maxSize)
{
throw new MessageTooLargeException("Frame is too large");
}
chunks.add(new Chunk(buf, offset, length));

byte copy[] = new byte[length - offset];
System.arraycopy(buf,offset,copy,0,length);

chunks.add(copy);
this.length += length;
}

Expand All @@ -54,26 +58,16 @@ public int getLength()
public void transferTo(ByteBuffer buffer)
{
if (buffer.remaining() < length)
throw new IllegalArgumentException();
int position = buffer.position();
for (Chunk chunk : chunks)
{
buffer.put(chunk.buffer, chunk.offset, chunk.length);
throw new IllegalArgumentException(String.format("Not enough space in ByteBuffer remaining [%d] for accumulated buffers length [%d]",
buffer.remaining(),length));
}
BufferUtil.flipToFlush(buffer, position);
}

private static class Chunk
{
private final byte[] buffer;
private final int offset;
private final int length;

private Chunk(byte[] buffer, int offset, int length)
int position = buffer.position();
for (byte[] chunk : chunks)
{
this.buffer = buffer;
this.offset = offset;
this.length = length;
buffer.put(chunk,0,chunk.length);
}
BufferUtil.flipToFlush(buffer,position);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ protected void decompress(ByteAccumulator accumulator, ByteBuffer buf)
{
return;
}
byte[] output = new byte[1024];
byte[] output = new byte[1024]; // TODO: make configurable size

if (inflater.needsInput() && !supplyInput(inflater, buf))
{
Expand All @@ -163,22 +163,7 @@ protected void decompress(ByteAccumulator accumulator, ByteBuffer buf)
if (read == 0)
{
LOG.debug("Decompress: read 0 {}",toDetail(inflater));
if (inflater.finished() || inflater.needsDictionary())
{
if (LOG.isDebugEnabled())
{
LOG.debug("Decompress: finished? {}",toDetail(inflater));
}
// We are finished ?
break;
}
else if (inflater.needsInput())
{
if (!supplyInput(inflater, buf))
{
break;
}
}
break;
}
else
{
Expand All @@ -187,7 +172,8 @@ else if (inflater.needsInput())
{
LOG.debug("Decompressed {} bytes: {}",read,toDetail(inflater));
}
accumulator.addChunk(output,0,read);

accumulator.copyChunk(output,0,read);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//

/**
* Jetty WebSocket Common : Frame &amp; Message Compression Extension Implementations
* Jetty WebSocket Common : Message Compression Extension Implementations
*/
package org.eclipse.jetty.websocket.common.extensions.compress;

0 comments on commit bf81ac9

Please sign in to comment.