Author: krosenvold
Date: Sat Jun 20 06:31:39 2015
New Revision: 1686527

URL: http://svn.apache.org/r1686527
Log:
IO-428 BOMInputStream.skip returns wrong count if stream contains no BOM

Patch by Stefan Gmeiner, applied with minor changes

Modified:
    
commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/BOMInputStream.java
    
commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/BOMInputStreamTest.java

Modified: 
commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/BOMInputStream.java
URL: 
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/BOMInputStream.java?rev=1686527&r1=1686526&r2=1686527&view=diff
==============================================================================
--- 
commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/BOMInputStream.java
 (original)
+++ 
commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/BOMInputStream.java
 Sat Jun 20 06:31:39 2015
@@ -399,9 +399,10 @@ public class BOMInputStream extends Prox
      */
     @Override
     public long skip(long n) throws IOException {
-        while (n > 0 && readFirstBytes() >= 0) {
-            n--;
+        int skipped = 0;
+        while ((n > skipped) && (readFirstBytes() >= 0)) {
+            skipped++;
         }
-        return in.skip(n);
+        return in.skip(n - skipped) + skipped;
     }
 }

Modified: 
commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/BOMInputStreamTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/BOMInputStreamTest.java?rev=1686527&r1=1686526&r2=1686527&view=diff
==============================================================================
--- 
commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/BOMInputStreamTest.java
 (original)
+++ 
commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/BOMInputStreamTest.java
 Sat Jun 20 06:31:39 2015
@@ -16,22 +16,15 @@
  */
 package org.apache.commons.io.input;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.*;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.Reader;
+import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
 
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-
 import org.apache.commons.io.ByteOrderMark;
 import org.apache.commons.io.Charsets;
 import org.junit.Assert;
@@ -41,6 +34,9 @@ import org.w3c.dom.Document;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
 /**
  * Test case for {@link BOMInputStream}.
  *
@@ -385,6 +381,31 @@ public class BOMInputStreamTest {
         }
     }
 
+    private static InputStream createInputStream(boolean addBOM) {
+        ByteBuffer bb = ByteBuffer.allocate(64);
+        if (addBOM) {
+            // UTF-8 BOM
+            bb.put(new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF});
+        }
+        bb.put((byte) 0x31);
+        bb.put((byte) 0x32);
+        bb.put((byte) 0x33);
+        return new ByteArrayInputStream(bb.array());
+    }
+
+    @Test
+    public void lengthWithNoBOM() throws IOException {
+        BOMInputStream is1 = new BOMInputStream(createInputStream(true));
+        assertEquals(2, is1.skip(2));
+        assertEquals((byte) 0x33, is1.read());
+
+        BOMInputStream is2 = new BOMInputStream(createInputStream(false));
+        assertEquals(2, is2.skip(2)); // fails here - skip returns 0
+        assertEquals((byte) 0x33, is2.read());
+    }
+
+
+
     @Test
     public void testReadEmpty() throws Exception {
         final byte[] data = new byte[] {};


Reply via email to