View Javadoc

1   /*****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.util;
21  
22  import java.io.InputStream;
23  import java.io.Reader;
24  import java.io.UnsupportedEncodingException;
25  import java.io.IOException;
26  
27  /***
28   * A Reader for use with SMTP or other protocols in which lines
29   * must end with CRLF.  Extends Reader and overrides its 
30   * readLine() method.  The Reader readLine() method cannot
31   * serve for SMTP because it ends lines with either CR or LF alone. 
32   */
33  public class CRLFTerminatedReader extends Reader {
34  
35      public class TerminationException extends IOException {
36          private int where;
37          public TerminationException(int where) {
38              super();
39              this.where = where;
40          }
41  
42          public TerminationException(String s, int where) {
43              super(s);
44              this.where = where;
45          }
46  
47          public int position() {
48              return where;
49          }
50      }
51  
52      public class LineLengthExceededException extends IOException {
53          public LineLengthExceededException(String s) {
54              super(s);
55          }
56      }
57  
58      /***
59       * Constructs this CRLFTerminatedReader.
60       * @param in an InputStream
61       * @param charsetName the String name of a supported charset.  
62       * "ASCII" is common here.
63       * @throws UnsupportedEncodingException if the named charset
64       * is not supported
65       */
66      InputStream in;
67  
68      public CRLFTerminatedReader(InputStream in) {
69      this.in = in;
70      }
71  
72      public CRLFTerminatedReader(InputStream in, String enc) throws UnsupportedEncodingException {
73          this(in);
74      }
75  
76      private StringBuffer lineBuffer = new StringBuffer();
77      private final int
78              EOF = -1,
79              CR  = 13,
80              LF  = 10;
81  
82      private int tainted = -1;
83  
84      /***
85       * Read a line of text which is terminated by CRLF.  The concluding
86       * CRLF characters are not returned with the String, but if either CR
87       * or LF appears in the text in any other sequence it is returned
88       * in the String like any other character.  Some characters at the 
89       * end of the stream may be lost if they are in a "line" not
90       * terminated by CRLF.
91       * 
92       * @return either a String containing the contents of a 
93       * line which must end with CRLF, or null if the end of the 
94       * stream has been reached, possibly discarding some characters 
95       * in a line not terminated with CRLF. 
96       * @throws IOException if an I/O error occurs.
97       */
98      public String readLine() throws IOException{
99  
100         //start with the StringBuffer empty
101         lineBuffer.delete(0, lineBuffer.length());
102 
103         /* This boolean tells which state we are in,
104          * depending upon whether or not we got a CR
105          * in the preceding read().
106          */ 
107         boolean cr_just_received = false;
108 
109         // Until we add support for specifying a maximum line lenth as
110         // a Service Extension, limit lines to 2K, which is twice what
111         // RFC 2821 4.5.3.1 requires.
112         while (lineBuffer.length() <= 2048) {
113             int inChar = read();
114 
115             if (!cr_just_received){
116                 //the most common case, somewhere before the end of a line
117                 switch (inChar){
118                     case CR  :  cr_just_received = true;
119                                 break;
120                     case EOF :  return null;   // premature EOF -- discards data(?)
121                     case LF  :  //the normal ending of a line
122                         if (tainted == -1) tainted = lineBuffer.length();
123                         // intentional fall-through
124                     default  :  lineBuffer.append((char)inChar);
125                 }
126             }else{
127                 // CR has been received, we may be at end of line
128                 switch (inChar){
129                     case LF  :  // LF without a preceding CR
130                         if (tainted != -1) {
131                             int pos = tainted;
132                             tainted = -1;
133                             throw new TerminationException("\"bare\" CR or LF in data stream", pos);
134                         }
135                         return lineBuffer.toString();
136                     case EOF :  return null;   // premature EOF -- discards data(?)
137                     case CR  :  //we got two (or more) CRs in a row
138                         if (tainted == -1) tainted = lineBuffer.length();
139                         lineBuffer.append((char)CR);
140                         break;
141                     default  :  //we got some other character following a CR
142                         if (tainted == -1) tainted = lineBuffer.length();
143                         lineBuffer.append((char)CR);
144                         lineBuffer.append((char)inChar);
145                         cr_just_received = false;
146                 }
147             }
148         }//while
149         throw new LineLengthExceededException("Exceeded maximum line length");
150     }//method readLine()
151 
152     public int read() throws IOException {
153     return in.read();
154     }
155 
156     public boolean ready() throws IOException {
157     return in.available() > 0;
158     }
159 
160     public int read(char cbuf[], int  off, int  len) throws IOException {
161     byte [] temp = new byte[len];
162     int result = in.read(temp, 0, len);
163     for (int i=0;i<result;i++) cbuf[i] = (char) temp[i];
164     return result;
165     }
166 
167     public void close() throws IOException {
168     in.close();
169     }
170 }