1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.mercury.maildir.util;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import javax.mail.MessagingException;
18 import javax.mail.internet.InternetHeaders;
19
20
21
22
23
24
25 public class InternetHeadersImpl extends InternetHeaders {
26
27
28
29
30 public InternetHeadersImpl() {
31 super();
32 }
33
34
35
36
37
38
39 public InternetHeadersImpl(InputStream inputStream) throws MessagingException {
40 super(inputStream);
41 }
42
43
44
45
46
47
48 public void load(InputStream inputStream) throws MessagingException {
49 String s1 = null;
50
51 StringBuffer stringbuffer = new StringBuffer();
52
53 boolean run = true;
54 while (run) {
55 try {
56 String s = readLine(inputStream);
57 if ((s != null) && (s.startsWith(" ") || s.startsWith("\t"))) {
58
59
60 if (s1 != null) {
61 stringbuffer.append(s1);
62 s1 = null;
63 }
64 stringbuffer.append("\r\n");
65 stringbuffer.append(s);
66 } else {
67 if (s1 != null) {
68 addHeaderLine(s1);
69 } else {
70 if (stringbuffer.length() > 0) {
71
72 addHeaderLine(stringbuffer.toString());
73 stringbuffer.setLength(0);
74 }
75 }
76 s1 = s;
77 }
78
79 if (s == null) {
80 run = false;
81 } else {
82 if (s.length() <= 0) {
83 return;
84 }
85 }
86 } catch (IOException e) {
87 throw new MessagingException("Error in input stream", e);
88 }
89 }
90 }
91
92
93
94
95
96
97
98 public String readLine(InputStream in) throws IOException {
99 byte[] line = new byte[1024];
100 int l = 0;
101 boolean maybeEOL = false;
102 while (true) {
103 int c = in.read();
104
105 int k = -1;
106
107 if (c == -1) {
108 if (l == 0) {
109 return "";
110 } else {
111 return new String(line, 0, l, "ISO-8859-1");
112 }
113 } else if (c == 13) {
114 if (maybeEOL) {
115 k = c;
116 } else {
117 maybeEOL = true;
118 }
119 } else if (c == 10) {
120
121 return new String(line, 0, l);
122
123
124
125
126 } else {
127 k = c;
128 maybeEOL = false;
129 }
130 if (k != -1) {
131 if (l == line.length) {
132 byte[] linet = new byte[line.length*2];
133 System.arraycopy(line, 0, linet, 0, line.length);
134 line = linet;
135 }
136 line[l] = (byte)k;
137 l = l + 1;
138 }
139 }
140 }
141 }