You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by kr...@apache.org on 2015/04/14 18:10:27 UTC
svn commit: r1673459 - in /commons/proper/io/trunk/src:
main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java
test/java/org/apache/commons/io/input/UnixLineEndingInputStreamTest.java
Author: krosenvold
Date: Tue Apr 14 16:10:27 2015
New Revision: 1673459
URL: http://svn.apache.org/r1673459
Log:
MASSEMBLY-753 CR only handling. Unreleased code so no separate issue for commons
According to http://en.wikipedia.org/wiki/Newline#Representations we're
probably be talking about files originating on old v9 MacOS'es or a Commodore 64
Modified:
commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java
commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/UnixLineEndingInputStreamTest.java
Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java?rev=1673459&r1=1673458&r2=1673459&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java Tue Apr 14 16:10:27 2015
@@ -29,6 +29,8 @@ public class UnixLineEndingInputStream e
private boolean slashNSeen = false;
+ private boolean slashRSeen = false;
+
private boolean eofSeen = false;
private final InputStream target;
@@ -53,6 +55,7 @@ public class UnixLineEndingInputStream e
return target;
}
slashNSeen = target == '\n';
+ slashRSeen = target == '\r';
return target;
}
@@ -62,23 +65,30 @@ public class UnixLineEndingInputStream e
@Override
public int read() throws IOException {
+ boolean previousWasSlashR = slashRSeen;
if ( eofSeen ) {
- return eofGame();
+ return eofGame(previousWasSlashR);
}
else {
int target = readWithUpdate();
if ( eofSeen ) {
- return eofGame();
+ return eofGame(previousWasSlashR);
+ }
+ if (slashRSeen)
+ {
+ return '\n';
}
- if ( target == '\r' ) {
- target = readWithUpdate();
+
+ if ( previousWasSlashR && slashNSeen){
+ return read();
}
+
return target;
}
}
- private int eofGame() {
- if ( !ensureLineFeedAtEndOfFile ) {
+ private int eofGame(boolean previousWasSlashR) {
+ if ( previousWasSlashR || !ensureLineFeedAtEndOfFile ) {
return -1;
}
if ( !slashNSeen ) {
Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/UnixLineEndingInputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/UnixLineEndingInputStreamTest.java?rev=1673459&r1=1673458&r2=1673459&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/UnixLineEndingInputStreamTest.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/UnixLineEndingInputStreamTest.java Tue Apr 14 16:10:27 2015
@@ -47,11 +47,26 @@ public class UnixLineEndingInputStreamTe
}
@Test
- public void malformed() throws Exception {
- assertEquals( "abc", roundtrip( "a\rbc", false ) );
+ public void crOnlyEnsureAtEof()
+ throws Exception
+ {
+ assertEquals( "a\nb\n", roundtrip( "a\rb" ) );
}
@Test
+ public void crOnlyNotAtEof()
+ throws Exception
+ {
+ assertEquals( "a\nb", roundtrip( "a\rb", false ) );
+ }
+
+ @Test
+ public void crAtEnd() throws Exception {
+ assertEquals( "a\n", roundtrip( "a\r" ) );
+ }
+
+
+ @Test
public void retainLineFeed() throws Exception {
assertEquals( "a\n\n", roundtrip( "a\r\n\r\n", false ) );
assertEquals( "a", roundtrip( "a", false ) );