pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r1544789 - in /pdfbox/trunk/pdfbox/src: main/java/org/apache/pdfbox/cos/COSFloat.java test/java/org/apache/pdfbox/cos/TestCOSFloat.java
Date Sat, 23 Nov 2013 13:25:31 GMT
Author: lehmi
Date: Sat Nov 23 13:25:31 2013
New Revision: 1544789

URL: http://svn.apache.org/r1544789
Log:
PDFBOX-1778: avoid a floating point string representation in some corner cases

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSFloat.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSFloat.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSFloat.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSFloat.java?rev=1544789&r1=1544788&r2=1544789&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSFloat.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSFloat.java Sat Nov 23 13:25:31
2013
@@ -40,8 +40,7 @@ public class COSFloat extends COSNumber
      */
     public COSFloat( float aFloat )
     {
-        valueAsString = String.valueOf(aFloat);
-        value = new BigDecimal(valueAsString);
+        setValue(aFloat);
     }
 
     /**
@@ -71,8 +70,23 @@ public class COSFloat extends COSNumber
      */
     public void setValue( float floatValue )
     {
-        valueAsString = String.valueOf(floatValue);
-        value = new BigDecimal(valueAsString);
+        // use a BigDecimal as intermediate state to avoid 
+        // a floating point string representation of the float value
+        value = new BigDecimal(String.valueOf(floatValue));
+        valueAsString = removeNullDigits(value.toPlainString());
+    }
+
+    private String removeNullDigits(String value)
+    {
+        // remove fraction digit "0" only
+        if (value.indexOf(".") > -1 && !value.endsWith(".0"))
+        {
+            while (value.endsWith("0") && !value.endsWith(".0"))
+            {
+                value = value.substring(0,value.length()-1);
+            }
+        }
+        return value;
     }
 
     /**
@@ -136,7 +150,7 @@ public class COSFloat extends COSNumber
      */
     public String toString()
     {
-        return "COSFloat{" + value + "}";
+        return "COSFloat{" + valueAsString + "}";
     }
 
     /**

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSFloat.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSFloat.java?rev=1544789&r1=1544788&r2=1544789&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSFloat.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSFloat.java Sat Nov 23 13:25:31
2013
@@ -20,6 +20,7 @@ package org.apache.pdfbox.cos;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.math.BigDecimal;
 import java.util.Random;
 
 import junit.framework.Test;
@@ -160,8 +161,8 @@ public class TestCOSFloat extends TestCO
                 num = i * rnd.nextFloat();
                 COSFloat cosFloat = new COSFloat(num);
                 cosFloat.accept(visitor);
-                assertEquals(Float.toString(cosFloat.floatValue()), outStream.toString("ISO-8859-1"));
-                testByteArrays(Float.toString(num).getBytes("ISO-8859-1"),
+                assertEquals(floatToString(cosFloat.floatValue()), outStream.toString("ISO-8859-1"));
+                testByteArrays(floatToString(num).getBytes("ISO-8859-1"),
                         outStream.toByteArray());
                 outStream.reset();
             }
@@ -186,11 +187,17 @@ public class TestCOSFloat extends TestCO
                 num = i * rnd.nextFloat();
                 COSFloat cosFloat = new COSFloat(num);
                 cosFloat.writePDF(outStream);
-                assertEquals(Float.toString(cosFloat.floatValue()), outStream.toString("ISO-8859-1"));
-                testByteArrays(Float.toString(num).getBytes("ISO-8859-1"),
+                assertEquals(floatToString(cosFloat.floatValue()), outStream.toString("ISO-8859-1"));
+                testByteArrays(floatToString(num).getBytes("ISO-8859-1"),
                         outStream.toByteArray());
                 outStream.reset();
             }
+            // test a corner case as described in PDFBOX-1778
+            num = 0.000000000000000000000000000000001f;
+            COSFloat test = new COSFloat(num);
+            test.writePDF(outStream);
+            assertEquals(floatToString(num), outStream.toString("ISO-8859-1"));
+            outStream.reset();
         }
         catch (IOException e)
         {
@@ -198,6 +205,26 @@ public class TestCOSFloat extends TestCO
         }
     }
 
+    private String floatToString(float value)
+    {
+        // use a BigDecimal as intermediate state to avoid 
+        // a floating point string representation of the float value
+        return removeTrailingNull(new BigDecimal(String.valueOf(value)).toPlainString());

+    }
+    
+    private String removeTrailingNull(String value)
+    {
+        // remove fraction digit "0" only
+        if (value.indexOf(".") > -1 && !value.endsWith(".0"))
+        {
+            while (value.endsWith("0") && !value.endsWith(".0"))
+            {
+                value = value.substring(0,value.length()-1);
+            }
+        }
+        return value;
+    }
+
     /**
      * This will get the suite of test that this class holds.
      *



Mime
View raw message