pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r992271 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox: encoding/ pdmodel/font/
Date Fri, 03 Sep 2010 12:09:56 GMT
Author: jukka
Date: Fri Sep  3 12:09:56 2010
New Revision: 992271

URL: http://svn.apache.org/viewvc?rev=992271&view=rev
Log:
PDFBOX-815: PDFont.getEncodingManager is not thread safe

Use singleton instances for the EncodingManager and default encoding classes.
Replace the static encodings map in EncodingManager with if clauses since we only need four
entries.
Remove the "protected static" getEncodingManager() method from PDFont. This was introduced
in PDFBOX-601 and the remove should not cause backwards compatibility problems even though
strictly speaking it is a change in the binary API.

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/DictionaryEncoding.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/EncodingManager.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/MacRomanEncoding.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/PdfDocEncoding.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/StandardEncoding.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/WinAnsiEncoding.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1CFont.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/DictionaryEncoding.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/DictionaryEncoding.java?rev=992271&r1=992270&r2=992271&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/DictionaryEncoding.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/DictionaryEncoding.java Fri
Sep  3 12:09:56 2010
@@ -55,19 +55,15 @@ public class DictionaryEncoding extends 
         //for a nonsymbolic font, it is StandardEncoding, and for a symbolic font, it
         //is the font�s built-in encoding."
 
-        Encoding baseEncoding = null;
-        COSName baseEncodingName = (COSName)encoding.getDictionaryObject( COSName.BASE_ENCODING
);
-
-        if( baseEncodingName != null )
-        {
-            EncodingManager manager = new EncodingManager();
-            baseEncoding = manager.getEncoding( baseEncodingName );
-        }
-        else
-        {
-            //the default base encoding is standardEncoding
-            baseEncoding = new StandardEncoding();
+        // The default base encoding is standardEncoding
+        Encoding baseEncoding = StandardEncoding.INSTANCE;
+        COSName baseEncodingName =
+            (COSName) encoding.getDictionaryObject(COSName.BASE_ENCODING);
+        if (baseEncodingName != null) {
+            baseEncoding =
+                EncodingManager.INSTANCE.getEncoding(baseEncodingName);
         }
+
         nameToCode.putAll( baseEncoding.nameToCode );
         codeToName.putAll( baseEncoding.codeToName );
 

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/EncodingManager.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/EncodingManager.java?rev=992271&r1=992270&r2=992271&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/EncodingManager.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/EncodingManager.java Fri
Sep  3 12:09:56 2010
@@ -18,9 +18,6 @@ package org.apache.pdfbox.encoding;
 
 import java.io.IOException;
 
-import java.util.HashMap;
-import java.util.Map;
-
 import org.apache.pdfbox.cos.COSName;
 
 /**
@@ -31,43 +28,43 @@ import org.apache.pdfbox.cos.COSName;
  */
 public class EncodingManager
 {
-    private static final Map<COSName, Encoding> ENCODINGS =
-        new HashMap<COSName, Encoding>();
 
-    static
-    {
-        ENCODINGS.put( COSName.MAC_ROMAN_ENCODING, new MacRomanEncoding() );
-        ENCODINGS.put( COSName.PDF_DOC_ENCODING, new PdfDocEncoding() );
-        ENCODINGS.put( COSName.STANDARD_ENCODING, new StandardEncoding() );
-        ENCODINGS.put( COSName.WIN_ANSI_ENCODING, new WinAnsiEncoding() );
-    }
+    /**
+     * Default singleton instance of this class.
+     *
+     * @since Apache PDFBox 1.3.0
+     */
+    public static final EncodingManager INSTANCE = new EncodingManager();
 
     /**
      * This will get the standard encoding.
      *
      * @return The standard encoding.
      */
-    public Encoding getStandardEncoding()
-    {
-        return ENCODINGS.get( COSName.STANDARD_ENCODING );
+    public Encoding getStandardEncoding() {
+        return StandardEncoding.INSTANCE;
     }
 
     /**
      * This will get an encoding by name.
      *
      * @param name The name of the encoding to get.
-     *
      * @return The encoding that matches the name.
-     *
-     * @throws IOException If there is not encoding with that name.
+     * @throws IOException if there is no encoding with that name.
      */
-    public Encoding getEncoding( COSName name ) throws IOException
-    {
-        Encoding encoding = ENCODINGS.get( name );
-        if( encoding == null )
-        {
-            throw new IOException( "Unknown encoding for '" + name.getName() + "'" );
+    public Encoding getEncoding( COSName name ) throws IOException {
+        if (COSName.STANDARD_ENCODING.equals(name)) {
+            return StandardEncoding.INSTANCE;
+        } else if (COSName.WIN_ANSI_ENCODING.equals(name)) {
+            return WinAnsiEncoding.INSTANCE;
+        } else if (COSName.MAC_ROMAN_ENCODING.equals(name)) {
+            return MacRomanEncoding.INSTANCE;
+        } else if (COSName.PDF_DOC_ENCODING.equals(name)) {
+            return PdfDocEncoding.INSTANCE;
+        } else {
+            throw new IOException(
+                    "Unknown encoding for '" + name.getName() + "'");
         }
-        return encoding;
     }
+
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/MacRomanEncoding.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/MacRomanEncoding.java?rev=992271&r1=992270&r2=992271&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/MacRomanEncoding.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/MacRomanEncoding.java Fri
Sep  3 12:09:56 2010
@@ -27,6 +27,14 @@ import org.apache.pdfbox.cos.COSName;
  */
 public class MacRomanEncoding extends Encoding
 {
+
+    /**
+     * Singleton instance of this class.
+     *
+     * @since Apache PDFBox 1.3.0
+     */
+    public static final MacRomanEncoding INSTANCE = new MacRomanEncoding();
+
     /**
      * Constructor.
      */

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/PdfDocEncoding.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/PdfDocEncoding.java?rev=992271&r1=992270&r2=992271&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/PdfDocEncoding.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/PdfDocEncoding.java Fri Sep
 3 12:09:56 2010
@@ -27,6 +27,14 @@ import org.apache.pdfbox.cos.COSName;
  */
 public class PdfDocEncoding extends Encoding
 {
+
+    /**
+     * Singleton instance of this class.
+     *
+     * @since Apache PDFBox 1.3.0
+     */
+    public static final PdfDocEncoding INSTANCE = new PdfDocEncoding();
+
     /**
      * Constructor.
      */

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/StandardEncoding.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/StandardEncoding.java?rev=992271&r1=992270&r2=992271&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/StandardEncoding.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/StandardEncoding.java Fri
Sep  3 12:09:56 2010
@@ -27,6 +27,14 @@ import org.apache.pdfbox.cos.COSName;
  */
 public class StandardEncoding extends Encoding
 {
+
+    /**
+     * Singleton instance of this class.
+     *
+     * @since Apache PDFBox 1.3.0
+     */
+    public static final StandardEncoding INSTANCE = new StandardEncoding();
+
     /**
      * Constructor.
      */

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/WinAnsiEncoding.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/WinAnsiEncoding.java?rev=992271&r1=992270&r2=992271&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/WinAnsiEncoding.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/WinAnsiEncoding.java Fri
Sep  3 12:09:56 2010
@@ -27,6 +27,14 @@ import org.apache.pdfbox.cos.COSName;
  */
 public class WinAnsiEncoding extends Encoding
 {
+
+    /**
+     * Singleton instance of this class.
+     *
+     * @since Apache PDFBox 1.3.0
+     */
+    public static final WinAnsiEncoding INSTANCE = new WinAnsiEncoding();
+
     /**
      * Constructor.
      */

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java?rev=992271&r1=992270&r2=992271&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java Fri Sep 
3 12:09:56 2010
@@ -220,10 +220,10 @@ public abstract class PDFont implements 
                 }
                 if (cmap == null && cmapName != null)
                 {
-                    EncodingManager manager = getEncodingManager();
                     try 
                     {
-                        fontEncoding = manager.getEncoding( encodingName );
+                        fontEncoding =
+                            EncodingManager.INSTANCE.getEncoding(encodingName);
                     }
                     catch(IOException exception) 
                     {
@@ -811,7 +811,9 @@ public abstract class PDFont implements 
                                     // ignore the first token
                                     st.nextElement();
                                     String type1Encoding = st.nextToken();
-                                    fontEncoding = getEncodingManager().getEncoding(COSName.getPDFName(type1Encoding));
+                                    fontEncoding =
+                                        EncodingManager.INSTANCE.getEncoding(
+                                                COSName.getPDFName(type1Encoding));
                                     break;
                                 }
                             }
@@ -859,13 +861,5 @@ public abstract class PDFont implements 
     {
         return this.getCOSObject().hashCode();
     }
-    
-    private static EncodingManager encodingManager = null;
-    
-    protected static EncodingManager getEncodingManager(){
-        if(encodingManager == null){
-            encodingManager = new EncodingManager();
-        }
-        return encodingManager;
-    }
+
 }

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=992271&r1=992270&r2=992271&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 Fri
Sep  3 12:09:56 2010
@@ -476,8 +476,7 @@ public class PDType1CFont extends PDSimp
     private Map<Integer,String> loadEncoding(COSName name) throws IOException
     {
         Map<Integer,String> result = new LinkedHashMap<Integer,String>();
-        EncodingManager encodingManager = new EncodingManager();
-        Encoding encoding = encodingManager.getEncoding(name);
+        Encoding encoding = EncodingManager.INSTANCE.getEncoding(name);
         for( Iterator<Map.Entry<Integer,String>> it = (encoding.getCodeToNameMap().entrySet()).iterator();
                     it.hasNext();)
         {



Mime
View raw message