diff options
author | Olivier Lamy <olamy@apache.org> | 2014-10-27 23:40:02 +0000 |
---|---|---|
committer | Olivier Lamy <olamy@apache.org> | 2014-10-27 23:40:02 +0000 |
commit | 19b55afedf54bfc9e3b08c1636251339b2be4c42 (patch) | |
tree | 905b0d2c810304b31840d1f1394686e838e7703e /src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java | |
parent | c9de74f5c44420e8d00ad0c37c3ba527ee6f8a1f (diff) | |
download | apache-commons-io-19b55afedf54bfc9e3b08c1636251339b2be4c42.tar.gz |
[IO-459] Add WindowsLineEndingInputStream and UnixLineEndingInputStream
Submitted by Kristian Rosenvold
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1634738 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java')
-rw-r--r-- | src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java b/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java new file mode 100644 index 00000000..b6ed27e8 --- /dev/null +++ b/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java @@ -0,0 +1,110 @@ +package org.apache.commons.io.input; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.IOException; +import java.io.InputStream; + +/** + * A filtering input stream that ensures the content will have unix-style line endings, LF. + * @since 2.5 + */ +public class UnixLineEndingInputStream + extends InputStream { + + private boolean slashNSeen = false; + + private boolean eofSeen = false; + + private final InputStream target; + + private final boolean ensureLineFeedAtEndOfFile; + + /** + * Create an input stream that filters another stream + * + * @param in The input stream to wrap + * @param ensureLineFeedAtEndOfFile true to ensure that the file ends with LF + */ + public UnixLineEndingInputStream(InputStream in, boolean ensureLineFeedAtEndOfFile) { + this.target = in; + this.ensureLineFeedAtEndOfFile = ensureLineFeedAtEndOfFile; + } + + private int readWithUpdate() throws IOException { + final int target = this.target.read(); + eofSeen = target == -1; + if (eofSeen) { + return target; + } + slashNSeen = target == '\n'; + return target; + } + + /** + * @inheritDoc + */ + + @Override public int read() + throws IOException { + if (eofSeen) { + return eofGame(); + } else { + int target = readWithUpdate(); + if (eofSeen) { + return eofGame(); + } + if (target == '\r') { + target = readWithUpdate(); + } + return target; + } + } + + private int eofGame() { + if (!ensureLineFeedAtEndOfFile) { + return -1; + } + if (!slashNSeen) { + slashNSeen = true; + return '\n'; + } else { + return -1; + } + } + + /** + * Closes the stream. Also closes the underlying stream. + */ + @Override + public void close() + throws IOException { + super.close(); + target.close(); + } + + /** + * @inheritDoc + */ + @Override + public synchronized void mark(int readlimit) { + throw new UnsupportedOperationException("Mark notsupported"); + } +} |