KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * LineInputStream.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.ByteArrayOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28
29 import javax.mail.MessagingException JavaDoc;
30 import javax.mail.Session JavaDoc;
31
32 /**
33  * An input stream that can read lines of input.
34  *
35  * @author Doug Porter
36  */

37 public class LineInputStream
38   extends ReadFilterInputStream
39 {
40     
41     private boolean isDataAvailable = false;
42     
43     /**
44     * Constructor.
45     */

46     public LineInputStream (InputStream JavaDoc in)
47     {
48         super (in);
49     }
50   
51     
52     /** Read the next line of text without character encoding.
53      *
54      * The contract is as for <code>DataInput.readLine</code>, except CR is accepted
55      * before the end of the line.
56      *
57      * @return Line, or null on end of stream.
58      */

59     public String JavaDoc readLine ()
60     throws IOException JavaDoc {
61         
62         final int EOF = -1;
63         final int CR = 13;
64         final int LF = 10;
65         
66         ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc ();
67         int c;
68         String JavaDoc line = null;
69         
70         try {
71             
72             c = read ();
73             isDataAvailable = (c != EOF);
74             
75             while (c != LF &&
76                    c != EOF) {
77     
78                 /* plain DataInput.readLine contract
79                 if (c != CR) {
80                     
81                     buffer.write (c);
82                     
83                 }
84                 
85                 c = read ();
86                 */

87                 
88                 if (c == CR) {
89                 
90                     // look ahead one byte
91
c = read ();
92                     if (c != LF) {
93                         buffer.write (CR);
94                     }
95                     
96                 }
97                 else {
98                     buffer.write (c);
99                     c = read ();
100                 }
101             }
102         }
103         catch (IOException JavaDoc ioe) {
104             isDataAvailable = false;
105             throw ioe;
106         }
107         
108         if (isDataAvailable) {
109             
110             // Contrary to its spec, ByteArrayOutputStream.toString(0) does character encoding,
111
// at least in Sun's 1.4.1 jvm. So there is no point in using it here.
112
line = buffer.toString ();
113             
114         }
115
116         //Session.log ("< " + line);
117
return line;
118     }
119     
120 }
121
Popular Tags