Author: krosenvold
Date: Wed Oct 15 17:59:14 2014
New Revision: 1632145

URL: http://svn.apache.org/r1632145
Log:
[MSHARED-325] Fixed bug, fix by me.

Added:
    
maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/BoundedReader.java
    
maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/BoundedReaderTest.java
Modified:
    
maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/MultiDelimiterInterpolatorFilterReaderLineEnding.java

Added: 
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/BoundedReader.java?rev=1632145&view=auto
==============================================================================
--- 
maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/BoundedReader.java
 (added)
+++ 
maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/BoundedReader.java
 Wed Oct 15 17:59:14 2014
@@ -0,0 +1,76 @@
+/*
+ * 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.
+ */
+package org.apache.maven.shared.filtering;
+
+import java.io.IOException;
+import java.io.Reader;
+
+/**
+ * A reader that imposes a limit to the number of bytes that can be read from
+ * an underlying reader, simulating eof when this limit is reached.
+ *
+ * This stream can typically be used to constrain a client with regard to a 
readAheadLimit
+ * of an underlying stream, to avoid overrunning this limit and hence
+ * lose the opportunity do to reset.
+ */
+public class BoundedReader extends Reader {
+
+       private final Reader target;
+
+       int pos = 0;
+
+       int readAheadLimit;
+
+       public BoundedReader(Reader target, int readAheadLimit) throws 
IOException {
+               this.target = target;
+               target.mark(readAheadLimit);
+               this.readAheadLimit = readAheadLimit;
+       }
+
+
+       @Override public void close() throws IOException {
+               target.close();
+       }
+
+       @Override public void reset() throws IOException {
+               pos = 0;
+               target.reset();
+       }
+
+       @Override public void mark(int readAheadLimit) throws IOException {
+               this.readAheadLimit = readAheadLimit;
+               target.mark(readAheadLimit);
+       }
+
+       @Override public int read() throws IOException {
+               if (pos >= readAheadLimit) return -1;
+               pos++;
+               return target.read();
+       }
+
+       @Override public int read(char[] cbuf, int off, int len) throws 
IOException{
+               int c;
+               for (int i = 0; i < len; i++){
+                       c = read();
+                       if (c == -1) return i;
+                       cbuf[off + i] = (char) c;
+               }
+               return len;
+       }
+}

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=1632145&r1=1632144&r2=1632145&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
 Wed Oct 15 17:59:14 2014
@@ -97,7 +97,9 @@ public class MultiDelimiterInterpolatorF
     /**
      * must always be bigger than escape string plus delimiters, but doesn't 
need to be exact
      */
-    private int markLength = 16;
+    private int markLength = 128;
+
+    private static final int maxBufferSize = 8192;
 
     private boolean eof = false;
 
@@ -125,7 +127,7 @@ public class MultiDelimiterInterpolatorF
                                                              boolean 
supportMultiLineFiltering )
     {
         // wrap our own buffer, so we can use mark/reset safely.
-        super( new BufferedReader( in ) );
+        super( new BufferedReader( in, maxBufferSize ) );
 
         this.interpolator = interpolator;
 
@@ -142,7 +144,6 @@ public class MultiDelimiterInterpolatorF
 
     }
 
-
     public boolean removeDelimiterSpec( String delimiterSpec )
     {
         return delimiters.remove( DelimiterSpecification.parse( delimiterSpec 
) );
@@ -237,7 +238,7 @@ public class MultiDelimiterInterpolatorF
             return -1;
         }
 
-        in.mark( markLength );
+        BoundedReader in = new BoundedReader(this.in, markLength);
 
         int ch = in.read();
         if ( ( ch == -1 ) || ( ch == '\n' && !supportMultiLineFiltering ) )
@@ -344,7 +345,7 @@ public class MultiDelimiterInterpolatorF
 
         // we're committed, find the end token, EOL or EOF
 
-        key.append( beginToken );
+        key.append(beginToken);
         in.reset();
         in.skip( beginToken.length() );
         ch = in.read();
@@ -487,7 +488,7 @@ public class MultiDelimiterInterpolatorF
 
     private void calculateMarkLength()
     {
-        markLength = 16;
+        markLength = 128;
 
         if ( escapeString != null )
         {

Added: 
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=1632145&view=auto
==============================================================================
--- 
maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/BoundedReaderTest.java
 (added)
+++ 
maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/BoundedReaderTest.java
 Wed Oct 15 17:59:14 2014
@@ -0,0 +1,92 @@
+/*
+ * 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.
+ */
+package org.apache.maven.shared.filtering;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+
+import org.junit.Test;
+
+public class BoundedReaderTest  {
+
+
+       private final Reader sr = new BufferedReader(new 
StringReader("01234567890"));
+
+       @Test
+       public void readTillEnd() throws IOException {
+               BoundedReader mr = new BoundedReader(sr, 3);
+               mr.mark(3);
+               mr.read();
+               mr.read();
+               mr.read();
+               assertEquals(-1, mr.read());
+       }
+
+       @Test
+       public void readMulti() throws IOException {
+               BoundedReader mr = new BoundedReader(sr, 3);
+               char[] cbuf = new char[4];
+               for (int i= 0; i < cbuf.length; i++) cbuf[i] = 'X';
+               final int read = mr.read(cbuf, 0, 4);
+               assertEquals( 3, read);
+               assertEquals('0', cbuf[0]);
+               assertEquals('1', cbuf[1]);
+               assertEquals('2', cbuf[2]);
+               assertEquals('X', cbuf[3]);
+       }
+
+       @Test
+       public void readMultiWithOffset() throws IOException {
+               BoundedReader mr = new BoundedReader(sr, 3);
+               char[] cbuf = new char[4];
+               for (int i= 0; i < cbuf.length; i++) cbuf[i] = 'X';
+               final int read = mr.read(cbuf, 1, 2);
+               assertEquals( 2, read);
+               assertEquals('X', cbuf[0]);
+               assertEquals('0', cbuf[1]);
+               assertEquals('1', cbuf[2]);
+               assertEquals('X', cbuf[3]);
+       }
+
+       @Test
+       public void resetWorks() throws IOException {
+               BoundedReader mr = new BoundedReader(sr,3 );
+               mr.read();
+               mr.read();
+               mr.read();
+               mr.reset();
+               mr.read();
+               mr.read();
+               mr.read();
+               assertEquals(-1, mr.read());
+
+       }
+
+       @Test
+       public void skipTest() throws IOException {
+               BoundedReader mr = new BoundedReader(sr, 3);
+               mr.skip(2);
+               mr.read();
+               assertEquals(-1, mr.read());
+       }
+}
\ No newline at end of file


Reply via email to