KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > util > LineInputStream


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)LineInputStream.java 1.8 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.mail.util;
29
30 import java.io.*;
31
32 /**
33  * This class is to support reading CRLF terminated lines that
34  * contain only US-ASCII characters from an input stream. Provides
35  * functionality that is similar to the deprecated
36  * <code>DataInputStream.readLine()</code>. Expected use is to read
37  * lines as String objects from a RFC822 stream.
38  *
39  * It is implemented as a FilterInputStream, so one can just wrap
40  * this class around any input stream and read bytes from this filter.
41  *
42  * @author John Mani
43  */

44
45 public class LineInputStream extends FilterInputStream {
46
47     private char[] lineBuffer = null; // reusable byte buffer
48

49     public LineInputStream(InputStream in) {
50     super(in);
51     }
52
53     /**
54      * Read a line containing only ASCII characters from the input
55      * stream. A line is terminated by a CR or NL or CR-NL sequence.
56      * A common error is a CR-CR-NL sequence, which will also terminate
57      * a line.
58      * The line terminator is not returned as part of the returned
59      * String. Returns null if no data is available. <p>
60      *
61      * This class is similar to the deprecated
62      * <code>DataInputStream.readLine()</code>
63      */

64     public String JavaDoc readLine() throws IOException {
65     InputStream in = this.in;
66     char[] buf = lineBuffer;
67
68     if (buf == null)
69         buf = lineBuffer = new char[128];
70
71     int c1;
72     int room = buf.length;
73     int offset = 0;
74
75     while ((c1 = in.read()) != -1) {
76         if (c1 == '\n') // Got NL, outa here.
77
break;
78         else if (c1 == '\r') {
79         // Got CR, is the next char NL ?
80
int c2 = in.read();
81         if (c2 == '\r') // discard extraneous CR
82
c2 = in.read();
83         if (c2 != '\n') {
84             // If not NL, push it back
85
if (!(in instanceof PushbackInputStream))
86             in = this.in = new PushbackInputStream(in);
87             ((PushbackInputStream)in).unread(c2);
88         }
89         break; // outa here.
90
}
91
92         // Not CR, NL or CR-NL ...
93
// .. Insert the byte into our byte buffer
94
if (--room < 0) { // No room, need to grow.
95
buf = new char[offset + 128];
96         room = buf.length - offset - 1;
97         System.arraycopy(lineBuffer, 0, buf, 0, offset);
98         lineBuffer = buf;
99         }
100         buf[offset++] = (char)c1;
101     }
102
103     if ((c1 == -1) && (offset == 0))
104         return null;
105     
106     return String.copyValueOf(buf, 0, offset);
107     }
108 }
109
Popular Tags