From 244158ee3e5571da831b1fbca5cbd6676ef85389 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Fri, 6 Jun 2014 16:22:42 +0200 Subject: [PATCH] Introduced class ByteBufferPool.Lease to keep track of buffers with associated information of whether they have been borrowed from the ByteBufferPool or not. --- .../jetty/fcgi/generator/Generator.java | 1 + .../org/eclipse/jetty/hpack/HpackEncoder.java | 18 +++---- .../org/eclipse/jetty/io/ByteBufferPool.java | 53 +++++++++++++++++++ 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/jetty-fcgi/fcgi-client/src/main/java/org/eclipse/jetty/fcgi/generator/Generator.java b/jetty-fcgi/fcgi-client/src/main/java/org/eclipse/jetty/fcgi/generator/Generator.java index 1fbf3d85ff4..04e6c0aa70b 100644 --- a/jetty-fcgi/fcgi-client/src/main/java/org/eclipse/jetty/fcgi/generator/Generator.java +++ b/jetty-fcgi/fcgi-client/src/main/java/org/eclipse/jetty/fcgi/generator/Generator.java @@ -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 callbacks = new ArrayList<>(2); diff --git a/jetty-hpack/src/main/java/org/eclipse/jetty/hpack/HpackEncoder.java b/jetty-hpack/src/main/java/org/eclipse/jetty/hpack/HpackEncoder.java index e8a75171388..34edb32281e 100644 --- a/jetty-hpack/src/main/java/org/eclipse/jetty/hpack/HpackEncoder.java +++ b/jetty-hpack/src/main/java/org/eclipse/jetty/hpack/HpackEncoder.java @@ -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 encode(HttpFields fields) + public ByteBufferPool.Lease encode(HttpFields fields) { - return Collections.emptyList(); + return new ByteBufferPool.Lease(byteBufferPool); } } diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferPool.java b/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferPool.java index 302adde35d8..f0153dc2f7a 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferPool.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferPool.java @@ -19,6 +19,8 @@ package org.eclipse.jetty.io; import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; /** *

A {@link ByteBuffer} pool.

@@ -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 buffers; + private final List 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 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); + } + } + } }