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 AbstractLineEndingInputStream {
32
33 /**
34 * Constructs an input stream that filters another stream
35 *
36 * @param inputStream The input stream to wrap.
37 * @param lineFeedAtEos true to ensure that the file ends with LF.
38 */
39 public UnixLineEndingInputStream(final InputStream inputStream, final boolean lineFeedAtEos) {
40 super(inputStream, lineFeedAtEos);
41 }
42
43 /**
44 * Handles the end of stream condition.
45 *
46 * @param previousWasSlashCr Indicates if the last seen was a {@code \r}.
47 * @return The next char to output to the stream.
48 */
49 private int handleEos(final boolean previousWasSlashCr) {
50 if (previousWasSlashCr || !lineFeedAtEos) {
51 return EOF;
52 }
53 if (!atSlashLf) {
54 atSlashLf = true;
55 return LF;
56 }
57 return EOF;
58 }
59
60 /**
61 * {@inheritDoc}
62 */
63 @Override
64 public synchronized int read() throws IOException {
65 final boolean previousWasSlashR = atSlashCr;
66 if (atEos) {
67 return handleEos(previousWasSlashR);
68 }
69 final int target = readUpdate();
70 if (atEos) {
71 return handleEos(previousWasSlashR);
72 }
73 if (atSlashCr) {
74 return LF;
75 }
76
77 if (previousWasSlashR && atSlashLf) {
78 return read();
79 }
80
81 return target;
82 }
83
84 /**
85 * Reads the next item from the target, updating internal flags in the process
86 *
87 * @return the next int read from the target stream.
88 * @throws IOException If an I/O error occurs.
89 */
90 private int readUpdate() throws IOException {
91 final int target = this.in.read();
92 atEos = target == EOF;
93 if (atEos) {
94 return target;
95 }
96 atSlashCr = target == CR;
97 atSlashLf = target == LF;
98 return target;
99 }
100 }