Headers.java
9.61 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang.time.FastDateFormat
*/
package com.adobe.granite.httpcache.api;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import org.apache.commons.lang.time.FastDateFormat;
public class Headers {
private final List<Entry> entries = new ArrayList<Entry>(16);
private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
private static final FastDateFormat RFC1123 = FastDateFormat.getInstance((String)"EEE, dd MMM yyyy HH:mm:ss z", (TimeZone)GMT, (Locale)Locale.US);
public String getHeader(String name) {
for (int i = 0; i < this.entries.size(); ++i) {
Entry e = this.entries.get(i);
if (!e.name.equalsIgnoreCase(name)) continue;
return e.getString();
}
return null;
}
public String[] getHeaders(String name) {
ArrayList<String> values = null;
for (int i = 0; i < this.entries.size(); ++i) {
Entry e = this.entries.get(i);
if (!e.name.equalsIgnoreCase(name)) continue;
if (values == null) {
values = new ArrayList<String>();
}
values.add(e.getString());
}
if (values != null) {
String[] result = new String[values.size()];
return values.toArray(result);
}
return null;
}
public long getDateHeader(String name) {
for (int i = 0; i < this.entries.size(); ++i) {
Entry e = this.entries.get(i);
if (!e.name.equalsIgnoreCase(name)) continue;
return e.getLong();
}
return -1;
}
public int getIntHeader(String name) {
for (int i = 0; i < this.entries.size(); ++i) {
Entry e = this.entries.get(i);
if (!e.name.equalsIgnoreCase(name)) continue;
return e.getInt();
}
return 0;
}
public void setHeader(String name, String value) {
if (value == null) {
for (int i = 0; i < this.entries.size(); ++i) {
if (!this.entries.get((int)i).name.equalsIgnoreCase(name)) continue;
this.entries.remove(i);
}
return;
}
this.setHeader(new Entry(name, value));
}
public void setHeader(String name, long value) {
this.setHeader(new Entry(name, value));
}
public void setHeader(String name, int value) {
this.setHeader(new Entry(name, value));
}
private void setHeader(Entry e) {
for (int i = 0; i < this.entries.size(); ++i) {
if (!this.entries.get((int)i).name.equalsIgnoreCase(e.name)) continue;
this.entries.set(i, e);
return;
}
this.entries.add(e);
}
public void addHeader(String name, String value) {
this.entries.add(new Entry(name, value));
}
public void addHeader(String name, long value) {
this.entries.add(new Entry(name, value));
}
public void addHeader(String name, int value) {
this.entries.add(new Entry(name, value));
}
public Entry[] getEntries() {
Entry[] result = new Entry[this.entries.size()];
this.entries.toArray(result);
return result;
}
public void save(OutputStream out) throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream(256);
Headers.writeShort(bout, this.entries.size());
block5 : for (int i = 0; i < this.entries.size(); ++i) {
Entry e = this.entries.get(i);
Headers.writeString(bout, e.name);
switch (e.type) {
case STRING: {
bout.write(83);
Headers.writeString(bout, e.getString());
continue block5;
}
case LONG: {
bout.write(76);
Headers.writeLong(bout, e.getLong());
continue block5;
}
case INT: {
bout.write(73);
Headers.writeInt(bout, e.getInt());
continue block5;
}
default: {
throw new InternalError("Illegal type: " + (Object)((Object)e.type));
}
}
}
out.write(bout.toByteArray());
}
private static void writeShort(OutputStream out, int n) throws IOException {
if (n > 65535) {
throw new IOException("Number too big to be saved as short: " + n);
}
out.write(n >>> 8 & 255);
out.write(n >>> 0 & 255);
}
private static void writeString(OutputStream out, String s) throws IOException {
byte[] b = s.getBytes("8859_1");
Headers.writeShort(out, b.length);
out.write(b);
}
private static void writeInt(OutputStream out, int n) throws IOException {
out.write(n >>> 24 & 255);
out.write(n >>> 16 & 255);
out.write(n >>> 8 & 255);
out.write(n >>> 0 & 255);
}
private static void writeLong(OutputStream out, long l) throws IOException {
Headers.writeInt(out, (int)(l >>> 32));
Headers.writeInt(out, (int)l & -1);
}
public void load(InputStream in) throws IOException {
int count = Headers.readUnsignedShort(in);
block5 : for (int i = 0; i < count; ++i) {
String name = Headers.readString(in);
char ch = (char)in.read();
switch (ch) {
case 'S': {
this.addHeader(name, Headers.readString(in));
continue block5;
}
case 'L': {
this.addHeader(name, Headers.readLong(in));
continue block5;
}
case 'I': {
this.addHeader(name, Headers.readInt(in));
continue block5;
}
default: {
throw new InternalError("Illegal type: " + ch);
}
}
}
}
private static int readUnsignedShort(InputStream in) throws IOException {
int ch2;
int ch1 = in.read();
if ((ch1 | (ch2 = in.read())) < 0) {
throw new EOFException();
}
return (ch1 << 8) + (ch2 << 0);
}
private static int readInt(InputStream in) throws IOException {
int ch4;
int ch3;
int ch2;
int ch1 = in.read();
if ((ch1 | (ch2 = in.read()) | (ch3 = in.read()) | (ch4 = in.read())) < 0) {
throw new EOFException();
}
return (ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0);
}
private static long readLong(InputStream in) throws IOException {
long h = (long)Headers.readInt(in) & 0xFFFFFFFFL;
long l = (long)Headers.readInt(in) & 0xFFFFFFFFL;
return h << 32 | l;
}
private static String readString(InputStream in) throws IOException {
int len;
byte[] b = new byte[Headers.readUnsignedShort(in)];
for (int off = 0; off < b.length; off += len) {
len = in.read(b, off, b.length - off);
if (len != -1) continue;
throw new EOFException();
}
return new String(b, "8859_1");
}
public String toString() {
StringBuilder s = new StringBuilder(256);
for (Entry e : this.entries) {
s.append(e.toString());
s.append("\r\n");
}
return s.toString();
}
public static class Entry {
final String name;
final Type type;
private String s;
private long l;
private int i;
Entry(String name, String value) {
this.type = Type.STRING;
this.name = name;
this.s = value;
}
Entry(String name, long value) {
this.type = Type.LONG;
this.name = name;
this.l = value;
}
Entry(String name, int value) {
this.type = Type.INT;
this.name = name;
this.i = value;
}
public String getString() {
switch (this.type) {
case STRING: {
return this.s;
}
case LONG: {
return RFC1123.format(this.l);
}
case INT: {
return String.valueOf(this.i);
}
}
throw new InternalError("Illegal type: " + (Object)((Object)this.type));
}
public long getLong() {
if (this.type == Type.LONG) {
return this.l;
}
return -1;
}
public int getInt() {
if (this.type == Type.INT) {
return this.i;
}
return 0;
}
public Type getType() {
return this.type;
}
public String getName() {
return this.name;
}
public String toString() {
StringBuilder s = new StringBuilder(32);
s.append(this.name);
s.append(": ");
s.append(this.getString());
return s.toString();
}
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public static enum Type {
STRING,
LONG,
INT;
private Type() {
}
}
}
}