1 /*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
47 */
48
49 public final class LineInputStream extends FilterInputStream {
50
51 private char[] lineBuffer = null; // reusable byte buffer
52
53 public LineInputStream(InputStream in) {
54 super(in);
55 }
56
57 /**
58 * Read a line containing only ASCII characters from the input
59 * stream. A line is terminated by a CR or NL or CR-NL sequence.
60 * A common error is a CR-CR-NL sequence, which will also terminate
61 * a line.
62 * The line terminator is not returned as part of the returned
63 * String. Returns null if no data is available. <p>
64 *
65 * This class is similar to the deprecated
66 * <code>DataInputStream.readLine()</code>
67 */
68 public String readLine() throws IOException {
69 InputStream in = this.in;
70 char[] buf = lineBuffer;
71
72 if (buf == null)
73 buf = lineBuffer = new char[128];
74
75 int c1;
76 int room = buf.length;
77 int offset = 0;
78
79 while ((c1 = in.read()) != -1) {
80 if (c1 == '\n') // Got NL, outa here.
81 break;
82 else if (c1 == '\r') {
83 // Got CR, is the next char NL ?
84 int c2 = in.read();
85 if (c2 == '\r') // discard extraneous CR
86 c2 = in.read();
|
1 /*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
47 */
48
49 public final class LineInputStream extends FilterInputStream {
50
51 private char[] lineBuffer = null; // reusable byte buffer
52
53 public LineInputStream(InputStream in) {
54 super(in);
55 }
56
57 /**
58 * Read a line containing only ASCII characters from the input
59 * stream. A line is terminated by a CR or NL or CR-NL sequence.
60 * A common error is a CR-CR-NL sequence, which will also terminate
61 * a line.
62 * The line terminator is not returned as part of the returned
63 * String. Returns null if no data is available. <p>
64 *
65 * This class is similar to the deprecated
66 * <code>DataInputStream.readLine()</code>
67 *
68 * @return line.
69 *
70 * @throws IOException if an I/O error occurs.
71 */
72 public String readLine() throws IOException {
73 InputStream in = this.in;
74 char[] buf = lineBuffer;
75
76 if (buf == null)
77 buf = lineBuffer = new char[128];
78
79 int c1;
80 int room = buf.length;
81 int offset = 0;
82
83 while ((c1 = in.read()) != -1) {
84 if (c1 == '\n') // Got NL, outa here.
85 break;
86 else if (c1 == '\r') {
87 // Got CR, is the next char NL ?
88 int c2 = in.read();
89 if (c2 == '\r') // discard extraneous CR
90 c2 = in.read();
|