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 package org.apache.james.mime4j.io;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.PushbackInputStream;
25
26 /**
27 * InputStream which converts <code>\r</code>
28 * bytes not followed by <code>\n</code> and <code>\n</code> not
29 * preceded by <code>\r</code> to <code>\r\n</code>.
30 */
31 public class EOLConvertingInputStream extends InputStream {
32 /** Converts single '\r' to '\r\n' */
33 public static final int CONVERT_CR = 1;
34 /** Converts single '\n' to '\r\n' */
35 public static final int CONVERT_LF = 2;
36 /** Converts single '\r' and '\n' to '\r\n' */
37 public static final int CONVERT_BOTH = 3;
38
39 private PushbackInputStream in = null;
40 private int previous = 0;
41 private int flags = CONVERT_BOTH;
42
43 /**
44 * Creates a new <code>EOLConvertingInputStream</code>
45 * instance converting bytes in the given <code>InputStream</code>.
46 * The flag <code>CONVERT_BOTH</code> is the default.
47 *
48 * @param in the <code>InputStream</code> to read from.
49 */
50 public EOLConvertingInputStream(InputStream in) {
51 this(in, CONVERT_BOTH);
52 }
53 /**
54 * Creates a new <code>EOLConvertingInputStream</code>
55 * instance converting bytes in the given <code>InputStream</code>.
56 *
57 * @param in the <code>InputStream</code> to read from.
58 * @param flags one of <code>CONVERT_CR</code>, <code>CONVERT_LF</code> or
59 * <code>CONVERT_BOTH</code>.
60 */
61 public EOLConvertingInputStream(InputStream in, int flags) {
62 super();
63
64 this.in = new PushbackInputStream(in, 2);
65 this.flags = flags;
66 }
67
68 /**
69 * Closes the underlying stream.
70 *
71 * @throws IOException on I/O errors.
72 */
73 @Override
74 public void close() throws IOException {
75 in.close();
76 }
77
78 /**
79 * @see java.io.InputStream#read()
80 */
81 @Override
82 public int read() throws IOException {
83 int b = in.read();
84
85 if (b == -1) {
86 return -1;
87 }
88
89 if ((flags & CONVERT_CR) != 0 && b == '\r') {
90 int c = in.read();
91 if (c != -1) {
92 in.unread(c);
93 }
94 if (c != '\n') {
95 in.unread('\n');
96 }
97 } else if ((flags & CONVERT_LF) != 0 && b == '\n' && previous != '\r') {
98 b = '\r';
99 in.unread('\n');
100 }
101
102 previous = b;
103
104 return b;
105 }
106
107 }