Skip to content

Package: LineInputStream

LineInputStream

nameinstructionbranchcomplexitylinemethod
LineInputStream(InputStream)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
readLine()
M: 45 C: 59
57%
M: 9 C: 11
55%
M: 7 C: 4
36%
M: 9 C: 17
65%
M: 0 C: 1
100%

Coverage

1: /*
2: * Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: /*
12: * @(#)LineInputStream.java 1.7 03/01/07
13: */
14:
15:
16:
17: package com.sun.xml.messaging.saaj.packaging.mime.util;
18:
19: import java.io.*;
20:
21: /**
22: * This class is to support reading CRLF terminated lines that
23: * contain only US-ASCII characters from an input stream. Provides
24: * functionality that is similar to the deprecated
25: * <code>DataInputStream.readLine()</code>. Expected use is to read
26: * lines as String objects from a RFC822 stream.
27: *
28: * It is implemented as a FilterInputStream, so one can just wrap
29: * this class around any input stream and read bytes from this filter.
30: *
31: * @author John Mani
32: */
33:
34: public final class LineInputStream extends FilterInputStream {
35:
36: private char[] lineBuffer = null; // reusable byte buffer
37:
38: public LineInputStream(InputStream in) {
39:         super(in);
40: }
41:
42: /**
43: * Read a line containing only ASCII characters from the input
44: * stream. A line is terminated by a CR or NL or CR-NL sequence.
45: * A common error is a CR-CR-NL sequence, which will also terminate
46: * a line.
47: * The line terminator is not returned as part of the returned
48: * String. Returns null if no data is available. <p>
49: *
50: * This class is similar to the deprecated
51: * <code>DataInputStream.readLine()</code>
52: *
53: * @return line.
54: *
55: * @throws IOException if an I/O error occurs.
56: */
57: public String readLine() throws IOException {
58:         InputStream in = this.in;
59:         char[] buf = lineBuffer;
60:
61:•        if (buf == null)
62:          buf = lineBuffer = new char[128];
63:
64:         int c1;
65:         int room = buf.length;
66:         int offset = 0;
67:
68:•        while ((c1 = in.read()) != -1) {
69:•         if (c1 == '\n') // Got NL, outa here.
70:                 break;
71:•         else if (c1 == '\r') {
72:                 // Got CR, is the next char NL ?
73:                 int c2 = in.read();
74:•                if (c2 == '\r')                // discard extraneous CR
75:                  c2 = in.read();
76:•                if (c2 != '\n') {
77:                  // If not NL, push it back
78:•                 if (!(in instanceof PushbackInputStream))
79:                         in = this.in = new PushbackInputStream(in);
80:                  ((PushbackInputStream)in).unread(c2);
81:                 }
82:                 break; // outa here.
83:          }
84:
85:          // Not CR, NL or CR-NL ...
86:          // .. Insert the byte into our byte buffer
87:•         if (--room < 0) { // No room, need to grow.
88:                 buf = new char[offset + 128];
89:                 room = buf.length - offset - 1;
90:                 System.arraycopy(lineBuffer, 0, buf, 0, offset);
91:                 lineBuffer = buf;
92:          }
93:          buf[offset++] = (char)c1;
94:         }
95:
96:•        if ((c1 == -1) && (offset == 0))
97:          return null;
98:         
99:         return String.copyValueOf(buf, 0, offset);
100: }
101: }