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