    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 slashNSeen;
    034
    035    private boolean slashRSeen;
    036
    037    private boolean eofSeen;
    038
    039    private final InputStream target;
    040
    041    private final boolean ensureLineFeedAtEndOfFile;
    042
    043    /**
    044     * Creates an input stream that filters another stream
    045     *
    046     * @param in                        The input stream to wrap
    047     * @param ensureLineFeedAtEndOfFile true to ensure that the file ends with LF
    048     */
    049    public UnixLineEndingInputStream(final InputStream in, final boolean ensureLineFeedAtEndOfFile) {
    050        this.target = in;
    051        this.ensureLineFeedAtEndOfFile = ensureLineFeedAtEndOfFile;
    052    }
    053
    054    /**
    055     * Reads the next item from the target, updating internal flags in the process
    056     * @return the next int read from the target stream
    057     * @throws IOException upon error
    058     */
    059    private int readWithUpdate() throws IOException {
    060        final int target = this.target.read();
    061        eofSeen = target == EOF;
    062        if (eofSeen) {
    063            return target;
    064        }
    065        slashNSeen = target == LF;
    066        slashRSeen = target == CR;
    067        return target;
    068    }
    069
    070    /**
    071     * {@inheritDoc}
    072     */
    073    @Override
    074    public int read() throws IOException {
    075        final boolean previousWasSlashR = slashRSeen;
    076        if (eofSeen) {
    077            return eofGame(previousWasSlashR);
    078        }
    079        final int target = readWithUpdate();
    080        if (eofSeen) {
    081            return eofGame(previousWasSlashR);
    082        }
    083        if (slashRSeen) {
    084            return LF;
    085        }
    086
    087        if (previousWasSlashR && slashNSeen) {
    088            return read();
    089        }
    090
    091        return target;
    092    }
    093
    094    /**
    095     * Handles the EOF-handling at the end of the stream
    096     * @param previousWasSlashR Indicates if the last seen was a \r
    097     * @return The next char to output to the stream
    098     */
    099    private int eofGame(final boolean previousWasSlashR) {
    100        if (previousWasSlashR || !ensureLineFeedAtEndOfFile) {
    101            return EOF;
    102        }
    103        if (!slashNSeen) {
    104            slashNSeen = true;
    105            return LF;
    106        }
    107        return EOF;
    108    }
    109
    110    /**
    111     * Closes the stream. Also closes the underlying stream.
    112     * @throws IOException upon error
    113     */
    114    @Override
    115    public void close() throws IOException {
    116        super.close();
    117        target.close();
    118    }
    119
    120    /**
    121     * {@inheritDoc}
    122     */
    123    @Override
    124    public synchronized void mark(final int readlimit) {
    125        throw UnsupportedOperationExceptions.mark();
    126    }
    127}