View Javadoc

1   /*
2    * Copyright (c) 2005-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
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   * This class reads headers from the input stream
22   *
23   * @author Daniel Sendula
24   */
25  public class InternetHeadersImpl extends InternetHeaders {
26  
27      /**
28       * Default constructor
29       */
30      public InternetHeadersImpl() {
31          super();
32      }
33  
34      /**
35       * Constroctor that reads headers from the input stream
36       * @param inputStream input stream
37       * @throws MessagingException
38       */
39      public InternetHeadersImpl(InputStream inputStream) throws MessagingException {
40          super(inputStream);
41      }
42  
43      /**
44       * Loads headers from the input stream
45       * @param inputStream input stream
46       * @throws MessagingException
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       * This method reads a line from the input stream
94       * @param in input stream
95       * @return new string representing the read line
96       * @throws IOException
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                 //if (maybeEOL) {
121                     return new String(line, 0, l);
122                 //} else {
123                 //    k = c;
124                 //    maybeEOL = false;
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 }