You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ck...@apache.org on 2020/09/14 15:16:08 UTC
[httpcomponents-core] 09/13: HTTPCORE-649: Implement
ByteArrayBuffer.append(ByteBuffer)
This is an automated email from the ASF dual-hosted git repository.
ckozak pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git
commit ac5754bedc16c9331771dfbfa641842910d600e9
Author: Carter Kozak <ck...@apache.org>
AuthorDate: Mon Sep 14 11:14:48 2020 -0400
HTTPCORE-649: Implement ByteArrayBuffer.append(ByteBuffer)
---
.../apache/hc/core5/http2/hpack/HPackDecoder.java | 18 ++---
.../apache/hc/core5/http2/hpack/HPackEncoder.java | 11 +---
.../http/nio/entity/BasicAsyncEntityConsumer.java | 11 +---
.../org/apache/hc/core5/util/ByteArrayBuffer.java | 16 +++++
.../entity/TestAbstractBinAsyncEntityConsumer.java | 11 +---
.../apache/hc/core5/util/TestByteArrayBuffer.java | 76 ++++++++++++++++++++++
6 files changed, 101 insertions(+), 42 deletions(-)
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/HPackDecoder.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/HPackDecoder.java
index dd9379c..1df29a2 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/HPackDecoder.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/HPackDecoder.java
@@ -125,21 +125,15 @@ public final class HPackDecoder {
}
static void decodePlainString(final ByteArrayBuffer buffer, final ByteBuffer src) throws HPackException {
-
final int strLen = decodeInt(src, 7);
- if (strLen > src.remaining()) {
+ final int remaining = src.remaining();
+ if (strLen > remaining) {
throw new HPackException(UNEXPECTED_EOS);
}
- if (src.hasArray()) {
- final byte[] b = src.array();
- final int off = src.position();
- buffer.append(b, src.arrayOffset() + off, strLen);
- src.position(off + strLen);
- } else {
- for (int i = 0; i < strLen; i++) {
- buffer.append(src.get());
- }
- }
+ final int originalLimit = src.limit();
+ src.limit(originalLimit - (remaining - strLen));
+ buffer.append(src);
+ src.limit(originalLimit);
}
static void decodeHuffman(final ByteArrayBuffer buffer, final ByteBuffer src) throws HPackException {
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/HPackEncoder.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/HPackEncoder.java
index 2824760..621bdf4 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/HPackEncoder.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/HPackEncoder.java
@@ -110,16 +110,7 @@ public final class HPackEncoder {
} else {
dst.ensureCapacity(strLen + 8);
encodeInt(dst, 7, strLen, 0x0);
- if (src.hasArray()) {
- final byte[] b = src.array();
- final int off = src.position();
- dst.append(b, src.arrayOffset() + off, strLen);
- src.position(off + strLen);
- } else {
- while (src.hasRemaining()) {
- dst.append(src.get());
- }
- }
+ dst.append(src);
}
}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/BasicAsyncEntityConsumer.java b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/BasicAsyncEntityConsumer.java
index 26487ec..648bde6 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/BasicAsyncEntityConsumer.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/BasicAsyncEntityConsumer.java
@@ -60,16 +60,7 @@ public class BasicAsyncEntityConsumer extends AbstractBinAsyncEntityConsumer<byt
@Override
protected void data(final ByteBuffer src, final boolean endOfStream) throws IOException {
- if (src == null) {
- return;
- }
- if (src.hasArray()) {
- buffer.append(src.array(), src.arrayOffset() + src.position(), src.remaining());
- } else {
- while (src.hasRemaining()) {
- buffer.append(src.get());
- }
- }
+ buffer.append(src);
}
@Override
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/util/ByteArrayBuffer.java b/httpcore5/src/main/java/org/apache/hc/core5/util/ByteArrayBuffer.java
index 573faa5..c9479c3 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/util/ByteArrayBuffer.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/util/ByteArrayBuffer.java
@@ -28,6 +28,7 @@
package org.apache.hc.core5.util;
import java.io.Serializable;
+import java.nio.ByteBuffer;
/**
* A resizable byte array.
@@ -171,6 +172,21 @@ public final class ByteArrayBuffer implements Serializable {
append(b.array(), off, len);
}
+ public void append(final ByteBuffer buffer) {
+ if (buffer == null) {
+ return;
+ }
+ final int bufferLength = buffer.remaining();
+ if (bufferLength > 0) {
+ final int newLength = this.len + bufferLength;
+ if (newLength > this.array.length) {
+ expand(newLength);
+ }
+ buffer.get(this.array, this.len, bufferLength);
+ this.len = newLength;
+ }
+ }
+
/**
* Clears content of the buffer. The underlying byte array is not resized.
*/
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestAbstractBinAsyncEntityConsumer.java b/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestAbstractBinAsyncEntityConsumer.java
index 7c7b12a..ea7609e 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestAbstractBinAsyncEntityConsumer.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestAbstractBinAsyncEntityConsumer.java
@@ -62,16 +62,7 @@ public class TestAbstractBinAsyncEntityConsumer {
@Override
protected void data(final ByteBuffer src, final boolean endOfStream) throws IOException {
- if (src == null) {
- return;
- }
- if (src.hasArray()) {
- buffer.append(src.array(), src.arrayOffset() + src.position(), src.remaining());
- } else {
- while (src.hasRemaining()) {
- buffer.append(src.get());
- }
- }
+ buffer.append(src);
}
@Override
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/util/TestByteArrayBuffer.java b/httpcore5/src/test/java/org/apache/hc/core5/util/TestByteArrayBuffer.java
index 204426a..6d8d6f5 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/util/TestByteArrayBuffer.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/util/TestByteArrayBuffer.java
@@ -31,6 +31,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
+import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import org.junit.Assert;
@@ -109,6 +110,73 @@ public class TestByteArrayBuffer {
}
@Test
+ public void testAppendHeapByteBuffer() {
+ final ByteArrayBuffer buffer = new ByteArrayBuffer(4);
+ Assert.assertEquals(4, buffer.capacity());
+
+ final ByteBuffer tmp = ByteBuffer.wrap(new byte[] { 1, 2, 3, 4, 5, 6});
+ buffer.append(tmp);
+
+ Assert.assertFalse("The input buffer should be drained", tmp.hasRemaining());
+ Assert.assertEquals(8, buffer.capacity());
+ Assert.assertEquals(6, buffer.length());
+
+ tmp.clear();
+ buffer.append(tmp);
+
+ Assert.assertFalse("The input buffer should be drained", tmp.hasRemaining());
+ Assert.assertEquals(16, buffer.capacity());
+ Assert.assertEquals(12, buffer.length());
+ Assert.assertArrayEquals(new byte[] {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6}, buffer.toByteArray());
+ }
+
+ @Test
+ public void testAppendHeapByteBufferWithOffset() {
+ final ByteArrayBuffer buffer = new ByteArrayBuffer(4);
+ Assert.assertEquals(4, buffer.capacity());
+
+ final ByteBuffer tmp = ByteBuffer.wrap(new byte[] { 7, 7, 1, 2, 3, 4, 5, 6, 7, 7}, 2, 6).slice();
+ Assert.assertTrue("Validate this is testing a buffer with an array offset", tmp.arrayOffset() > 0);
+
+ buffer.append(tmp);
+
+ Assert.assertFalse("The input buffer should be drained", tmp.hasRemaining());
+ Assert.assertEquals(8, buffer.capacity());
+ Assert.assertEquals(6, buffer.length());
+
+ tmp.clear();
+ Assert.assertEquals(6, tmp.remaining());
+ buffer.append(tmp);
+
+ Assert.assertFalse("The input buffer should be drained", tmp.hasRemaining());
+ Assert.assertEquals(16, buffer.capacity());
+ Assert.assertEquals(12, buffer.length());
+ Assert.assertArrayEquals(new byte[] {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6}, buffer.toByteArray());
+ }
+
+ @Test
+ public void testAppendDirectByteBuffer() {
+ final ByteArrayBuffer buffer = new ByteArrayBuffer(4);
+ Assert.assertEquals(4, buffer.capacity());
+
+ final ByteBuffer tmp = ByteBuffer.allocateDirect(6);
+ tmp.put(new byte[] { 1, 2, 3, 4, 5, 6}).flip();
+ buffer.append(tmp);
+
+ Assert.assertFalse("The input buffer should be drained", tmp.hasRemaining());
+ Assert.assertEquals(8, buffer.capacity());
+ Assert.assertEquals(6, buffer.length());
+
+ tmp.clear();
+ buffer.append(tmp);
+
+ Assert.assertFalse("The input buffer should be drained", tmp.hasRemaining());
+ Assert.assertEquals(16, buffer.capacity());
+ Assert.assertEquals(12, buffer.length());
+ Assert.assertArrayEquals(new byte[] {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6}, buffer.toByteArray());
+ }
+
+ @Test
public void testInvalidAppend() throws Exception {
final ByteArrayBuffer buffer = new ByteArrayBuffer(4);
buffer.append((byte[])null, 0, 0);
@@ -251,6 +319,14 @@ public class TestByteArrayBuffer {
}
@Test
+ public void testAppendNullByteBuffer() throws Exception {
+ final ByteArrayBuffer buffer = new ByteArrayBuffer(8);
+ final ByteBuffer nullBuffer = null;
+ buffer.append(nullBuffer);
+ Assert.assertEquals(0, buffer.length());
+ }
+
+ @Test
public void testInvalidAppendCharArrayAsAscii() throws Exception {
final ByteArrayBuffer buffer = new ByteArrayBuffer(4);
buffer.append((char[])null, 0, 0);