Skip to content

Instantly share code, notes, and snippets.

@gregw
Created November 3, 2018 16:54
Show Gist options
  • Save gregw/61f923f825963addd0aaf036bb9ea8d7 to your computer and use it in GitHub Desktop.
Save gregw/61f923f825963addd0aaf036bb9ea8d7 to your computer and use it in GitHub Desktop.
hpack works with direct buffers
diff --git a/jetty-http2/http2-hpack/src/main/java/org/eclipse/jetty/http2/hpack/Huffman.java b/jetty-http2/http2-hpack/src/main/java/org/eclipse/jetty/http2/hpack/Huffman.java
index ddb2c4a97d..438a5a7267 100644
--- a/jetty-http2/http2-hpack/src/main/java/org/eclipse/jetty/http2/hpack/Huffman.java
+++ b/jetty-http2/http2-hpack/src/main/java/org/eclipse/jetty/http2/hpack/Huffman.java
@@ -357,16 +357,16 @@ public class Huffman
int current = 0;
int bits = 0;
- byte[] array = buffer.array();
+ byte[] array = buffer.hasArray()?buffer.array():null;
int position=buffer.position();
- int start=buffer.arrayOffset()+position;
+ int start=(buffer.hasArray()?buffer.arrayOffset():0)+position;
int end=start+length;
buffer.position(position+length);
for (int i=start; i<end; i++)
{
- int b = array[i]&0xFF;
+ int b = (array==null?buffer.get(i):array[i])&0xFF;
current = (current << 8) | b;
bits += 8;
while (bits >= 8)
@@ -461,8 +461,8 @@ public class Huffman
long current = 0;
int n = 0;
- byte[] array = buffer.array();
- int p=buffer.arrayOffset()+buffer.position();
+ byte[] array = buffer.hasArray()?buffer.array():null;
+ int p=(buffer.hasArray()?buffer.arrayOffset():0)+buffer.position();
int len = s.length();
for (int i=0;i<len;i++)
@@ -480,18 +480,24 @@ public class Huffman
while (n >= 8)
{
n -= 8;
- array[p++]=(byte)(current >> n);
+ if (array==null)
+ buffer.put(p++,(byte)(current >> n));
+ else
+ array[p++]=(byte)(current >> n);
}
}
- if (n > 0)
+ if (n > 0)
{
- current <<= (8 - n);
- current |= (0xFF >>> n);
- array[p++]=(byte)current;
+ current <<= (8 - n);
+ current |= (0xFF >>> n);
+ if (array==null)
+ buffer.put(p++,(byte)(current));
+ else
+ array[p++]=(byte)(current);
}
-
- buffer.position(p-buffer.arrayOffset());
+
+ buffer.position(p-(buffer.hasArray()?buffer.arrayOffset():0));
}
}
diff --git a/jetty-http2/http2-hpack/src/test/java/org/eclipse/jetty/http2/hpack/HpackTest.java b/jetty-http2/http2-hpack/src/test/java/org/eclipse/jetty/http2/hpack/HpackTest.java
index 355ffb2953..29a99dca5a 100644
--- a/jetty-http2/http2-hpack/src/test/java/org/eclipse/jetty/http2/hpack/HpackTest.java
+++ b/jetty-http2/http2-hpack/src/test/java/org/eclipse/jetty/http2/hpack/HpackTest.java
@@ -51,7 +51,7 @@ public class HpackTest
{
HpackEncoder encoder = new HpackEncoder();
HpackDecoder decoder = new HpackDecoder(4096,8192);
- ByteBuffer buffer = BufferUtil.allocate(16*1024);
+ ByteBuffer buffer = BufferUtil.allocateDirect(16*1024);
HttpFields fields0 = new HttpFields();
fields0.add(HttpHeader.CONTENT_TYPE,"text/html");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment