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 UNIX-style line endings, LF.
28 *
29 * @since 2.5
30 */
31 public class UnixLineEndingInputStream 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 final boolean lineFeedAtEndOfFile;
42
43 /**
44 * Constructs an input stream that filters another stream
45 *
46 * @param inputStream The input stream to wrap
47 * @param ensureLineFeedAtEndOfFile true to ensure that the file ends with LF
48 */
49 public UnixLineEndingInputStream(final InputStream inputStream, final boolean ensureLineFeedAtEndOfFile) {
50 this.in = inputStream;
51 this.lineFeedAtEndOfFile = ensureLineFeedAtEndOfFile;
52 }
53
54 /**
55 * Closes the stream. Also closes the underlying stream.
56 * @throws IOException If an I/O error occurs.
57 */
58 @Override
59 public void close() throws IOException {
60 super.close();
61 in.close();
62 }
63
64 /**
65 * Handles the end of stream condition.
66 *
67 * @param previousWasSlashCr Indicates if the last seen was a {@code \r}.
68 * @return The next char to output to the stream.
69 */
70 private int handleEos(final boolean previousWasSlashCr) {
71 if (previousWasSlashCr || !lineFeedAtEndOfFile) {
72 return EOF;
73 }
74 if (!atSlashLf) {
75 atSlashLf = true;
76 return LF;
77 }
78 return EOF;
79 }
80
81 /**
82 * {@inheritDoc}
83 */
84 @Override
85 public synchronized void mark(final int readLimit) {
86 throw UnsupportedOperationExceptions.mark();
87 }
88
89 /**
90 * {@inheritDoc}
91 */
92 @Override
93 public synchronized int read() throws IOException {
94 final boolean previousWasSlashR = atSlashCr;
95 if (atEos) {
96 return handleEos(previousWasSlashR);
97 }
98 final int target = readWithUpdate();
99 if (atEos) {
100 return handleEos(previousWasSlashR);
101 }
102 if (atSlashCr) {
103 return LF;
104 }
105
106 if (previousWasSlashR && atSlashLf) {
107 return read();
108 }
109
110 return target;
111 }
112
113 /**
114 * Reads the next item from the target, updating internal flags in the process
115 * @return the next int read from the target stream
116 * @throws IOException If an I/O error occurs.
117 */
118 private int readWithUpdate() throws IOException {
119 final int target = this.in.read();
120 atEos = target == EOF;
121 if (atEos) {
122 return target;
123 }
124 atSlashCr = target == CR;
125 atSlashLf = target == LF;
126 return target;
127 }
128 }