pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1764156 - /pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDictionary.java
Date Mon, 10 Oct 2016 17:53:54 GMT
Author: tilman
Date: Mon Oct 10 17:53:53 2016
New Revision: 1764156

URL: http://svn.apache.org/viewvc?rev=1764156&view=rev
Log:
PDFBOX-3514: avoid endless recursion in toString(), by Simon Steiner

Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDictionary.java

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDictionary.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDictionary.java?rev=1764156&r1=1764155&r2=1764156&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDictionary.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDictionary.java Mon
Oct 10 17:53:53 2016
@@ -17,12 +17,17 @@
 package org.apache.pdfbox.cos;
 
 import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Collection;
 import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.pdfbox.io.IOUtils;
 import org.apache.pdfbox.pdmodel.common.COSObjectable;
 import org.apache.pdfbox.util.DateConverter;
 
@@ -1402,9 +1407,8 @@ public class COSDictionary extends COSBa
      */
     public COSBase getObjectFromPath(String objPath)
     {
-        COSBase retval = null;
         String[] path = objPath.split(PATH_SEPARATOR);
-        retval = this;
+        COSBase retval = this;
         for (String pathString : path)
         {
             if (retval instanceof COSArray)
@@ -1436,25 +1440,54 @@ public class COSDictionary extends COSBa
     @Override
     public String toString()
     {
-        StringBuilder retVal = new StringBuilder(getClass().getSimpleName());
-        retVal.append("{");
-        for (COSName key : items.keySet())
-        {
-            retVal.append("(");
-            retVal.append(key);
-            retVal.append(":");
-            if (getDictionaryObject(key) != null)
+        try
+        {
+            return getDictionaryString(this, new ArrayList<COSBase>());
+        }
+        catch (IOException e)
+        {
+            return "COSDictionary{" + e.getMessage() + "}";
+        }
+    }
+
+    private static String getDictionaryString(COSBase base, List<COSBase> objs) throws
IOException
+    {
+        if (base == null)
+        {
+            return "null";
+        }
+        if (objs.contains(base))
+        {
+            // avoid endless recursion
+            return String.valueOf(base.hashCode());
+        }
+        objs.add(base);
+        if (base instanceof COSDictionary)
+        {
+            StringBuilder sb = new StringBuilder();
+            sb.append("COSDictionary{");
+            for (Map.Entry<COSName, COSBase> x : ((COSDictionary) base).entrySet())
             {
-                retVal.append(getDictionaryObject(key).toString());
+                sb.append(x.getKey());
+                sb.append(":");
+                sb.append(getDictionaryString(x.getValue(), objs));
+                sb.append(";");
             }
-            else
+            sb.append("}");
+            if (base instanceof COSStream)
             {
-                retVal.append("<null>");
+                InputStream stream = ((COSStream) base).createRawInputStream();
+                byte[] b = IOUtils.toByteArray(stream);
+                sb.append("COSStream{").append(Arrays.hashCode(b)).append("}");
+                stream.close();
             }
-            retVal.append(") ");
+            return sb.toString();
+        }
+        if (base instanceof COSObject)
+        {
+            COSObject obj = (COSObject) base;
+            return "COSObject{" + getDictionaryString(obj.getObject(), objs) + "}";
         }
-        retVal.append("}");
-        return retVal.toString();
+        return base.toString();
     }
-
 }



Mime
View raw message