You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2022/05/15 09:19:45 UTC

svn commit: r1900918 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font: PDCIDFontType0.java PDType1CFont.java

Author: lehmi
Date: Sun May 15 09:19:44 2022
New Revision: 1900918

URL: http://svn.apache.org/viewvc?rev=1900918&view=rev
Log:
PDFBOX-5143: sonar fix, use try-with-resources

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType0.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1CFont.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType0.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType0.java?rev=1900918&r1=1900917&r2=1900918&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType0.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType0.java Sun May 15 09:19:44 2022
@@ -73,42 +73,36 @@ public class PDCIDFontType0 extends PDCI
     {
         super(fontDictionary, parent);
 
+        boolean fontIsDamaged = false;
+        CFFFont cffFont = null;
         PDFontDescriptor fd = getFontDescriptor();
-        RandomAccessRead randomAccessRead = null;
         if (fd != null)
         {
             PDStream ff3Stream = fd.getFontFile3();
             if (ff3Stream != null)
             {
-                randomAccessRead = ff3Stream.getCOSObject().createView();
+                try (RandomAccessRead randomAccessRead = ff3Stream.getCOSObject().createView())
+                {
+                    if (randomAccessRead.length() > 0 && randomAccessRead.peek() == '%')
+                    {
+                        // PDFBOX-2642 contains a corrupt PFB font instead of a CFF
+                        LOG.warn("Found PFB but expected embedded CFF font " + fd.getFontName());
+                        fontIsDamaged = true;
+                    }
+                    else
+                    {
+                        CFFParser cffParser = new CFFParser();
+                        cffFont = cffParser.parse(randomAccessRead).get(0);
+                    }
+                }
+                catch (IOException e)
+                {
+                    LOG.error("Can't read the embedded CFF font " + fd.getFontName(), e);
+                    fontIsDamaged = true;
+                }
             }
         }
 
-        boolean fontIsDamaged = false;
-        CFFFont cffFont = null;
-        if (randomAccessRead != null && randomAccessRead.length() > 0
-                && randomAccessRead.peek() == '%')
-        {
-            // PDFBOX-2642 contains a corrupt PFB font instead of a CFF
-            LOG.warn("Found PFB but expected embedded CFF font " + fd.getFontName());
-            fontIsDamaged = true;
-            randomAccessRead.close();
-        }
-        else if (randomAccessRead != null)
-        {
-            CFFParser cffParser = new CFFParser();
-            try
-            {
-                cffFont = cffParser.parse(randomAccessRead).get(0);
-                randomAccessRead.close();
-            }
-            catch (IOException e)
-            {
-                LOG.error("Can't read the embedded CFF font " + fd.getFontName(), e);
-                fontIsDamaged = true;
-            }
-        }
-        
         if (cffFont != null)
         {
             // embedded

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1CFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1CFont.java?rev=1900918&r1=1900917&r2=1900918&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1CFont.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1CFont.java Sun May 15 09:19:44 2022
@@ -76,52 +76,47 @@ public class PDType1CFont extends PDSimp
     {
         super(fontDictionary);
 
+        boolean fontIsDamaged = false;
+        CFFType1Font cffEmbedded = null;
         PDFontDescriptor fd = getFontDescriptor();
-        RandomAccessRead randomAccessRead = null;
         if (fd != null)
         {
             PDStream ff3Stream = fd.getFontFile3();
             if (ff3Stream != null)
             {
-                randomAccessRead = fd.getFontFile3().getCOSObject().createView();
-                if (randomAccessRead.length() == 0)
-                {
-                    LOG.error("Invalid data for embedded Type1C font " + getName());
-                    randomAccessRead.close();
-                    randomAccessRead = null;
-                }
-            }
-        }
-
-        boolean fontIsDamaged = false;
-        CFFType1Font cffEmbedded = null;
-        try
-        {
-            if (randomAccessRead != null)
-            {
-                // note: this could be an OpenType file, fortunately CFFParser can handle that
-                CFFParser cffParser = new CFFParser();
-                CFFFont parsedCffFont = cffParser.parse(randomAccessRead).get(0);
-                if (parsedCffFont instanceof CFFType1Font)
+                try (RandomAccessRead randomAccessRead = fd.getFontFile3().getCOSObject()
+                        .createView())
                 {
-                    cffEmbedded = (CFFType1Font) parsedCffFont;
+                    if (randomAccessRead.length() == 0)
+                    {
+                        LOG.error("Invalid data for embedded Type1C font " + getName());
+                    }
+                    else
+                    {
+                        // note: this could be an OpenType file, fortunately CFFParser can handle that
+                        CFFParser cffParser = new CFFParser();
+                        CFFFont parsedCffFont = cffParser.parse(randomAccessRead).get(0);
+                        if (parsedCffFont instanceof CFFType1Font)
+                        {
+                            cffEmbedded = (CFFType1Font) parsedCffFont;
+                        }
+                        else
+                        {
+                            LOG.error("Expected CFFType1Font, got "
+                                    + parsedCffFont.getClass().getSimpleName());
+                            fontIsDamaged = true;
+                        }
+                    }
                 }
-                else
+                catch (IOException e)
                 {
-                    LOG.error("Expected CFFType1Font, got " + parsedCffFont.getClass().getSimpleName());
+                    LOG.error("Can't read the embedded Type1C font " + getName(), e);
                     fontIsDamaged = true;
                 }
-                randomAccessRead.close();
             }
         }
-        catch (IOException e)
-        {
-            LOG.error("Can't read the embedded Type1C font " + getName(), e);
-            fontIsDamaged = true;
-        }
         isDamaged = fontIsDamaged;
         cffFont = cffEmbedded;
-
         if (cffFont != null)
         {
             genericFont = cffFont;