26 |
26 |
27 import jdk.internal.net.http.hpack.HPACK.Logger; |
27 import jdk.internal.net.http.hpack.HPACK.Logger; |
28 |
28 |
29 import java.util.NoSuchElementException; |
29 import java.util.NoSuchElementException; |
30 |
30 |
|
31 import static jdk.internal.net.http.common.Utils.pow2Size; |
31 import static jdk.internal.net.http.hpack.HPACK.Logger.Level.EXTRA; |
32 import static jdk.internal.net.http.hpack.HPACK.Logger.Level.EXTRA; |
32 import static jdk.internal.net.http.hpack.HPACK.Logger.Level.NORMAL; |
33 import static jdk.internal.net.http.hpack.HPACK.Logger.Level.NORMAL; |
33 import static java.lang.String.format; |
34 import static java.lang.String.format; |
34 |
35 |
35 /* |
36 /* |
304 |
305 |
305 int tail, head, size, capacity; |
306 int tail, head, size, capacity; |
306 Object[] elements; |
307 Object[] elements; |
307 |
308 |
308 CircularBuffer(int capacity) { |
309 CircularBuffer(int capacity) { |
309 this.capacity = capacity; |
310 this.capacity = pow2Size(capacity); |
310 elements = new Object[capacity]; |
311 elements = new Object[this.capacity]; |
311 } |
312 } |
312 |
313 |
313 void add(E elem) { |
314 void add(E elem) { |
314 if (size == capacity) { |
315 if (size == capacity) { |
315 throw new IllegalStateException( |
316 throw new IllegalStateException( |
316 format("No room for '%s': capacity=%s", elem, capacity)); |
317 format("No room for '%s': capacity=%s", elem, capacity)); |
317 } |
318 } |
318 elements[head] = elem; |
319 elements[head] = elem; |
319 head = (head + 1) % capacity; |
320 head = (head + 1) & (capacity - 1); |
320 size++; |
321 size++; |
321 } |
322 } |
322 |
323 |
323 @SuppressWarnings("unchecked") |
324 @SuppressWarnings("unchecked") |
324 E remove() { |
325 E remove() { |
325 if (size == 0) { |
326 if (size == 0) { |
326 throw new NoSuchElementException("Empty"); |
327 throw new NoSuchElementException("Empty"); |
327 } |
328 } |
328 E elem = (E) elements[tail]; |
329 E elem = (E) elements[tail]; |
329 elements[tail] = null; |
330 elements[tail] = null; |
330 tail = (tail + 1) % capacity; |
331 tail = (tail + 1) & (capacity - 1); |
331 size--; |
332 size--; |
332 return elem; |
333 return elem; |
333 } |
334 } |
334 |
335 |
335 @SuppressWarnings("unchecked") |
336 @SuppressWarnings("unchecked") |
337 if (index < 0 || index >= size) { |
338 if (index < 0 || index >= size) { |
338 throw new IndexOutOfBoundsException( |
339 throw new IndexOutOfBoundsException( |
339 format("0 <= index <= capacity: index=%s, capacity=%s", |
340 format("0 <= index <= capacity: index=%s, capacity=%s", |
340 index, capacity)); |
341 index, capacity)); |
341 } |
342 } |
342 int idx = (tail + (size - index - 1)) % capacity; |
343 int idx = (tail + (size - index - 1)) & (capacity - 1); |
343 return (E) elements[idx]; |
344 return (E) elements[idx]; |
344 } |
345 } |
345 |
346 |
346 public void resize(int newCapacity) { |
347 public void resize(int newCapacity) { |
347 if (newCapacity < size) { |
348 if (newCapacity < size) { |
348 throw new IllegalStateException( |
349 throw new IllegalStateException( |
349 format("newCapacity >= size: newCapacity=%s, size=%s", |
350 format("newCapacity >= size: newCapacity=%s, size=%s", |
350 newCapacity, size)); |
351 newCapacity, size)); |
351 } |
352 } |
352 |
353 |
353 Object[] newElements = new Object[newCapacity]; |
354 int capacity = pow2Size(newCapacity); |
|
355 Object[] newElements = new Object[capacity]; |
354 |
356 |
355 if (tail < head || size == 0) { |
357 if (tail < head || size == 0) { |
356 System.arraycopy(elements, tail, newElements, 0, size); |
358 System.arraycopy(elements, tail, newElements, 0, size); |
357 } else { |
359 } else { |
358 System.arraycopy(elements, tail, newElements, 0, elements.length - tail); |
360 System.arraycopy(elements, tail, newElements, 0, elements.length - tail); |