1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.core5.http2.hpack;
29
30 import org.apache.hc.core5.http.Header;
31
32 final class FifoBuffer {
33
34 private HPackHeader[] array;
35 private int head;
36 private int tail;
37
38 FifoBuffer(final int initialCapacity) {
39 this.array = new HPackHeader[initialCapacity];
40 this.head = 0;
41 this.tail = 0;
42 }
43
44 private void expand() {
45
46 int newcapacity = (array.length + 1) << 1;
47 if (newcapacity < 0) {
48 newcapacity = Integer.MAX_VALUE;
49 }
50 final Header[] oldArray = array;
51 final int len = oldArray.length;
52 final HPackHeader/HPackHeader.html#HPackHeader">HPackHeader[] newArray = new HPackHeader[newcapacity];
53 System.arraycopy(oldArray, head, newArray, 0, len - head);
54 System.arraycopy(oldArray, 0, newArray, len - head, head);
55 array = newArray;
56 head = len;
57 tail = 0;
58 }
59
60 public void clear() {
61 head = 0;
62 tail = 0;
63 }
64
65 public void addFirst(final HPackHeader header) {
66 array[head++] = header;
67 if (head == array.length) {
68 head = 0;
69 }
70 if (head == tail) {
71 expand();
72 }
73 }
74
75 public HPackHeader get(final int index) {
76 int i = head - index - 1;
77 if (i < 0) {
78 i = array.length + i;
79 }
80 return array[i];
81 }
82
83 public HPackHeader getFirst() {
84 return array[head > 0 ? head - 1 : array.length - 1];
85 }
86
87 public HPackHeader getLast() {
88 return array[tail];
89 }
90
91 public HPackHeader removeLast() {
92 final HPackHeader header = array[tail];
93 if (header != null) {
94 array[tail++] = null;
95 if (tail == array.length) {
96 tail = 0;
97 }
98 }
99 return header;
100 }
101
102 public int capacity() {
103 return array.length;
104 }
105
106 public int size() {
107 int i = head - tail;
108 if (i < 0) {
109 i = array.length + i;
110 }
111 return i;
112 }
113
114 }