--- a/src/java.net.http/share/classes/jdk/internal/net/http/hpack/SimpleHeaderTable.java Wed Apr 18 12:03:06 2018 +0100
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/hpack/SimpleHeaderTable.java Wed Apr 18 12:55:11 2018 +0100
@@ -28,6 +28,7 @@
import java.util.NoSuchElementException;
+import static jdk.internal.net.http.common.Utils.pow2Size;
import static jdk.internal.net.http.hpack.HPACK.Logger.Level.EXTRA;
import static jdk.internal.net.http.hpack.HPACK.Logger.Level.NORMAL;
import static java.lang.String.format;
@@ -306,8 +307,8 @@
Object[] elements;
CircularBuffer(int capacity) {
- this.capacity = capacity;
- elements = new Object[capacity];
+ this.capacity = pow2Size(capacity);
+ elements = new Object[this.capacity];
}
void add(E elem) {
@@ -316,7 +317,7 @@
format("No room for '%s': capacity=%s", elem, capacity));
}
elements[head] = elem;
- head = (head + 1) % capacity;
+ head = (head + 1) & (capacity - 1);
size++;
}
@@ -327,7 +328,7 @@
}
E elem = (E) elements[tail];
elements[tail] = null;
- tail = (tail + 1) % capacity;
+ tail = (tail + 1) & (capacity - 1);
size--;
return elem;
}
@@ -339,7 +340,7 @@
format("0 <= index <= capacity: index=%s, capacity=%s",
index, capacity));
}
- int idx = (tail + (size - index - 1)) % capacity;
+ int idx = (tail + (size - index - 1)) & (capacity - 1);
return (E) elements[idx];
}
@@ -350,7 +351,8 @@
newCapacity, size));
}
- Object[] newElements = new Object[newCapacity];
+ int capacity = pow2Size(newCapacity);
+ Object[] newElements = new Object[capacity];
if (tail < head || size == 0) {
System.arraycopy(elements, tail, newElements, 0, size);
@@ -362,7 +364,7 @@
elements = newElements;
tail = 0;
head = size;
- this.capacity = newCapacity;
+ this.capacity = capacity;
}
}
}
--- a/test/jdk/java/net/httpclient/http2/java.net.http/jdk/internal/net/http/hpack/CircularBufferTest.java Wed Apr 18 12:03:06 2018 +0100
+++ b/test/jdk/java/net/httpclient/http2/java.net.http/jdk/internal/net/http/hpack/CircularBufferTest.java Wed Apr 18 12:55:11 2018 +0100
@@ -25,13 +25,16 @@
import org.testng.annotations.Test;
import jdk.internal.net.http.hpack.SimpleHeaderTable.CircularBuffer;
+import java.util.Arrays;
import java.util.Queue;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
+import static jdk.internal.net.http.common.Utils.pow2Size;
import static org.testng.Assert.assertEquals;
import static jdk.internal.net.http.hpack.TestHelper.assertVoidThrows;
import static jdk.internal.net.http.hpack.TestHelper.newRandom;
+import static org.testng.Assert.assertTrue;
public final class CircularBufferTest {
@@ -80,6 +83,8 @@
private void resizeOnce(int capacity) {
+ capacity = pow2Size(capacity);
+
int nextNumberToPut = 0;
Queue<Integer> referenceQueue = new ArrayBlockingQueue<>(capacity);
@@ -104,11 +109,15 @@
Integer[] expected = referenceQueue.toArray(new Integer[0]);
buffer.resize(expected.length);
- assertEquals(buffer.elements, expected);
+ boolean equals = Arrays.equals(buffer.elements, 0, buffer.size,
+ expected, 0, expected.length);
+ assertTrue(equals);
}
private void queueOnce(int capacity, int numWraps) {
+ capacity = pow2Size(capacity);
+
Queue<Integer> referenceQueue = new ArrayBlockingQueue<>(capacity);
CircularBuffer<Integer> buffer = new CircularBuffer<>(capacity);