View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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 }