KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * DotTerminatedInputStream.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 /**
33  * An input stream that returns end of stream on the sequence "CRLF . CRLF".
34  * <p>
35  * If you want to read until a line with a single dot, wrap your stream in
36  * this class before passing it to LineInputStream.
37  * <p>
38  * This implementation is dependent on methods ultimately calling read()
39  * for input.
40  * <p>
41  * @author Doug Porter
42  */

43 public class DotTerminatedInputStream
44 extends ReadFilterInputStream
45 {
46     static final int MarkReadLimit = 5;
47     static final int EOS = -1;
48     
49     private boolean isEOS = false;
50     
51     /**
52     * Constructor.
53     * @param in the inner input stream
54     */

55     public DotTerminatedInputStream (InputStream JavaDoc in) {
56         // we need mark/reset
57
super (in.markSupported () ? in : new BufferedInputStream JavaDoc (in));
58     }
59     
60     /**
61      * Returns the number of bytes available.
62      * <p>
63      * If a dot line has been seen, returns zero.
64      * @return the number of bytes available
65      */

66     public int available ()
67     throws IOException JavaDoc
68     {
69         int n;
70         if (isEOS) {
71             n = 0;
72         }
73         else {
74             n = super.available ();
75         }
76         
77         return n;
78     }
79     
80     /**
81      * Returns the next byte from this stream.
82      * <p>
83      * If a dot line has been seen, returns -1.
84      * @return The next byte from this stream, or -1 on dot line or end of stream
85      */

86     public int read ()
87     throws IOException JavaDoc
88     {
89         final int CR = 13;
90         final int LF = 10;
91         final int DOT = '.';
92         
93         int b;
94         
95         if (isEOS) {
96             
97             b = EOS;
98             
99         }
100         else {
101             
102             b = super.read ();
103             mark (MarkReadLimit);
104             if (b == CR &&
105                 super.read () == LF &&
106                 super.read () == DOT &&
107                 super.read () == CR &&
108                 super.read () == LF) {
109                     
110                 isEOS = true;
111                 
112             }
113             
114             else {
115                 // our streams often have stripped CRs (CRLFInputStream)
116

117                 reset ();
118                 mark (MarkReadLimit);
119                 
120                 if (b == LF &&
121                     super.read () == DOT &&
122                     super.read () == LF) {
123
124                     isEOS = true;
125                     
126                 }
127             }
128             
129             if (isEOS) {
130                     
131                 // end of stream
132
b = EOS;
133                 
134             }
135             
136             else {
137                 
138                 reset ();
139                 
140             }
141         }
142         
143         return b;
144     }
145 }
146
147
Popular Tags