Skip to content

Package: NewlineOutputStream

NewlineOutputStream

nameinstructionbranchcomplexitylinemethod
NewlineOutputStream(OutputStream)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
NewlineOutputStream(OutputStream, boolean)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
flush()
M: 30 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 18 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
write(byte[])
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
write(byte[], int, int)
M: 15 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
write(int)
M: 43 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2023 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 Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package org.eclipse.angus.mail.mbox;
18:
19: import java.io.FilterOutputStream;
20: import java.io.IOException;
21: import java.io.OutputStream;
22: import java.nio.charset.StandardCharsets;
23:
24: /**
25: * Convert the various newline conventions to the local platform's
26: * newline convention. Optionally, make sure the output ends with
27: * a blank line.
28: */
29: public class NewlineOutputStream extends FilterOutputStream {
30: private int lastb = -1;
31: private int bol = 1; // number of times in a row we're at beginning of line
32: private final boolean endWithBlankLine;
33: private static final byte[] newline;
34:
35: static {
36: String s = null;
37: try {
38: s = System.lineSeparator();
39: } catch (SecurityException sex) {
40: // ignore, should never happen
41: }
42:• if (s == null || s.length() <= 0)
43: s = "\n";
44: newline = s.getBytes(StandardCharsets.ISO_8859_1);
45: }
46:
47: public NewlineOutputStream(OutputStream os) {
48: this(os, false);
49: }
50:
51: public NewlineOutputStream(OutputStream os, boolean endWithBlankLine) {
52: super(os);
53: this.endWithBlankLine = endWithBlankLine;
54: }
55:
56: public void write(int b) throws IOException {
57:• if (b == '\r') {
58: out.write(newline);
59: bol++;
60:• } else if (b == '\n') {
61:• if (lastb != '\r') {
62: out.write(newline);
63: bol++;
64: }
65: } else {
66: out.write(b);
67: bol = 0; // no longer at beginning of line
68: }
69: lastb = b;
70: }
71:
72: public void write(byte[] b) throws IOException {
73: write(b, 0, b.length);
74: }
75:
76: public void write(byte[] b, int off, int len) throws IOException {
77:• for (int i = 0; i < len; i++) {
78: write(b[off + i]);
79: }
80: }
81:
82: public void flush() throws IOException {
83:• if (endWithBlankLine) {
84:• if (bol == 0) {
85: // not at bol, return to bol and add a blank line
86: out.write(newline);
87: out.write(newline);
88:• } else if (bol == 1) {
89: // at bol, add a blank line
90: out.write(newline);
91: }
92: }
93: bol = 2;
94: out.flush();
95: }
96: }