1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.io.input;
18
19 import static org.apache.commons.io.IOUtils.CR;
20 import static org.apache.commons.io.IOUtils.EOF;
21 import static org.apache.commons.io.IOUtils.LF;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25
26 /**
27 * A filtering input stream that ensures the content will have Windows line endings, CRLF.
28 *
29 * @since 2.5
30 */
31 public class WindowsLineEndingInputStream extends InputStream {
32
33 private boolean atEos;
34
35 private boolean atSlashCr;
36
37 private boolean atSlashLf;
38
39 private final InputStream in;
40
41 private boolean injectSlashLf;
42
43 private final boolean lineFeedAtEos;
44
45 /**
46 * Constructs an input stream that filters another stream.
47 *
48 * @param in The input stream to wrap.
49 * @param lineFeedAtEos true to ensure that the stream ends with CRLF.
50 */
51 public WindowsLineEndingInputStream(final InputStream in, final boolean lineFeedAtEos) {
52 this.in = in;
53 this.lineFeedAtEos = lineFeedAtEos;
54 }
55
56 /**
57 * Closes the stream. Also closes the underlying stream.
58 *
59 * @throws IOException If an I/O error occurs.
60 */
61 @Override
62 public void close() throws IOException {
63 super.close();
64 in.close();
65 }
66
67 /**
68 * Handles the end of stream condition.
69 *
70 * @return The next char to output to the stream.
71 */
72 private int handleEos() {
73 if (!lineFeedAtEos) {
74 return EOF;
75 }
76 if (!atSlashLf && !atSlashCr) {
77 atSlashCr = true;
78 return CR;
79 }
80 if (!atSlashLf) {
81 atSlashCr = false;
82 atSlashLf = true;
83 return LF;
84 }
85 return EOF;
86 }
87
88 /**
89 * {@inheritDoc}
90 */
91 @Override
92 public synchronized void mark(final int readLimit) {
93 throw UnsupportedOperationExceptions.mark();
94 }
95
96 /**
97 * {@inheritDoc}
98 */
99 @Override
100 public synchronized int read() throws IOException {
101 if (atEos) {
102 return handleEos();
103 }
104 if (injectSlashLf) {
105 injectSlashLf = false;
106 return LF;
107 }
108 final boolean prevWasSlashR = atSlashCr;
109 final int target = in.read();
110 atEos = target == EOF;
111 if (!atEos) {
112 atSlashCr = target == CR;
113 atSlashLf = target == LF;
114 }
115 if (atEos) {
116 return handleEos();
117 }
118 if (target == LF && !prevWasSlashR) {
119 injectSlashLf = true;
120 return CR;
121 }
122 return target;
123 }
124 }