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
29
30
31
32 package io.netty.handler.codec.http2;
33
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37
38
39
40
41 public final class HpackBenchmarkUtil {
42 private HpackBenchmarkUtil() {
43 }
44
45
46
47
48 private static class HeadersKey {
49 final HpackHeadersSize size;
50 final boolean limitToAscii;
51
52 HeadersKey(HpackHeadersSize size, boolean limitToAscii) {
53 this.size = size;
54 this.limitToAscii = limitToAscii;
55 }
56
57 List<HpackHeader> newHeaders() {
58 return size.newHeaders(limitToAscii);
59 }
60
61 @Override
62 public boolean equals(Object o) {
63 if (this == o) {
64 return true;
65 }
66 if (o == null || getClass() != o.getClass()) {
67 return false;
68 }
69
70 HeadersKey that = (HeadersKey) o;
71
72 if (limitToAscii != that.limitToAscii) {
73 return false;
74 }
75 return size == that.size;
76 }
77
78 @Override
79 public int hashCode() {
80 int result = size.hashCode();
81 result = 31 * result + (limitToAscii ? 1 : 0);
82 return result;
83 }
84 }
85
86 private static final Map<HeadersKey, List<HpackHeader>> headersMap;
87
88 static {
89 HpackHeadersSize[] sizes = HpackHeadersSize.values();
90 headersMap = new HashMap<HeadersKey, List<HpackHeader>>(sizes.length * 2);
91 for (HpackHeadersSize size : sizes) {
92 HeadersKey key = new HeadersKey(size, true);
93 headersMap.put(key, key.newHeaders());
94
95 key = new HeadersKey(size, false);
96 headersMap.put(key, key.newHeaders());
97 }
98 }
99
100
101
102
103 static List<HpackHeader> headers(HpackHeadersSize size, boolean limitToAscii) {
104 return headersMap.get(new HeadersKey(size, limitToAscii));
105 }
106
107 static Http2Headers http2Headers(HpackHeadersSize size, boolean limitToAscii) {
108 List<HpackHeader> hpackHeaders = headersMap.get(new HeadersKey(size, limitToAscii));
109 Http2Headers http2Headers = new DefaultHttp2Headers(false);
110 for (int i = 0; i < hpackHeaders.size(); ++i) {
111 HpackHeader hpackHeader = hpackHeaders.get(i);
112 http2Headers.add(hpackHeader.name, hpackHeader.value);
113 }
114 return http2Headers;
115 }
116 }