1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20
21
22 package org.apache.james.util.stream;
23
24 import java.io.IOException;
25 import java.io.OutputStream;
26
27 /**
28 * Adds extra dot if dot occurs in message body at beginning of line (according to RFC1939)
29 * Compare also org.apache.james.smtpserver.SMTPInputStream
30 */
31 public class ExtraDotOutputStream extends CRLFOutputStream {
32
33 /**
34 * Constructor that wraps an OutputStream.
35 *
36 * @param out the OutputStream to be wrapped
37 */
38 public ExtraDotOutputStream(OutputStream out) {
39 super(out);
40 }
41
42 /**
43 * Overrides super writeChunk in order to add a "." if the previous chunk ended with
44 * a new line and a new chunk starts with "."
45 *
46 * @see org.apache.james.util.stream.CRLFOutputStream#writeChunk(byte[], int, int)
47 */
48 protected void writeChunk(byte buffer[], int offset, int length) throws IOException {
49 if (length > 0 && buffer[offset] == '.' && startOfLine) {
50 // add extra dot (the first of the pair)
51 out.write('.');
52 }
53 super.writeChunk(buffer, offset, length);
54 }
55
56 /**
57 * Writes a byte to the stream, adding dots where appropriate.
58 * Also fixes any naked CR or LF to the RFC 2821 mandated CRLF
59 * pairing.
60 *
61 * @param b the byte to write
62 *
63 * @throws IOException if an error occurs writing the byte
64 */
65 public void write(int b) throws IOException {
66 if (b == '.' && statusLast != LAST_WAS_OTHER) {
67 // add extra dot (the first of the pair)
68 out.write('.');
69 }
70 super.write(b);
71 }
72 }