pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r1558585 - in /pdfbox/branches/1.8: ./ pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBuffer.java
Date Wed, 15 Jan 2014 22:02:58 GMT
Author: lehmi
Date: Wed Jan 15 22:02:58 2014
New Revision: 1558585

URL: http://svn.apache.org/r1558585
Log:
merged the changes of PDFBOX-1625

Modified:
    pdfbox/branches/1.8/   (props changed)
    pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java
    pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBuffer.java

Propchange: pdfbox/branches/1.8/
------------------------------------------------------------------------------
  Merged /pdfbox/trunk:r1509187,1558570

Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java?rev=1558585&r1=1558584&r2=1558585&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java Wed Jan
15 22:02:58 2014
@@ -28,13 +28,10 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.pdfbox.filter.Filter;
 import org.apache.pdfbox.filter.FilterManager;
+import org.apache.pdfbox.io.*;
 import org.apache.pdfbox.pdfparser.PDFStreamParser;
 import org.apache.pdfbox.exceptions.COSVisitorException;
 import org.apache.pdfbox.io.IOUtils;
-import org.apache.pdfbox.io.RandomAccess;
-import org.apache.pdfbox.io.RandomAccessFileInputStream;
-import org.apache.pdfbox.io.RandomAccessFileOutputStream;
-
 /**
  * This class represents a stream object in a PDF document.
  *
@@ -61,6 +58,16 @@ public class COSStream extends COSDictio
      */
     private RandomAccessFileOutputStream unFilteredStream;
 
+    private RandomAccess clone (RandomAccess file) {
+        if (file == null) {
+            return null;
+        } else if (file instanceof RandomAccessFile) {
+            return file;
+        } else {
+            return ((RandomAccessBuffer)file).clone();
+        }
+    }
+
     /**
      * Constructor.  Creates a new stream with an empty dictionary.
      *
@@ -69,7 +76,7 @@ public class COSStream extends COSDictio
     public COSStream( RandomAccess storage )
     {
         super();
-        file = storage;
+        file = clone(storage);
     }
 
     /**
@@ -81,7 +88,7 @@ public class COSStream extends COSDictio
     public COSStream( COSDictionary dictionary, RandomAccess storage )
     {
         super( dictionary );
-        file = storage;
+        file = clone(storage);
     }
 
     /**

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=1558585&r1=1558584&r2=1558585&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
Wed Jan 15 22:02:58 2014
@@ -59,6 +59,30 @@ public class RandomAccessBuffer implemen
         bufferListMaxIndex = 0;
     }
 
+    @Override
+    public RandomAccessBuffer clone() {
+        RandomAccessBuffer copy = new RandomAccessBuffer();
+
+        copy.bufferList = new ArrayList<byte[]>(bufferList.size());
+        for (byte [] buffer : bufferList) {
+            byte [] newBuffer = new byte [buffer.length];
+            System.arraycopy(buffer,0,newBuffer,0,buffer.length);
+            copy.bufferList.add(newBuffer);
+        }
+        if (currentBuffer!=null) {
+            copy.currentBuffer = copy.bufferList.get(copy.bufferList.size()-1);
+        } else {
+            copy.currentBuffer = null;
+        }
+        copy.pointer = pointer;
+        copy.currentBufferPointer = currentBufferPointer;
+        copy.size = size;
+        copy.bufferListIndex = bufferListIndex;
+        copy.bufferListMaxIndex = bufferListMaxIndex;
+
+        return copy;
+    }
+
     /**
      * {@inheritDoc}
      */
@@ -77,6 +101,7 @@ public class RandomAccessBuffer implemen
      */
     public void seek(long position) throws IOException
     {
+        checkClosed();
         pointer = position;
         // calculate the chunk list index
         bufferListIndex = (int)(position / BUFFER_SIZE);
@@ -88,7 +113,8 @@ public class RandomAccessBuffer implemen
      * {@inheritDoc}
      */
     public long getPosition() throws IOException {
-        return pointer;
+       checkClosed();
+       return pointer;
     }
     
     /**
@@ -96,6 +122,7 @@ public class RandomAccessBuffer implemen
      */
     public int read() throws IOException
     {
+        checkClosed();
         if (pointer >= this.size)
         {
             return -1;
@@ -121,6 +148,7 @@ public class RandomAccessBuffer implemen
      */
     public int read(byte[] b, int offset, int length) throws IOException
     {
+        checkClosed();
         if (pointer >= this.size)
         {
             return 0;
@@ -164,6 +192,7 @@ public class RandomAccessBuffer implemen
      */
     public long length() throws IOException
     {
+        checkClosed();
         return size;
     }
 
@@ -172,6 +201,7 @@ public class RandomAccessBuffer implemen
      */
     public void write(int b) throws IOException
     {
+        checkClosed();
         // end of buffer reached?
         if (currentBufferPointer >= BUFFER_SIZE) 
         {
@@ -203,6 +233,7 @@ public class RandomAccessBuffer implemen
      */
     public void write(byte[] b, int offset, int length) throws IOException
     {
+        checkClosed();
         long newSize = pointer + length;
         long remainingBytes = BUFFER_SIZE - currentBufferPointer;
         if (length >= remainingBytes)
@@ -276,4 +307,16 @@ public class RandomAccessBuffer implemen
         currentBufferPointer = 0;
         currentBuffer = bufferList.get(++bufferListIndex);
     }
+    
+    /**
+     * Ensure that the RandomAccessBuffer is not closed
+     * @throws IOException
+     */
+    private void checkClosed () throws IOException {
+        if (currentBuffer==null) {
+            // consider that the rab is closed if there is no current buffer
+            throw new IOException("RandomAccessBuffer already closed");
+        }
+        
+    }
 }



Mime
View raw message