From f4e4effe50dc82a9e3bc9ba514f94afb188de420 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Thu, 1 Mar 2012 11:47:51 +0100 Subject: [PATCH] DataInfo javadocs + modified slightly the API for helper methods, passing a boolean telling to read or consume the bytes. --- .../org/eclipse/jetty/spdy/api/DataInfo.java | 55 +++++++++++++++---- .../jetty/spdy/api/ClientUsageTest.java | 2 +- .../ServerHTTPSPDYAsyncConnectionFactory.java | 2 +- .../jetty/spdy/http/ServerHTTPSPDYTest.java | 18 +++--- .../org/eclipse/jetty/spdy/SynReplyTest.java | 8 +-- 5 files changed, 58 insertions(+), 27 deletions(-) diff --git a/spdy-core/src/main/java/org/eclipse/jetty/spdy/api/DataInfo.java b/spdy-core/src/main/java/org/eclipse/jetty/spdy/api/DataInfo.java index 15529275c48..85941291e55 100644 --- a/spdy-core/src/main/java/org/eclipse/jetty/spdy/api/DataInfo.java +++ b/spdy-core/src/main/java/org/eclipse/jetty/spdy/api/DataInfo.java @@ -28,6 +28,16 @@ import java.util.concurrent.atomic.AtomicInteger; * type, via {@link Stream#data(DataInfo)}. The last instance must have the * {@link #isClose() close flag} set, so that the client knows that no more content is * expected.

+ *

Receivers of {@link DataInfo} via {@link StreamFrameListener#onData(Stream, DataInfo)} + * have two different APIs to read the data content bytes: a {@link #readInto(ByteBuffer) read} + * API that does not interact with flow control, and a {@link #drainInto(ByteBuffer) drain} + * API that interacts with flow control.

+ *

Flow control is defined so that when the sender wants to sends a number of bytes larger + * than the {@link Settings.ID#INITIAL_WINDOW_SIZE} value, it will stop sending as soon as it + * has sent a number of bytes equal to the window size. The receiver has to consume + * the data bytes that it received in order to tell the sender to send more bytes.

+ *

Consuming the data bytes can be done only via {@link #drainInto(ByteBuffer)} or by a combination + * of {@link #readInto(ByteBuffer)} and {@link #consume(int)} (possibly at different times).

*/ public abstract class DataInfo { @@ -139,15 +149,23 @@ public abstract class DataInfo /** *

Copies the content bytes of this {@link DataInfo} into the given {@link ByteBuffer}.

*

If the given {@link ByteBuffer} cannot contain the whole content of this {@link DataInfo} - * then {@link #available()} will return a positive value, and further content - * may be retrieved by invoking again this method.

+ * then after the read {@link #available()} will return a positive value, and further content + * may be retrieved by invoking again this method with a new output buffer.

* * @param output the {@link ByteBuffer} to copy to bytes into * @return the number of bytes copied * @see #available() + * @see #drainInto(ByteBuffer) */ public abstract int readInto(ByteBuffer output); + /** + *

Reads and consumes the content bytes of this {@link DataInfo} into the given {@link ByteBuffer}.

+ * + * @param output the {@link ByteBuffer} to copy to bytes into + * @return the number of bytes copied + * @see #consume(int) + */ public int drainInto(ByteBuffer output) { int read = readInto(output); @@ -155,8 +173,15 @@ public abstract class DataInfo return read; } + /** + *

Consumes the given number of bytes from this {@link DataInfo}.

+ * + * @param delta the number of bytes consumed + */ public void consume(int delta) { + if (delta < 0) + throw new IllegalArgumentException(); int read = length() - available(); int newConsumed = consumed() + delta; if (newConsumed > read) @@ -164,31 +189,33 @@ public abstract class DataInfo consumed.addAndGet(delta); } + /** + * @return the number of bytes consumed + */ public int consumed() { return consumed.get(); } /** + * * @param charset the charset used to convert the bytes + * @param consume whether to consume the content * @return a String with the content of this {@link DataInfo} */ - public String asString(String charset) + public String asString(String charset, boolean consume) { - ByteBuffer buffer = ByteBuffer.allocate(available()); - readInto(buffer); - buffer.flip(); + ByteBuffer buffer = asByteBuffer(consume); return Charset.forName(charset).decode(buffer).toString(); } /** * @return a byte array with the content of this {@link DataInfo} + * @param consume whether to consume the content */ - public byte[] asBytes() + public byte[] asBytes(boolean consume) { - ByteBuffer buffer = ByteBuffer.allocate(available()); - readInto(buffer); - buffer.flip(); + ByteBuffer buffer = asByteBuffer(consume); byte[] result = new byte[buffer.remaining()]; buffer.get(result); return result; @@ -196,11 +223,15 @@ public abstract class DataInfo /** * @return a {@link ByteBuffer} with the content of this {@link DataInfo} + * @param consume whether to consume the content */ - public ByteBuffer asByteBuffer() + public ByteBuffer asByteBuffer(boolean consume) { ByteBuffer buffer = ByteBuffer.allocate(available()); - readInto(buffer); + if (consume) + drainInto(buffer); + else + readInto(buffer); buffer.flip(); return buffer; } diff --git a/spdy-core/src/test/java/org/eclipse/jetty/spdy/api/ClientUsageTest.java b/spdy-core/src/test/java/org/eclipse/jetty/spdy/api/ClientUsageTest.java index 00dd2b59f02..2183cdf8c38 100644 --- a/spdy-core/src/test/java/org/eclipse/jetty/spdy/api/ClientUsageTest.java +++ b/spdy-core/src/test/java/org/eclipse/jetty/spdy/api/ClientUsageTest.java @@ -130,7 +130,7 @@ public class ClientUsageTest public void onData(Stream stream, DataInfo dataInfo) { StringBuilder builder = (StringBuilder)stream.getAttribute("builder"); - builder.append(dataInfo.asString("UTF-8")); + builder.append(dataInfo.asString("UTF-8", true)); if (dataInfo.isClose()) { int receivedLength = builder.toString().getBytes(Charset.forName("UTF-8")).length; diff --git a/spdy-jetty-http/src/main/java/org/eclipse/jetty/spdy/http/ServerHTTPSPDYAsyncConnectionFactory.java b/spdy-jetty-http/src/main/java/org/eclipse/jetty/spdy/http/ServerHTTPSPDYAsyncConnectionFactory.java index 38029013461..a61f77bab35 100644 --- a/spdy-jetty-http/src/main/java/org/eclipse/jetty/spdy/http/ServerHTTPSPDYAsyncConnectionFactory.java +++ b/spdy-jetty-http/src/main/java/org/eclipse/jetty/spdy/http/ServerHTTPSPDYAsyncConnectionFactory.java @@ -159,7 +159,7 @@ public class ServerHTTPSPDYAsyncConnectionFactory extends ServerSPDYAsyncConnect logger.debug("Received {} on {}", dataInfo, stream); final ServerHTTPSPDYAsyncConnection connection = (ServerHTTPSPDYAsyncConnection)stream.getAttribute("connection"); - final ByteBuffer buffer = dataInfo.asByteBuffer(); + final ByteBuffer buffer = dataInfo.asByteBuffer(true); final boolean isClose = dataInfo.isClose(); connection.post(new Runnable() diff --git a/spdy-jetty-http/src/test/java/org/eclipse/jetty/spdy/http/ServerHTTPSPDYTest.java b/spdy-jetty-http/src/test/java/org/eclipse/jetty/spdy/http/ServerHTTPSPDYTest.java index 3b358a5e9ac..64b1aacf299 100644 --- a/spdy-jetty-http/src/test/java/org/eclipse/jetty/spdy/http/ServerHTTPSPDYTest.java +++ b/spdy-jetty-http/src/test/java/org/eclipse/jetty/spdy/http/ServerHTTPSPDYTest.java @@ -349,7 +349,7 @@ public class ServerHTTPSPDYTest extends AbstractHTTPSPDYTest public void onData(Stream stream, DataInfo dataInfo) { Assert.assertTrue(dataInfo.isClose()); - Assert.assertEquals(data, dataInfo.asString("UTF-8")); + Assert.assertEquals(data, dataInfo.asString("UTF-8", true)); dataLatch.countDown(); } }); @@ -399,7 +399,7 @@ public class ServerHTTPSPDYTest extends AbstractHTTPSPDYTest public void onData(Stream stream, DataInfo dataInfo) { Assert.assertTrue(dataInfo.isClose()); - byte[] bytes = dataInfo.asBytes(); + byte[] bytes = dataInfo.asBytes(true); Assert.assertEquals(1, bytes.length); Assert.assertEquals(data, bytes[0]); dataLatch.countDown(); @@ -460,9 +460,9 @@ public class ServerHTTPSPDYTest extends AbstractHTTPSPDYTest int data = dataFrames.incrementAndGet(); Assert.assertTrue(data >= 1 && data <= 2); if (data == 1) - Assert.assertEquals(data1, dataInfo.asString("UTF8")); + Assert.assertEquals(data1, dataInfo.asString("UTF8", true)); else - Assert.assertEquals(data2, dataInfo.asString("UTF8")); + Assert.assertEquals(data2, dataInfo.asString("UTF8", true)); dataLatch.countDown(); } }); @@ -628,7 +628,7 @@ public class ServerHTTPSPDYTest extends AbstractHTTPSPDYTest @Override public void onData(Stream stream, DataInfo dataInfo) { - ByteBuffer byteBuffer = dataInfo.asByteBuffer(); + ByteBuffer byteBuffer = dataInfo.asByteBuffer(true); while (byteBuffer.hasRemaining()) buffer.write(byteBuffer.get()); if (dataInfo.isClose()) @@ -690,7 +690,7 @@ public class ServerHTTPSPDYTest extends AbstractHTTPSPDYTest @Override public void onData(Stream stream, DataInfo dataInfo) { - ByteBuffer byteBuffer = dataInfo.asByteBuffer(); + ByteBuffer byteBuffer = dataInfo.asByteBuffer(true); while (byteBuffer.hasRemaining()) buffer.write(byteBuffer.get()); if (dataInfo.isClose()) @@ -887,12 +887,12 @@ public class ServerHTTPSPDYTest extends AbstractHTTPSPDYTest if (count == 1) { Assert.assertFalse(dataInfo.isClose()); - Assert.assertEquals(pangram1, dataInfo.asString("UTF-8")); + Assert.assertEquals(pangram1, dataInfo.asString("UTF-8", true)); } else if (count == 2) { Assert.assertTrue(dataInfo.isClose()); - Assert.assertEquals(pangram2, dataInfo.asString("UTF-8")); + Assert.assertEquals(pangram2, dataInfo.asString("UTF-8", true)); } dataLatch.countDown(); } @@ -949,7 +949,7 @@ public class ServerHTTPSPDYTest extends AbstractHTTPSPDYTest { Assert.assertEquals(1, dataFrames.incrementAndGet()); Assert.assertTrue(dataInfo.isClose()); - Assert.assertArrayEquals(data, dataInfo.asBytes()); + Assert.assertArrayEquals(data, dataInfo.asBytes(true)); dataLatch.countDown(); } }); diff --git a/spdy-jetty/src/test/java/org/eclipse/jetty/spdy/SynReplyTest.java b/spdy-jetty/src/test/java/org/eclipse/jetty/spdy/SynReplyTest.java index 5849f07cbcb..c48e0fde53e 100644 --- a/spdy-jetty/src/test/java/org/eclipse/jetty/spdy/SynReplyTest.java +++ b/spdy-jetty/src/test/java/org/eclipse/jetty/spdy/SynReplyTest.java @@ -232,13 +232,13 @@ public class SynReplyTest extends AbstractTest int dataCount = this.dataCount.incrementAndGet(); if (dataCount == 1) { - String chunk1 = dataInfo.asString("UTF-8"); + String chunk1 = dataInfo.asString("UTF-8", true); Assert.assertEquals(data1, chunk1); dataLatch1.countDown(); } else if (dataCount == 2) { - String chunk2 = dataInfo.asString("UTF-8"); + String chunk2 = dataInfo.asString("UTF-8", true); Assert.assertEquals(data2, chunk2); dataLatch2.countDown(); } @@ -274,7 +274,7 @@ public class SynReplyTest extends AbstractTest @Override public void onData(Stream stream, DataInfo dataInfo) { - String data = dataInfo.asString("UTF-8"); + String data = dataInfo.asString("UTF-8", true); Assert.assertEquals(clientData, data); clientDataLatch.countDown(); } @@ -393,7 +393,7 @@ public class SynReplyTest extends AbstractTest @Override public void onData(Stream stream, DataInfo dataInfo) { - String chunk = dataInfo.asString("UTF-8"); + String chunk = dataInfo.asString("UTF-8", true); Assert.assertEquals(data, chunk); dataLatch.countDown(); }