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  
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 }