Introduced class ByteBufferPool.Lease to keep track of buffers with

associated information of whether they have been borrowed from the
ByteBufferPool or not.
This commit is contained in:
Simone Bordet 2014-06-06 16:22:42 +02:00
parent 9c13b300f0
commit 244158ee3e
3 changed files with 60 additions and 12 deletions

View File

@ -80,6 +80,7 @@ public class Generator
return result;
}
// TODO: rewrite this class in light of ByteBufferPool.Lease.
public static class Result implements Callback
{
private final List<Callback> callbacks = new ArrayList<>(2);

View File

@ -19,26 +19,20 @@
package org.eclipse.jetty.hpack;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.List;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.io.ByteBufferPool;
/* ------------------------------------------------------------ */
/**
*/
public class HpackEncoder
{
public HpackEncoder(ByteBufferPool pool)
private final ByteBufferPool byteBufferPool;
public HpackEncoder(ByteBufferPool byteBufferPool)
{
this.byteBufferPool = byteBufferPool;
}
public List<ByteBuffer> encode(HttpFields fields)
public ByteBufferPool.Lease encode(HttpFields fields)
{
return Collections.emptyList();
return new ByteBufferPool.Lease(byteBufferPool);
}
}

View File

@ -19,6 +19,8 @@
package org.eclipse.jetty.io;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
/**
* <p>A {@link ByteBuffer} pool.</p>
@ -48,4 +50,55 @@ public interface ByteBufferPool
* @see #acquire(int, boolean)
*/
public void release(ByteBuffer buffer);
public static class Lease
{
private final ByteBufferPool byteBufferPool;
private final List<ByteBuffer> buffers;
private final List<Boolean> recycles;
public Lease(ByteBufferPool byteBufferPool)
{
this.byteBufferPool = byteBufferPool;
this.buffers = new ArrayList<>();
this.recycles = new ArrayList<>();
}
public void add(ByteBuffer buffer, boolean recycle)
{
buffers.add(buffer);
recycles.add(recycle);
}
public List<ByteBuffer> getByteBuffers()
{
return buffers;
}
public Lease merge(Lease that)
{
assert byteBufferPool == that.byteBufferPool;
buffers.addAll(that.buffers);
recycles.addAll(that.recycles);
return this;
}
public long getTotalLength()
{
long length = 0;
for (int i = 0; i < buffers.size(); ++i)
length += buffers.get(i).remaining();
return length;
}
public void recycle()
{
for (int i = 0; i < buffers.size(); ++i)
{
ByteBuffer buffer = buffers.get(i);
if (recycles.get(i))
byteBufferPool.release(buffer);
}
}
}
}