Uploaded image for project: 'Commons IO'
  1. Commons IO
  2. IO-428

BOMInputStream.skip returns wrong count if stream contains no BOM

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.0.1
    • 2.5
    • Streams/Writers
    • None

    Description

      If the skip method of BOMInputStream is used on a stream without a BOM, skip returns the wrong number of bytes (n - max(BOM-length)). This can lead to problems if the return value is evaluated for example from guava ByteStreams.skipFully.

      BomTest.java
      public class BomTest {
      
      	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());
      	}
      	
      	public static void main(String[] args) 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());
      	}
      	
      }
      

      I catched this bug in 2.0.1, but as far as I can see on the source 2.5 is still affected.

      I suggest the following change to the skip method:

      BOMInputStream.java
          public long skip(long n) throws IOException {
          	int skipped = 0;
              while ((n > skipped) && (readFirstBytes() >= 0)) {
                  skipped++;
              }
              return in.skip(n - skipped) + skipped;
          }
      

      Attachments

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            krosenvold Kristian Rosenvold
            steff Stefan Gmeiner
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment