Author: niallp
Date: Mon Oct  4 02:33:36 2010
New Revision: 1004087

URL: http://svn.apache.org/viewvc?rev=1004087&view=rev
Log:
IO-178 Fix Bug with firstBytes being consumed by getBOM()

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

Modified: 
commons/proper/io/trunk/src/java/org/apache/commons/io/input/BOMInputStream.java
URL: 
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/input/BOMInputStream.java?rev=1004087&r1=1004086&r2=1004087&view=diff
==============================================================================
--- 
commons/proper/io/trunk/src/java/org/apache/commons/io/input/BOMInputStream.java
 (original)
+++ 
commons/proper/io/trunk/src/java/org/apache/commons/io/input/BOMInputStream.java
 Mon Oct  4 02:33:36 2010
@@ -156,19 +156,6 @@ public class BOMInputStream extends Prox
      * @throws IOException if an error reading the first bytes of the stream 
occurs
      */
     public ByteOrderMark getBOM() throws IOException {
-        readFirstBytes();
-        return byteOrderMark;
-    }
-
-    /**
-     * This method reads and either preserves or skips the first bytes in the
-     * stream. It behaves like the single-byte <code>read()</code> method,
-     * either returning a valid byte or -1 to indicate that the initial bytes
-     * have been processed already.
-     * @return the byte read (excluding BOM) or -1 if the end of stream
-     * @throws IOException if an I/O error occurs
-     */
-    private int readFirstBytes() throws IOException {
         if (firstBytes == null) {
             int max = 0;
             for (ByteOrderMark bom : boms) {
@@ -191,7 +178,19 @@ public class BOMInputStream extends Prox
                 }
             }
         }
+        return byteOrderMark;
+    }
 
+    /**
+     * This method reads and either preserves or skips the first bytes in the
+     * stream. It behaves like the single-byte <code>read()</code> method,
+     * either returning a valid byte or -1 to indicate that the initial bytes
+     * have been processed already.
+     * @return the byte read (excluding BOM) or -1 if the end of stream
+     * @throws IOException if an I/O error occurs
+     */
+    private int readFirstBytes() throws IOException {
+        getBOM();
         return (fbIndex < fbLength) ? firstBytes[fbIndex++] : -1;
     }
 

Modified: 
commons/proper/io/trunk/src/test/org/apache/commons/io/input/BOMInputStreamTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/input/BOMInputStreamTest.java?rev=1004087&r1=1004086&r2=1004087&view=diff
==============================================================================
--- 
commons/proper/io/trunk/src/test/org/apache/commons/io/input/BOMInputStreamTest.java
 (original)
+++ 
commons/proper/io/trunk/src/test/org/apache/commons/io/input/BOMInputStreamTest.java
 Mon Oct  4 02:33:36 2010
@@ -175,6 +175,21 @@ public class BOMInputStreamTest extends 
         assertEquals("getBOM", ByteOrderMark.UTF_8, in.getBOM());
     }
 
+    public void testGetBOMFirstThenReadInclude() throws Exception {
+        byte[] data = new byte[] { 'A', 'B', 'C' };
+        BOMInputStream in = new BOMInputStream(createDataStream(data, true), 
true);
+        assertTrue("hasBOM()", in.hasBOM());
+        assertTrue("hasBOM(UTF-8)", in.hasBOM(ByteOrderMark.UTF_8));
+        assertEquals("getBOM", ByteOrderMark.UTF_8, in.getBOM());
+        assertEquals(0xEF, in.read());
+        assertEquals(0xBB, in.read());
+        assertEquals(0xBF, in.read());
+        assertEquals('A', in.read());
+        assertEquals('B', in.read());
+        assertEquals('C', in.read());
+        assertEquals(-1, in.read());
+    }
+
     public void testReadWithMultipleBOM() throws Exception {
         byte[] data = new byte[] { 'A', 'B', 'C' };
         BOMInputStream in = new BOMInputStream(createDataStream(data, true), 


Reply via email to