Author: msahyoun
Date: Fri Feb  9 17:30:14 2018
New Revision: 1823682

URL: http://svn.apache.org/viewvc?rev=1823682&view=rev
Log:
PDFBOX-4071: use try-with for resource handling

Modified:
    
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java

Modified: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java?rev=1823682&r1=1823681&r2=1823682&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java
 (original)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java
 Fri Feb  9 17:30:14 2018
@@ -273,51 +273,53 @@ class AppearanceGeneratorHelper
      */
     private void initializeAppearanceContent(PDAnnotationWidget widget, 
PDAppearanceStream appearanceStream) throws IOException
     {
-        ByteArrayOutputStream output = new ByteArrayOutputStream();
-        PDAppearanceContentStream contents = new 
PDAppearanceContentStream(appearanceStream, output);
-        PDAppearanceCharacteristicsDictionary appearanceCharacteristics = 
widget.getAppearanceCharacteristics();
-        
-        // TODO: support more entries like patterns, etc.
-        if (appearanceCharacteristics != null)
+        try (ByteArrayOutputStream output = new ByteArrayOutputStream();
+                PDAppearanceContentStream contents = new 
PDAppearanceContentStream(appearanceStream, output))
         {
-            PDColor backgroundColour = 
appearanceCharacteristics.getBackground();
-            if (backgroundColour != null)
-            {
-                contents.setNonStrokingColor(backgroundColour);
-                PDRectangle bbox = resolveBoundingBox(widget, 
appearanceStream);
-                
contents.addRect(bbox.getLowerLeftX(),bbox.getLowerLeftY(),bbox.getWidth(), 
bbox.getHeight());
-                contents.fill();
-            }
 
-            float lineWidth = 0f;
-            PDColor borderColour = appearanceCharacteristics.getBorderColour();
-            if (borderColour != null)
-            {
-                contents.setStrokingColor(borderColour);
-                lineWidth = 1f;
-            }
-            PDBorderStyleDictionary borderStyle = widget.getBorderStyle();
-            if (borderStyle != null && borderStyle.getWidth() > 0)
+            PDAppearanceCharacteristicsDictionary appearanceCharacteristics = 
widget.getAppearanceCharacteristics();
+            
+            // TODO: support more entries like patterns, etc.
+            if (appearanceCharacteristics != null)
             {
-                lineWidth = borderStyle.getWidth();
-            }
-
-            if (lineWidth > 0 && borderColour != null)
-            {
-                if (lineWidth != 1)
+                PDColor backgroundColour = 
appearanceCharacteristics.getBackground();
+                if (backgroundColour != null)
+                {
+                    contents.setNonStrokingColor(backgroundColour);
+                    PDRectangle bbox = resolveBoundingBox(widget, 
appearanceStream);
+                    
contents.addRect(bbox.getLowerLeftX(),bbox.getLowerLeftY(),bbox.getWidth(), 
bbox.getHeight());
+                    contents.fill();
+                }
+    
+                float lineWidth = 0f;
+                PDColor borderColour = 
appearanceCharacteristics.getBorderColour();
+                if (borderColour != null)
                 {
-                    contents.setLineWidth(lineWidth);
+                    contents.setStrokingColor(borderColour);
+                    lineWidth = 1f;
+                }
+                PDBorderStyleDictionary borderStyle = widget.getBorderStyle();
+                if (borderStyle != null && borderStyle.getWidth() > 0)
+                {
+                    lineWidth = borderStyle.getWidth();
+                }
+    
+                if (lineWidth > 0 && borderColour != null)
+                {
+                    if (lineWidth != 1)
+                    {
+                        contents.setLineWidth(lineWidth);
+                    }
+                    PDRectangle bbox = resolveBoundingBox(widget, 
appearanceStream);
+                    PDRectangle clipRect = applyPadding(bbox, 
Math.max(DEFAULT_PADDING, lineWidth/2)); 
+                    
contents.addRect(clipRect.getLowerLeftX(),clipRect.getLowerLeftY(),clipRect.getWidth(),
 clipRect.getHeight());
+                    contents.closeAndStroke();
                 }
-                PDRectangle bbox = resolveBoundingBox(widget, 
appearanceStream);
-                PDRectangle clipRect = applyPadding(bbox, 
Math.max(DEFAULT_PADDING, lineWidth/2)); 
-                
contents.addRect(clipRect.getLowerLeftX(),clipRect.getLowerLeftY(),clipRect.getWidth(),
 clipRect.getHeight());
-                contents.closeAndStroke();
             }
+            
+            writeToStream(output.toByteArray(), appearanceStream);
+
         }
-        
-        contents.close();
-        output.close();
-        writeToStream(output.toByteArray(), appearanceStream);
     }
     
     /**
@@ -342,40 +344,40 @@ class AppearanceGeneratorHelper
         
         // then replace the existing contents of the appearance stream from 
/Tx BMC
         // to the matching EMC
-        ByteArrayOutputStream output = new ByteArrayOutputStream();
-        ContentStreamWriter writer = new ContentStreamWriter(output);
-        
-        List<Object> tokens = tokenize(appearanceStream);
-        int bmcIndex = tokens.indexOf(BMC);
-        if (bmcIndex == -1)
-        {
-            // append to existing stream
-            writer.writeTokens(tokens);
-            writer.writeTokens(COSName.TX, BMC);
-        }
-        else
-        {
-            // prepend content before BMC
-            writer.writeTokens(tokens.subList(0, bmcIndex + 1));
-        }
-        
-        // insert field contents
-        insertGeneratedAppearance(widget, appearanceStream, output);
-        
-        int emcIndex = tokens.indexOf(EMC);
-        if (emcIndex == -1)
-        {
-            // append EMC
-            writer.writeTokens(EMC);
-        }
-        else
+        try (ByteArrayOutputStream output = new ByteArrayOutputStream())
         {
-            // append contents after EMC
-            writer.writeTokens(tokens.subList(emcIndex, tokens.size()));
+            ContentStreamWriter writer = new ContentStreamWriter(output);
+            
+            List<Object> tokens = tokenize(appearanceStream);
+            int bmcIndex = tokens.indexOf(BMC);
+            if (bmcIndex == -1)
+            {
+                // append to existing stream
+                writer.writeTokens(tokens);
+                writer.writeTokens(COSName.TX, BMC);
+            }
+            else
+            {
+                // prepend content before BMC
+                writer.writeTokens(tokens.subList(0, bmcIndex + 1));
+            }
+            
+            // insert field contents
+            insertGeneratedAppearance(widget, appearanceStream, output);
+            
+            int emcIndex = tokens.indexOf(EMC);
+            if (emcIndex == -1)
+            {
+                // append EMC
+                writer.writeTokens(EMC);
+            }
+            else
+            {
+                // append contents after EMC
+                writer.writeTokens(tokens.subList(emcIndex, tokens.size()));
+            }
+            writeToStream(output.toByteArray(), appearanceStream);
         }
-
-        output.close();
-        writeToStream(output.toByteArray(), appearanceStream);
     }
     
     /**


Reply via email to