Skip to content

Package: ContentLengthCounter

ContentLengthCounter

nameinstructionbranchcomplexitylinemethod
ContentLengthCounter()
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getSize()
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%
write(byte[])
M: 16 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
write(byte[], int, int)
M: 17 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
write(int)
M: 51 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 12 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.IOException;
20: import java.io.OutputStream;
21:
22: /**
23: * Count the number of bytes in the body of the message written to the stream.
24: */
25: class ContentLengthCounter extends OutputStream {
26: private long size = 0;
27: private boolean inHeader = true;
28: private int lastb1 = -1, lastb2 = -1;
29:
30: public void write(int b) throws IOException {
31:• if (inHeader) {
32: // if line terminator is CR
33:• if (b == '\r' && lastb1 == '\r')
34: inHeader = false;
35:• else if (b == '\n') {
36: // if line terminator is \n
37:• if (lastb1 == '\n')
38: inHeader = false;
39: // if line terminator is CRLF
40:• else if (lastb1 == '\r' && lastb2 == '\n')
41: inHeader = false;
42: }
43: lastb2 = lastb1;
44: lastb1 = b;
45: } else
46: size++;
47: }
48:
49: public void write(byte[] b) throws IOException {
50:• if (inHeader)
51: super.write(b);
52: else
53: size += b.length;
54: }
55:
56: public void write(byte[] b, int off, int len) throws IOException {
57:• if (inHeader)
58: super.write(b, off, len);
59: else
60: size += len;
61: }
62:
63: public long getSize() {
64: return size;
65: }
66:
67: /*
68: public static void main(String argv[]) throws Exception {
69:         int b;
70:         ContentLengthCounter os = new ContentLengthCounter();
71:         while ((b = System.in.read()) >= 0)
72:          os.write(b);
73:         System.out.println("size " + os.getSize());
74: }
75: */
76: }