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 atEos;
034
035    private boolean atSlashCr;
036
037    private boolean atSlashLf;
038
039    private final InputStream in;
040
041    private boolean injectSlashLf;
042
043    private final boolean lineFeedAtEndOfFile;
044
045    /**
046     * Constructs 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.in = in;
053        this.lineFeedAtEndOfFile = ensureLineFeedAtEndOfFile;
054    }
055
056    /**
057     * Closes the stream. Also closes the underlying stream.
058     *
059     * @throws IOException upon error
060     */
061    @Override
062    public void close() throws IOException {
063        super.close();
064        in.close();
065    }
066
067    /**
068     * Handles the end of stream condition.
069     *
070     * @return The next char to output to the stream
071     */
072    private int handleEos() {
073        if (!lineFeedAtEndOfFile) {
074            return EOF;
075        }
076        if (!atSlashLf && !atSlashCr) {
077            atSlashCr = true;
078            return CR;
079        }
080        if (!atSlashLf) {
081            atSlashCr = false;
082            atSlashLf = true;
083            return LF;
084        }
085        return EOF;
086    }
087
088    /**
089     * {@inheritDoc}
090     */
091    @Override
092    public synchronized void mark(final int readLimit) {
093        throw UnsupportedOperationExceptions.mark();
094    }
095
096    /**
097     * {@inheritDoc}
098     */
099    @Override
100    public int read() throws IOException {
101        if (atEos) {
102            return handleEos();
103        }
104        if (injectSlashLf) {
105            injectSlashLf = false;
106            return LF;
107        }
108        final boolean prevWasSlashR = atSlashCr;
109        final int target = readWithUpdate();
110        if (atEos) {
111            return handleEos();
112        }
113        if (target == LF && !prevWasSlashR) {
114            injectSlashLf = true;
115            return CR;
116        }
117        return target;
118    }
119
120    /**
121     * Reads the next item from the target, updating internal flags in the process
122     * @return the next int read from the target stream
123     * @throws IOException upon error
124     */
125    private int readWithUpdate() throws IOException {
126        final int target = this.in.read();
127        atEos = target == EOF;
128        if (atEos) {
129            return target;
130        }
131        atSlashCr = target == CR;
132        atSlashLf = target == LF;
133        return target;
134    }
135}