Author: tilman
Date: Thu Apr 24 18:33:19 2014
New Revision: 1589828

URL: http://svn.apache.org/r1589828
Log:
PDFBOX-2042: a read operation must not alter the pdf

Modified:
    
pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDICCBased.java

Modified: 
pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDICCBased.java
URL: 
http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDICCBased.java?rev=1589828&r1=1589827&r2=1589828&view=diff
==============================================================================
--- 
pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDICCBased.java
 (original)
+++ 
pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDICCBased.java
 Thu Apr 24 18:33:19 2014
@@ -212,10 +212,8 @@ public class PDICCBased extends PDColorS
      * be 1,3,4.
      *
      * @return The number of components in this color space.
-     *
-     * @throws IOException If there is an error getting the number of color 
components.
      */
-    public int getNumberOfComponents() throws IOException
+    public int getNumberOfComponents()
     {
         if (numberOfComponents < 0)
         {
@@ -312,45 +310,20 @@ public class PDICCBased extends PDColorS
     }
 
     /**
-     * Get the range array, create and fill it with default values (0, 1) if
-     * needed so that it has enough value pairs for the position.
-     *
-     * @param pos The zero-based position that should exist after this call is
-     * completed.
-     * @return A valid range array.
-     */
-    private COSArray getRangeArray(int pos)
-    {
-        //TODO per "clean code", a method should either 
-        // return something or modify something, but not both.
-        COSArray rangeArray = 
(COSArray)stream.getStream().getDictionaryObject(COSName.RANGE);
-        if(rangeArray == null)
-        {
-            rangeArray = new COSArray();
-            stream.getStream().setItem(COSName.RANGE, rangeArray);
-        }
-        // extend range array with default values if needed
-        while (rangeArray.size() < (pos + 1) * 2)
-        {
-            rangeArray.add(new COSFloat(0));
-            rangeArray.add(new COSFloat(1));
-        }
-        return rangeArray;
-    }
-
-    /**
-     * This will get the range for a certain component number.  This is will 
never
-     * return null.  If it is not present then the range -100 to 100 will
-     * be returned.
-     *
-     * @param n The component number to get the range for.
-     *
-     * @return The range for this component.
+     * Returns the range for a certain component number.
+     * This will never return null.
+     * If it is not present then the range 0..1 will be returned.
+     * @param n the component number to get the range for
+     * @return the range for this component
      */
-    public PDRange getRangeForComponent( int n )
+    public PDRange getRangeForComponent(int n)
     {
-        COSArray rangeArray = getRangeArray( n );
-        return new PDRange( rangeArray, n );
+        COSArray rangeArray = (COSArray) 
stream.getStream().getDictionaryObject(COSName.RANGE);
+        if (rangeArray == null || rangeArray.size() < getNumberOfComponents() 
* 2)
+        {
+            return new PDRange(); // 0..1
+        }
+        return new PDRange(rangeArray, n);
     }
 
     /**
@@ -359,11 +332,22 @@ public class PDICCBased extends PDColorS
      * @param range The new range for the a component.
      * @param n The component to set the range for.
      */
-    public void setRangeForComponent( PDRange range, int n )
+    public void setRangeForComponent(PDRange range, int n)
     {
-        COSArray rangeArray = getRangeArray( n );
-        rangeArray.set( n*2, new COSFloat( range.getMin() ) );
-        rangeArray.set( n*2+1, new COSFloat( range.getMax() ) );
+        COSArray rangeArray = (COSArray) 
stream.getStream().getDictionaryObject(COSName.RANGE);
+        if (rangeArray == null)
+        {
+            rangeArray = new COSArray();
+            stream.getStream().setItem(COSName.RANGE, rangeArray);
+        }
+        // extend range array with default values if needed
+        while (rangeArray.size() < (n + 1) * 2)
+        {
+            rangeArray.add(new COSFloat(0));
+            rangeArray.add(new COSFloat(1));
+        }
+        rangeArray.set(n*2, new COSFloat(range.getMin()));
+        rangeArray.set(n*2+1, new COSFloat(range.getMax()));
     }
 
     /**
@@ -391,17 +375,9 @@ public class PDICCBased extends PDColorS
     /**
      * {@inheritDoc}
      */
+    @Override
     public String toString()
     {
-        String retVal = super.toString() + "\n\t Number of Components: ";
-        try
-        {
-            retVal = retVal + getNumberOfComponents();
-        }
-        catch (IOException exception)
-        {
-            retVal = retVal + exception.toString();
-        }
-        return retVal;
+        return getName() + "{numberOfComponents: " + getNumberOfComponents() + 
"}";
     }
 }


Reply via email to