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 UNIX-style line endings, LF. 028 * 029 * @since 2.5 030 */ 031public class UnixLineEndingInputStream extends AbstractLineEndingInputStream { 032 033 /** 034 * Constructs an input stream that filters another stream 035 * 036 * @param inputStream The input stream to wrap. 037 * @param lineFeedAtEos true to ensure that the file ends with LF. 038 */ 039 public UnixLineEndingInputStream(final InputStream inputStream, final boolean lineFeedAtEos) { 040 super(inputStream, lineFeedAtEos); 041 } 042 043 /** 044 * Handles the end of stream condition. 045 * 046 * @param previousWasSlashCr Indicates if the last seen was a {@code \r}. 047 * @return The next char to output to the stream. 048 */ 049 private int handleEos(final boolean previousWasSlashCr) { 050 if (previousWasSlashCr || !lineFeedAtEos) { 051 return EOF; 052 } 053 if (!atSlashLf) { 054 atSlashLf = true; 055 return LF; 056 } 057 return EOF; 058 } 059 060 /** 061 * {@inheritDoc} 062 */ 063 @Override 064 public synchronized int read() throws IOException { 065 final boolean previousWasSlashR = atSlashCr; 066 if (atEos) { 067 return handleEos(previousWasSlashR); 068 } 069 final int target = readUpdate(); 070 if (atEos) { 071 return handleEos(previousWasSlashR); 072 } 073 if (atSlashCr) { 074 return LF; 075 } 076 077 if (previousWasSlashR && atSlashLf) { 078 return read(); 079 } 080 081 return target; 082 } 083 084 /** 085 * Reads the next item from the target, updating internal flags in the process 086 * 087 * @return the next int read from the target stream. 088 * @throws IOException If an I/O error occurs. 089 */ 090 private int readUpdate() throws IOException { 091 final int target = this.in.read(); 092 atEos = target == EOF; 093 if (atEos) { 094 return target; 095 } 096 atSlashCr = target == CR; 097 atSlashLf = target == LF; 098 return target; 099 } 100}