You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ja...@apache.org on 2018/05/10 05:27:19 UTC

svn commit: r1831304 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel: PDAbstractContentStream.java PDPageContentStream.java

Author: jahewson
Date: Thu May 10 05:27:19 2018
New Revision: 1831304

URL: http://svn.apache.org/viewvc?rev=1831304&view=rev
Log:
PDFBOX-4068: refactor: outputStream is only ever set within constructors, make it a constructor parameter

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDAbstractContentStream.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDAbstractContentStream.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDAbstractContentStream.java?rev=1831304&r1=1831303&r2=1831304&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDAbstractContentStream.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDAbstractContentStream.java Thu May 10 05:27:19 2018
@@ -55,7 +55,7 @@ import org.apache.pdfbox.util.NumberForm
  */
 abstract class PDAbstractContentStream implements Closeable
 {
-    private OutputStream outputStream;
+    protected final OutputStream outputStream;
     private PDResources resources;
 
     protected boolean inTextMode = false;
@@ -70,20 +70,10 @@ abstract class PDAbstractContentStream i
 
     /**
      * Create a new appearance stream.
-     *
-     */
-    public PDAbstractContentStream()
-    {
-        formatDecimal.setMaximumFractionDigits(4);
-        formatDecimal.setGroupingUsed(false);
-    }
-
-    /**
-     * Create a new appearance stream.
      * 
      * @param outputStream The appearances output stream to write to.
      */
-    public PDAbstractContentStream(OutputStream outputStream)
+    PDAbstractContentStream(OutputStream outputStream)
     {
         this.outputStream = outputStream;
         this.resources = null;
@@ -103,16 +93,6 @@ abstract class PDAbstractContentStream i
         formatDecimal.setMaximumFractionDigits(fractionDigitsNumber);
     }
 
-    public OutputStream getOutputStream()
-    {
-        return outputStream;
-    }
-
-    public void setOutputStream(OutputStream outputStream)
-    {
-        this.outputStream = outputStream;
-    }
-
     public PDResources getResources()
     {
         return resources;
@@ -1483,11 +1463,7 @@ abstract class PDAbstractContentStream i
     @Override
     public void close() throws IOException
     {
-        if (outputStream != null)
-        {
-            outputStream.close();
-            outputStream = null;
-        }
+        outputStream.close();
     }
 
     protected boolean isOutside255Interval(int val)

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java?rev=1831304&r1=1831303&r2=1831304&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageContentStream.java Thu May 10 05:27:19 2018
@@ -57,6 +57,7 @@ import org.apache.pdfbox.pdmodel.graphic
 import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
 import org.apache.pdfbox.pdmodel.graphics.state.RenderingMode;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream;
+import org.apache.pdfbox.util.Charsets;
 import org.apache.pdfbox.util.Matrix;
 
 /**
@@ -149,16 +150,18 @@ public final class PDPageContentStream e
     public PDPageContentStream(PDDocument document, PDPage sourcePage, AppendMode appendContent,
                                boolean compress, boolean resetContext) throws IOException
     {
-        super();
+        this(document, sourcePage, appendContent, compress, resetContext, new PDStream(document));
+    }
+
+    private PDPageContentStream(PDDocument document, PDPage sourcePage, AppendMode appendContent,
+                                boolean compress, boolean resetContext, PDStream stream) throws IOException
+    {
+        super(stream.createOutputStream(compress ? COSName.FLATE_DECODE : null));
         this.document = document;
-        COSName filter = compress ? COSName.FLATE_DECODE : null;
-        
+
         // If request specifies the need to append/prepend to the document
         if (!appendContent.isOverwrite() && sourcePage.hasContents())
         {
-            // Create a stream to append new content
-            PDStream contentsToAppend = new PDStream(document);
-            
             // Add new stream to contents array
             COSBase contents = sourcePage.getCOSObject().getDictionaryObject(COSName.CONTENTS);
             COSArray array;
@@ -173,35 +176,36 @@ public final class PDPageContentStream e
                 array = new COSArray();
                 array.add(contents);
             }
+
             if (appendContent.isPrepend())
             {
-                array.add(0, contentsToAppend.getCOSObject());
+                array.add(0, stream.getCOSObject());
             }
             else
             {
-                array.add(contentsToAppend);
+                array.add(stream);
             }
 
             // save the initial/unmodified graphics context
             if (resetContext)
             {
-                // create a new stream to encapsulate the existing stream
-                PDStream saveGraphics = new PDStream(document);
-                setOutputStream(saveGraphics.createOutputStream(filter));
-                
-                // save the initial/unmodified graphics context
-                saveGraphicsState();
-                close();
-                
+                // create a new stream to prefix existing stream
+                PDStream prefixStream = new PDStream(document);
+
+                // save the pre-append graphics state
+                OutputStream prefixOut = prefixStream.createOutputStream();
+                prefixOut.write("q".getBytes(Charsets.US_ASCII));
+                prefixOut.write('\n');
+                prefixOut.close();
+
                 // insert the new stream at the beginning
-                array.add(0, saveGraphics.getCOSObject());
+                array.add(0, prefixStream.getCOSObject());
             }
 
             // Sets the compoundStream as page contents
             sourcePage.getCOSObject().setItem(COSName.CONTENTS, array);
-            setOutputStream(contentsToAppend.createOutputStream(filter));
 
-            // restore the initial/unmodified graphics context
+            // restore the pre-append graphics state
             if (resetContext)
             {
                 restoreGraphicsState();
@@ -213,11 +217,9 @@ public final class PDPageContentStream e
             {
                 LOG.warn("You are overwriting an existing content, you should use the append mode");
             }
-            PDStream contents = new PDStream(document);
-            sourcePage.setContents(contents);
-            setOutputStream(contents.createOutputStream(filter));
+            sourcePage.setContents(stream);
         }
-        
+
         // this has to be done here, as the resources will be set to null when resetting the content
         // stream
         PDResources resources = sourcePage.getResources();
@@ -227,6 +229,7 @@ public final class PDPageContentStream e
             sourcePage.setResources(resources);
         }
         setResources(resources);
+
         // configure NumberFormat
         setMaximumFractionDigits(5);
     }
@@ -368,7 +371,7 @@ public final class PDPageContentStream e
             }
         }
 
-        COSWriter.writeString(encodedText, getOutputStream());
+        COSWriter.writeString(encodedText, outputStream);
     }
 
     /**