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 *      https://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 AbstractLineEndingInputStream {
032
033    private boolean injectSlashLf;
034
035    /**
036     * Constructs an input stream that filters another stream.
037     *
038     * @param in                        The input stream to wrap.
039     * @param lineFeedAtEos true to ensure that the stream ends with CRLF.
040     */
041    public WindowsLineEndingInputStream(final InputStream in, final boolean lineFeedAtEos) {
042        super(in, lineFeedAtEos);
043    }
044
045    /**
046     * Handles the end of stream condition.
047     *
048     * @return The next char to output to the stream.
049     */
050    private int handleEos() {
051        if (!lineFeedAtEos) {
052            return EOF;
053        }
054        if (!atSlashLf && !atSlashCr) {
055            atSlashCr = true;
056            return CR;
057        }
058        if (!atSlashLf) {
059            atSlashCr = false;
060            atSlashLf = true;
061            return LF;
062        }
063        return EOF;
064    }
065
066    /**
067     * {@inheritDoc}
068     */
069    @Override
070    public synchronized int read() throws IOException {
071        if (atEos) {
072            return handleEos();
073        }
074        if (injectSlashLf) {
075            injectSlashLf = false;
076            return LF;
077        }
078        final boolean prevWasSlashR = atSlashCr;
079        final int target = in.read();
080        atEos = target == EOF;
081        if (!atEos) {
082            atSlashCr = target == CR;
083            atSlashLf = target == LF;
084        }
085        if (atEos) {
086            return handleEos();
087        }
088        if (target == LF && !prevWasSlashR) {
089            injectSlashLf = true;
090            return CR;
091        }
092        return target;
093    }
094}