001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.io.input;
018
019import static org.apache.commons.io.IOUtils.CR;
020import static org.apache.commons.io.IOUtils.EOF;
021import static org.apache.commons.io.IOUtils.LF;
022
023import java.io.IOException;
024import java.io.InputStream;
025
026/**
027 * A filtering input stream that ensures the content will have UNIX-style line endings, LF.
028 *
029 * @since 2.5
030 */
031public class UnixLineEndingInputStream extends InputStream {
032
033    private boolean atEos;
034
035    private boolean atSlashCr;
036
037    private boolean atSlashLf;
038
039    private final InputStream in;
040
041    private final boolean lineFeedAtEndOfFile;
042
043    /**
044     * Constructs an input stream that filters another stream
045     *
046     * @param inputStream                        The input stream to wrap
047     * @param ensureLineFeedAtEndOfFile true to ensure that the file ends with LF
048     */
049    public UnixLineEndingInputStream(final InputStream inputStream, final boolean ensureLineFeedAtEndOfFile) {
050        this.in = inputStream;
051        this.lineFeedAtEndOfFile = ensureLineFeedAtEndOfFile;
052    }
053
054    /**
055     * Closes the stream. Also closes the underlying stream.
056     * @throws IOException upon error
057     */
058    @Override
059    public void close() throws IOException {
060        super.close();
061        in.close();
062    }
063
064    /**
065     * Handles the end of stream condition.
066     *
067     * @param previousWasSlashCr Indicates if the last seen was a {@code \r}.
068     * @return The next char to output to the stream.
069     */
070    private int handleEos(final boolean previousWasSlashCr) {
071        if (previousWasSlashCr || !lineFeedAtEndOfFile) {
072            return EOF;
073        }
074        if (!atSlashLf) {
075            atSlashLf = true;
076            return LF;
077        }
078        return EOF;
079    }
080
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    public synchronized void mark(final int readLimit) {
086        throw UnsupportedOperationExceptions.mark();
087    }
088
089    /**
090     * {@inheritDoc}
091     */
092    @Override
093    public int read() throws IOException {
094        final boolean previousWasSlashR = atSlashCr;
095        if (atEos) {
096            return handleEos(previousWasSlashR);
097        }
098        final int target = readWithUpdate();
099        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 upon error
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}