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 import org.apache.hc.core5.util.Asserts;
32
33 final class InboundDynamicTable {
34
35 private final StaticTable staticTable;
36 private final FifoBuffer headers;
37
38 private int maxSize;
39 private int currentSize;
40
41 InboundDynamicTable(final StaticTable staticTable) {
42 this.staticTable = staticTable;
43 this.headers = new FifoBuffer(256);
44 this.maxSize = Integer.MAX_VALUE;
45 this.currentSize = 0;
46 }
47
48 InboundDynamicTable() {
49 this(StaticTable.INSTANCE);
50 }
51
52 public int getMaxSize() {
53 return maxSize;
54 }
55
56 public void setMaxSize(final int maxSize) {
57 this.maxSize = maxSize;
58 evict();
59 }
60
61 public int getCurrentSize() {
62 return currentSize;
63 }
64
65 int staticLength() {
66 return staticTable.length();
67 }
68
69 int dynamicLength() {
70 return headers.size();
71 }
72
73 Header getDynamicEntry(final int index) {
74 return headers.get(index);
75 }
76
77 public int length() {
78 return staticTable.length() + headers.size();
79 }
80
81 public HPackHeader getHeader(final int index) {
82 if (index < 1 || index > length()) {
83 throw new IndexOutOfBoundsException();
84 }
85 return index <= staticTable.length()
86 ? staticTable.get(index)
87 : headers.get(index - staticTable.length() - 1);
88 }
89
90 public void add(final HPackHeader header) {
91 final int entrySize = header.getTotalSize();
92 if (entrySize > this.maxSize) {
93 clear();
94 return;
95 }
96 headers.addFirst(header);
97 currentSize += entrySize;
98 evict();
99 }
100
101 private void clear() {
102 currentSize = 0;
103 headers.clear();
104 }
105
106 private void evict() {
107 while (currentSize > maxSize) {
108 final HPackHeader header = headers.removeLast();
109 if (header != null) {
110 currentSize -= header.getTotalSize();
111 } else {
112 Asserts.check(currentSize == 0, "Current table size must be zero");
113 break;
114 }
115 }
116 }
117
118 }