pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r1681608 - in /pdfbox/branches/1.8/pdfbox/src: main/java/org/apache/pdfbox/io/RandomAccessBuffer.java test/java/org/apache/pdfbox/io/TestRandomAccessBuffer.java
Date Mon, 25 May 2015 16:25:20 GMT
Author: lehmi
Date: Mon May 25 16:25:20 2015
New Revision: 1681608

URL: http://svn.apache.org/r1681608
Log:
PDFBOX-2811: fixed calculation of currentBufferPointer to avoid infinite loop in a specific
corner case, added junit test

Modified:
    pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBuffer.java
    pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/io/TestRandomAccessBuffer.java

Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBuffer.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBuffer.java?rev=1681608&r1=1681607&r2=1681608&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBuffer.java
(original)
+++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBuffer.java
Mon May 25 16:25:20 2015
@@ -156,10 +156,16 @@ public class RandomAccessBuffer implemen
         }
         int maxLength = (int) Math.min(length, this.size-pointer);
         long remainingBytes = BUFFER_SIZE - currentBufferPointer;
+        if (remainingBytes == 0)
+        {
+            nextBuffer();
+            remainingBytes = BUFFER_SIZE;
+        }
         if (maxLength >= remainingBytes)
         {
             // copy the first bytes from the current buffer
             System.arraycopy(currentBuffer, (int)currentBufferPointer, b, offset, (int)remainingBytes);
+            currentBufferPointer += remainingBytes;
             int newOffset = offset + (int)remainingBytes;
             long remainingBytes2Read = length - remainingBytes;
             // determine how many buffers are needed to get the remaining amount bytes

Modified: pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/io/TestRandomAccessBuffer.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/io/TestRandomAccessBuffer.java?rev=1681608&r1=1681607&r2=1681608&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/io/TestRandomAccessBuffer.java
(original)
+++ pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/io/TestRandomAccessBuffer.java
Mon May 25 16:25:20 2015
@@ -31,6 +31,31 @@ public class TestRandomAccessBuffer exte
     private static final int BUFFER_SIZE = 16384;
     
     /**
+     * This test checks a corner case where the last read ends
+     * exactly at the end of a chunck (remainingBytes == 0)
+     * @throws IOException
+     */
+    public void testRemainingByteZero() throws IOException
+    {
+        RandomAccessBuffer buffer = new RandomAccessBuffer();
+        byte[] byteArray = new byte[BUFFER_SIZE + 2];
+        // fill the second chunk with "1"
+        for (int i = 0; i < 2; i++)
+        {
+            byteArray[BUFFER_SIZE + i] = 1;
+        }
+        buffer.write(byteArray, 0, byteArray.length);
+        buffer.seek(BUFFER_SIZE - 2);
+        // read the last bytes of the first chunk
+        buffer.read(byteArray, 0, 2);
+        // read the last 2 bytes of the buffer/the first bytes of the second chunk
+        buffer.read(byteArray, 0, 2);
+        // check the values read from the second chunk
+        assertEquals(2, byteArray[0]+byteArray[1]);
+        buffer.close();
+    }
+
+    /**
      * This will test the {@link RandomAccessBuffer#read()} 
      * and {@link RandomAccessBuffer#write(int)} method.
      * 
@@ -53,6 +78,7 @@ public class TestRandomAccessBuffer exte
             result += buffer.read();
         }
         assertEquals(45, result);
+        buffer.close();
     }
 
     /**
@@ -93,6 +119,7 @@ public class TestRandomAccessBuffer exte
             result += byteArray[i];
         }
         assertEquals(45, result);
+        buffer.close();
     }
 
     /**
@@ -155,6 +182,7 @@ public class TestRandomAccessBuffer exte
             result += byteArray[i];
         }
         assertEquals(15, result);
+        buffer.close();
     }
 
     /**
@@ -209,6 +237,7 @@ public class TestRandomAccessBuffer exte
             result += byteArray[i];
         }
         assertEquals(25, result);
+        buffer.close();
     }
     
     public void testPDFBOX1490() throws Exception
@@ -221,5 +250,6 @@ public class TestRandomAccessBuffer exte
         buffer.write(0);
         // seek the current == last position in the first buffer chunk
         buffer.seek(buffer.getPosition());
+        buffer.close();
     }
 }



Mime
View raw message