Author: krosenvold Date: Sat Oct 25 07:09:38 2014 New Revision: 1634185 URL: http://svn.apache.org/r1634185 Log: Added inputstreams that convert to windows or linux line feeds. Taken from maven-assembly-plugin
Added: maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/streams/ maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/streams/BoundedReader.java - copied, changed from r1633997, maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/BoundedReader.java maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/streams/LinuxLineFeedInputStream.java maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/streams/WindowsLineFeedInputStream.java maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/streams/ maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/streams/LinuxLineFeedInputStreamTest.java maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/streams/WindowsLineFeedInputStreamTest.java Removed: maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/BoundedReader.java Modified: maven/shared/trunk/maven-filtering/pom.xml maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/MultiDelimiterInterpolatorFilterReaderLineEnding.java maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/BoundedReaderTest.java Modified: maven/shared/trunk/maven-filtering/pom.xml URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-filtering/pom.xml?rev=1634185&r1=1634184&r2=1634185&view=diff ============================================================================== --- maven/shared/trunk/maven-filtering/pom.xml (original) +++ maven/shared/trunk/maven-filtering/pom.xml Sat Oct 25 07:09:38 2014 @@ -129,7 +129,6 @@ <scope>test</scope> <classifier>tests</classifier> </dependency> - <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> Modified: maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/MultiDelimiterInterpolatorFilterReaderLineEnding.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/MultiDelimiterInterpolatorFilterReaderLineEnding.java?rev=1634185&r1=1634184&r2=1634185&view=diff ============================================================================== --- maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/MultiDelimiterInterpolatorFilterReaderLineEnding.java (original) +++ maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/MultiDelimiterInterpolatorFilterReaderLineEnding.java Sat Oct 25 07:09:38 2014 @@ -26,6 +26,7 @@ import java.io.Reader; import java.util.LinkedHashSet; import java.util.Set; +import org.apache.maven.shared.filtering.streams.BoundedReader; import org.codehaus.plexus.interpolation.InterpolationException; import org.codehaus.plexus.interpolation.Interpolator; import org.codehaus.plexus.interpolation.RecursionInterceptor; Copied: maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/streams/BoundedReader.java (from r1633997, maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/BoundedReader.java) URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/streams/BoundedReader.java?p2=maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/streams/BoundedReader.java&p1=maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/BoundedReader.java&r1=1633997&r2=1634185&rev=1634185&view=diff ============================================================================== --- maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/BoundedReader.java (original) +++ maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/streams/BoundedReader.java Sat Oct 25 07:09:38 2014 @@ -1,4 +1,4 @@ -package org.apache.maven.shared.filtering; +package org.apache.maven.shared.filtering.streams; /* * Licensed to the Apache Software Foundation (ASF) under one @@ -19,6 +19,7 @@ package org.apache.maven.shared.filterin * under the License. */ +import javax.annotation.Nonnull; import java.io.IOException; import java.io.Reader; @@ -83,7 +84,7 @@ public class BoundedReader } @Override - public int read( char[] cbuf, int off, int len ) + public int read( @Nonnull char[] cbuf, int off, int len ) throws IOException { int c; Added: maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/streams/LinuxLineFeedInputStream.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/streams/LinuxLineFeedInputStream.java?rev=1634185&view=auto ============================================================================== --- maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/streams/LinuxLineFeedInputStream.java (added) +++ maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/streams/LinuxLineFeedInputStream.java Sat Oct 25 07:09:38 2014 @@ -0,0 +1,109 @@ +package org.apache.maven.shared.filtering.streams; + +/* + * 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; + +/** + * @author Kristian Rosenvold + */ +class LinuxLineFeedInputStream + extends InputStream +{ + + private boolean slashNSeen = false; + + private boolean eofSeen = false; + + private final InputStream target; + + private final boolean ensureLineFeedAtEndOfFile; + + public LinuxLineFeedInputStream( 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; + } + + @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; + } + } + + @Override public void close() + throws IOException + { + super.close(); + target.close(); + } + + @Override public synchronized void mark( int readlimit ) + { + throw new UnsupportedOperationException( "Mark not implemented yet" ); + } +} Added: maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/streams/WindowsLineFeedInputStream.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/streams/WindowsLineFeedInputStream.java?rev=1634185&view=auto ============================================================================== --- maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/streams/WindowsLineFeedInputStream.java (added) +++ maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/streams/WindowsLineFeedInputStream.java Sat Oct 25 07:09:38 2014 @@ -0,0 +1,130 @@ +package org.apache.maven.shared.filtering.streams; + +/* + * 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; + +/** + * @author Kristian Rosenvold + */ +class WindowsLineFeedInputStream + extends InputStream +{ + + private boolean slashRSeen = false; + + private boolean slashNSeen = false; + + private boolean injectSlashN = false; + + private boolean eofSeen = false; + + private final InputStream target; + + private final boolean ensureLineFeedAtEndOfFile; + + public WindowsLineFeedInputStream( 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; + } + slashRSeen = target == '\r'; + slashNSeen = target == '\n'; + return target; + } + + @Override public int read() + throws IOException + { + if ( eofSeen ) + { + return eofGame(); + } + else if ( injectSlashN ) + { + injectSlashN = false; + return '\n'; + } + else + { + boolean prevWasSlashR = slashRSeen; + int target = readWithUpdate(); + if ( eofSeen ) + { + return eofGame(); + } + if ( target == '\n' ) + { + if ( !prevWasSlashR ) + { + injectSlashN = true; + return '\r'; + } + } + return target; + } + } + + private int eofGame() + { + if ( !ensureLineFeedAtEndOfFile ) + { + return -1; + } + if ( !slashNSeen && !slashRSeen ) + { + slashRSeen = true; + return '\r'; + } + if ( !slashNSeen ) + { + slashRSeen = false; + slashNSeen = true; + return '\n'; + } + else + { + return -1; + } + } + + @Override public void close() + throws IOException + { + super.close(); + target.close(); + } + + @Override public synchronized void mark( int readlimit ) + { + throw new UnsupportedOperationException( "Mark not implemented yet" ); + } +} Modified: maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/BoundedReaderTest.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/BoundedReaderTest.java?rev=1634185&r1=1634184&r2=1634185&view=diff ============================================================================== --- maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/BoundedReaderTest.java (original) +++ maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/BoundedReaderTest.java Sat Oct 25 07:09:38 2014 @@ -25,6 +25,7 @@ import java.io.IOException; import java.io.Reader; import java.io.StringReader; +import org.apache.maven.shared.filtering.streams.BoundedReader; import org.junit.Test; public class BoundedReaderTest Added: maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/streams/LinuxLineFeedInputStreamTest.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/streams/LinuxLineFeedInputStreamTest.java?rev=1634185&view=auto ============================================================================== --- maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/streams/LinuxLineFeedInputStreamTest.java (added) +++ maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/streams/LinuxLineFeedInputStreamTest.java Sat Oct 25 07:09:38 2014 @@ -0,0 +1,90 @@ +package org.apache.maven.shared.filtering.streams; +/* + * 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 org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import static org.junit.Assert.assertEquals; + +public class LinuxLineFeedInputStreamTest +{ + + @Test + public void simpleString() + throws Exception + { + assertEquals( "abc\n", roundtrip( "abc" ) ); + } + + @Test + public void inTheMiddleOfTheLine() + throws Exception + { + assertEquals( "a\nbc\n", roundtrip( "a\r\nbc" ) ); + } + + @Test + public void multipleBlankLines() + throws Exception + { + assertEquals( "a\n\nbc\n", roundtrip( "a\r\n\r\nbc" ) ); + } + + @Test + public void twoLinesAtEnd() + throws Exception + { + assertEquals( "a\n\n", roundtrip( "a\r\n\r\n" ) ); + } + + @Test + public void malformed() + throws Exception + { + assertEquals( "abc", roundtrip( "a\rbc", false ) ); + } + + @Test + public void retainLineFeed() + throws Exception + { + assertEquals( "a\n\n", roundtrip( "a\r\n\r\n", false ) ); + assertEquals( "a", roundtrip( "a", false ) ); + } + + private String roundtrip( String msg ) + throws IOException + { + return roundtrip( msg, true ); + } + + private String roundtrip( String msg, boolean ensure ) + throws IOException + { + ByteArrayInputStream baos = new ByteArrayInputStream( msg.getBytes() ); + LinuxLineFeedInputStream lf = new LinuxLineFeedInputStream( baos, ensure ); + byte[] buf = new byte[100]; + final int read = lf.read( buf ); + return new String( buf, 0, read ); + } + +} \ No newline at end of file Added: maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/streams/WindowsLineFeedInputStreamTest.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/streams/WindowsLineFeedInputStreamTest.java?rev=1634185&view=auto ============================================================================== --- maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/streams/WindowsLineFeedInputStreamTest.java (added) +++ maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/streams/WindowsLineFeedInputStreamTest.java Sat Oct 25 07:09:38 2014 @@ -0,0 +1,100 @@ +package org.apache.maven.shared.filtering.streams; +/* + * 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 org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import static org.junit.Assert.assertEquals; + +public class WindowsLineFeedInputStreamTest +{ + + @Test + public void simpleString() + throws Exception + { + assertEquals( "abc\r\n", roundtrip( "abc" ) ); + } + + @Test + public void inTheMiddleOfTheLine() + throws Exception + { + assertEquals( "a\r\nbc\r\n", roundtrip( "a\r\nbc" ) ); + } + + @Test + public void multipleBlankLines() + throws Exception + { + assertEquals( "a\r\n\r\nbc\r\n", roundtrip( "a\r\n\r\nbc" ) ); + } + + @Test + public void twoLinesAtEnd() + throws Exception + { + assertEquals( "a\r\n\r\n", roundtrip( "a\r\n\r\n" ) ); + } + + @Test + public void linuxLinefeeds() + throws Exception + { + final String roundtrip = roundtrip( "ab\nc", false ); + assertEquals( "ab\r\nc", roundtrip ); + } + + + @Test + public void malformed() + throws Exception + { + assertEquals( "a\rbc", roundtrip( "a\rbc", false ) ); + } + + @Test + public void retainLineFeed() + throws Exception + { + assertEquals( "a\r\n\r\n", roundtrip( "a\r\n\r\n", false ) ); + assertEquals( "a", roundtrip( "a", false ) ); + } + + private String roundtrip( String msg ) + throws IOException + { + return roundtrip( msg, true ); + } + + private String roundtrip( String msg, boolean ensure ) + throws IOException + { + ByteArrayInputStream baos = new ByteArrayInputStream( msg.getBytes() ); + WindowsLineFeedInputStream lf = new WindowsLineFeedInputStream( baos, ensure ); + byte[] buf = new byte[100]; + final int read = lf.read( buf ); + return new String( buf, 0, read ); + } + + +} \ No newline at end of file