01: package pygmy.core;
02:
03: import java.util.Map;
04: import java.util.Iterator;
05: import java.util.LinkedHashMap;
06: import java.io.IOException;
07:
08: public class HttpHeaders {
09: private Map map;
10:
11: public HttpHeaders() {
12: this .map = new LinkedHashMap();
13: }
14:
15: public HttpHeaders(InternetInputStream stream) throws IOException {
16: this ();
17: String currentKey = null;
18: while (true) {
19: String line = stream.readline();
20: if ((line == null) || (line.length() == 0)) {
21: break;
22: }
23:
24: if (!Character.isSpaceChar(line.charAt(0))) {
25: int index = line.indexOf(':');
26: if (index >= 0) {
27: currentKey = line.substring(0, index).trim();
28: String value = line.substring(index + 1).trim();
29: put(currentKey, value);
30: }
31: } else if (currentKey != null) {
32: String value = get(currentKey);
33: put(currentKey, value + "\r\n\t" + line.trim());
34: }
35: }
36: }
37:
38: public String get(String key) {
39: return (String) map.get(key);
40: }
41:
42: public String get(String key, String defaultValue) {
43: String value = get(key);
44: return (value == null) ? defaultValue : value;
45: }
46:
47: public void put(String key, String value) {
48: map.put(key, value);
49: }
50:
51: public boolean contains(String headerKey) {
52: return map.containsKey(headerKey);
53: }
54:
55: public void clear() {
56: map.clear();
57: }
58:
59: public Iterator iterator() {
60: return map.keySet().iterator();
61: }
62:
63: public void print(InternetOutputStream stream) throws IOException {
64: for (Iterator i = iterator(); i.hasNext();) {
65: String key = (String) i.next();
66: stream.println(key + ": " + get(key));
67: }
68:
69: stream.println();
70: stream.flush();
71: }
72: }
|