01: /*
02: * @(#)MimeInputStream.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package pnuts.servlet;
10:
11: import java.io.*;
12: import java.util.*;
13:
14: /**
15: * A class to parse MIME headers.
16: */
17: public class MimeInputStream extends FilterInputStream {
18:
19: protected Hashtable headers;
20:
21: public MimeInputStream(InputStream in) throws IOException {
22: super (in);
23: readHeaders();
24: }
25:
26: protected void readHeaders() throws IOException {
27: headers = new Hashtable();
28: ByteArrayOutputStream bout = new ByteArrayOutputStream();
29: int ch = read();
30: while (ch > 0) {
31: if (ch == '\r') {
32: ch = read();
33: if (ch == '\n') {
34: byte[] b = bout.toByteArray();
35: if (b.length == 0) {
36: return;
37: }
38: int len = b.length;
39: for (int i = 0; i < len; i++) {
40: if (b[i] == ':') {
41: String key = new String(b, 0, i, "8859_1");
42: i++;
43: while (i < len) {
44: if (b[i] == ' ' || b[i] == '\t') {
45: i++;
46: } else {
47: break;
48: }
49: }
50: String value = new String(b, i, len - i,
51: "8859_1");
52: headers.put(key.toLowerCase(), value);
53: bout.reset();
54: break;
55: }
56: }
57: ch = read();
58: } else {
59: throw new IOException();
60: }
61: } else {
62: bout.write(ch);
63: ch = read();
64: }
65: }
66: }
67:
68: public Enumeration getHeaders() {
69: return headers.keys();
70: }
71:
72: public String getHeader(String name) {
73: return (String) headers.get(name);
74: }
75: }
|