KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mail > util > CRLFInputStream


1 /*
2  * CRLFInputStream.java
3  * Copyright (C) 2003 Doug Porter
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * You also have permission to link it with the Sun Microsystems, Inc.
11  * JavaMail(tm) extension and run that combination.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  */

22
23 package gnu.mail.util;
24
25 import java.io.BufferedInputStream JavaDoc;
26 import java.io.FilterInputStream JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 import javax.mail.Session JavaDoc;
31 /**
32  * CR/LF to LF filter input stream.
33  * <p>
34  * This class should probably be deprecated. Too many other classes, such as
35  * LineInputStream and DotTerminatedInputStream, don't know whether this class wraps
36  * their inner input streams. Those classes currently have to accomodate for lines
37  * ending in both LF and CRLF.
38  *
39  * @author Doug Porter
40  */

41 public class CRLFInputStream
42   extends ReadFilterInputStream
43 {
44     /**
45     * Constructor.
46     */

47     public CRLFInputStream (InputStream JavaDoc in)
48     {
49         // we need mark/reset
50
super (in.markSupported () ? in : new BufferedInputStream JavaDoc (in));
51     }
52     
53     /**
54     * Reads the next byte of data from this input stream, converting CR/LF to LF.
55     * Returns -1 if the end of the stream has been reached.
56     * @exception IOException if an I/O error occurs
57     */

58     public int read ()
59     throws IOException JavaDoc
60     {
61         // carriage return/line feed
62
final int CR = 13;
63         final int LF = 10;
64     
65         // max bytes to save for mark/reset
66
final int MarkReadLimit = 5;
67         
68         int c = super.read();
69         if (c == CR) {
70         
71             // look ahead one byte
72
mark (MarkReadLimit);
73             c = super.read ();
74             if (c != LF) {
75                 
76                 // keep any CR in the middle of a line
77
reset ();
78                 c = CR;
79                 
80             }
81             
82         }
83         
84         return c;
85     }
86 }
87
Popular Tags