]> git.argeo.org Git - gpl/argeo-slc.git/blob - ext/javax.mail.mbox/src/com/sun/mail/mbox/ContentLengthCounter.java
Make logging synchronous during native image build
[gpl/argeo-slc.git] / ext / javax.mail.mbox / src / com / sun / mail / mbox / ContentLengthCounter.java
1 /*
2 * Copyright (c) 1997, 2018 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 com.sun.mail.mbox;
18
19 import java.io.*;
20
21 /**
22 * Count the number of bytes in the body of the message written to the stream.
23 */
24 class ContentLengthCounter extends OutputStream {
25 private long size = 0;
26 private boolean inHeader = true;
27 private int lastb1 = -1, lastb2 = -1;
28
29 public void write(int b) throws IOException {
30 if (inHeader) {
31 // if line terminator is CR
32 if (b == '\r' && lastb1 == '\r')
33 inHeader = false;
34 else if (b == '\n') {
35 // if line terminator is \n
36 if (lastb1 == '\n')
37 inHeader = false;
38 // if line terminator is CRLF
39 else if (lastb1 == '\r' && lastb2 == '\n')
40 inHeader = false;
41 }
42 lastb2 = lastb1;
43 lastb1 = b;
44 } else
45 size++;
46 }
47
48 public void write(byte[] b) throws IOException {
49 if (inHeader)
50 super.write(b);
51 else
52 size += b.length;
53 }
54
55 public void write(byte[] b, int off, int len) throws IOException {
56 if (inHeader)
57 super.write(b, off, len);
58 else
59 size += len;
60 }
61
62 public long getSize() {
63 return size;
64 }
65
66 /*
67 public static void main(String argv[]) throws Exception {
68 int b;
69 ContentLengthCounter os = new ContentLengthCounter();
70 while ((b = System.in.read()) >= 0)
71 os.write(b);
72 System.out.println("size " + os.getSize());
73 }
74 */
75 }