Skip to content

Package: LineInputStream

LineInputStream

nameinstructionbranchcomplexitylinemethod
LineInputStream(InputStream)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
readLine()
M: 148 C: 0
0%
M: 30 C: 0
0%
M: 16 C: 0
0%
M: 36 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

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: /* FROM mail.jar */
12: package org.jvnet.mimepull;
13:
14: import java.io.*;
15:
16: /**
17: * This class is to support reading CRLF terminated lines that
18: * contain only US-ASCII characters from an input stream. Provides
19: * functionality that is similar to the deprecated
20: * <code>DataInputStream.readLine()</code>. Expected use is to read
21: * lines as String objects from a RFC822 stream.
22: *
23: * It is implemented as a FilterInputStream, so one can just wrap
24: * this class around any input stream and read bytes from this filter.
25: *
26: * @author John Mani
27: */
28:
29: final class LineInputStream extends FilterInputStream {
30:
31: private char[] lineBuffer = null; // reusable byte buffer
32: private static int MAX_INCR = 1024*1024;        // 1MB
33:
34: public LineInputStream(InputStream in) {
35:         super(in);
36: }
37:
38: /**
39: * Read a line containing only ASCII characters from the input
40: * stream. A line is terminated by a CR or NL or CR-NL sequence.
41: * A common error is a CR-CR-NL sequence, which will also terminate
42: * a line.
43: * The line terminator is not returned as part of the returned
44: * String. Returns null if no data is available. <p>
45: *
46: * This class is similar to the deprecated
47: * <code>DataInputStream.readLine()</code>
48: */
49: public String readLine() throws IOException {
50:         //InputStream in = this.in;
51:         char[] buf = lineBuffer;
52:
53:•        if (buf == null) {
54: buf = lineBuffer = new char[128];
55: }
56:
57:         int c1;
58:         int room = buf.length;
59:         int offset = 0;
60:
61:•        while ((c1 = in.read()) != -1) {
62:•         if (c1 == '\n') {
63: break;
64:• } else if (c1 == '\r') {
65:                 // Got CR, is the next char NL ?
66:                 boolean twoCRs = false;
67:•                if (in.markSupported()) {
68: in.mark(2);
69: }
70:                 int c2 = in.read();
71:•                if (c2 == '\r') {                // discard extraneous CR
72:                  twoCRs = true;
73:                  c2 = in.read();
74:                 }
75:•                if (c2 != '\n') {
76:                  /*
77:                  * If the stream supports it (which we hope will always
78:                  * be the case), reset to after the first CR. Otherwise,
79:                  * we wrap a PushbackInputStream around the stream so we
80:                  * can unread the characters we don't need. The only
81:                  * problem with that is that the caller might stop
82:                  * reading from this LineInputStream, throw it away,
83:                  * and then start reading from the underlying stream.
84:                  * If that happens, the pushed back characters will be
85:                  * lost forever.
86:                  */
87:•                 if (in.markSupported()) {
88: in.reset();
89: } else {
90:•                        if (!(in instanceof PushbackInputStream)) {
91: in /*= this.in*/ = new PushbackInputStream(in, 2);
92: }
93:•                        if (c2 != -1) {
94: ((PushbackInputStream)in).unread(c2);
95: }
96:•                        if (twoCRs) {
97: ((PushbackInputStream)in).unread('\r');
98: }
99:                  }
100:                 }
101:                 break; // outa here.
102:          }
103:
104:          // Not CR, NL or CR-NL ...
105:          // .. Insert the byte into our byte buffer
106:•         if (--room < 0) { // No room, need to grow.
107:•                if (buf.length < MAX_INCR) {
108: buf = new char[buf.length * 2];
109: } else {
110: buf = new char[buf.length + MAX_INCR];
111: }
112:                 room = buf.length - offset - 1;
113:                 System.arraycopy(lineBuffer, 0, buf, 0, offset);
114:                 lineBuffer = buf;
115:          }
116:          buf[offset++] = (char)c1;
117:         }
118:
119:•        if ((c1 == -1) && (offset == 0)) {
120: return null;
121: }
122:         
123:         return String.copyValueOf(buf, 0, offset);
124: }
125: }