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 2013/08/25 13:24:53 UTC

svn commit: r1517273 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io: ASCII85InputStream.java ASCII85OutputStream.java

Author: lehmi
Date: Sun Aug 25 11:24:52 2013
New Revision: 1517273

URL: http://svn.apache.org/r1517273
Log:
PDFBOX-1694, PDFBOX-1696: fixed a padding issue, added some optimizations based on a proposal by Tilman Hausherr

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/ASCII85InputStream.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/ASCII85OutputStream.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/ASCII85InputStream.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/ASCII85InputStream.java?rev=1517273&r1=1517272&r2=1517273&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/ASCII85InputStream.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/ASCII85InputStream.java Sun Aug 25 11:24:52 2013
@@ -17,14 +17,14 @@
 package org.apache.pdfbox.io;
 
 import java.io.FilterInputStream;
-import java.io.InputStream;
 import java.io.IOException;
+import java.io.InputStream;
 
 /**
  * This class represents an ASCII85 stream.
  *
  * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
- * @version $Revision: 1.6 $
+ *
  */
 public class ASCII85InputStream extends FilterInputStream
 {
@@ -35,12 +35,20 @@ public class ASCII85InputStream extends 
     private byte[] ascii;
     private byte[] b;
 
+    private static final char TERMINATOR = '~';
+    private static final char OFFSET = '!';
+    private static final char NEWLINE = '\n';
+    private static final char RETURN = '\r';
+    private static final char SPACE = ' ';
+    private static final char PADDING_U = 'u';
+    private static final char Z = 'z';
+
     /**
      * Constructor.
      *
      * @param is The input stream to actually read from.
      */
-    public ASCII85InputStream( InputStream is )
+    public ASCII85InputStream(InputStream is)
     {
         super(is);
         index = 0;
@@ -59,9 +67,9 @@ public class ASCII85InputStream extends 
      */
     public final int read() throws IOException
     {
-        if( index >= n )
+        if (index >= n)
         {
-            if(eof)
+            if (eof)
             {
                 return -1;
             }
@@ -70,69 +78,72 @@ public class ASCII85InputStream extends 
             byte z;
             do
             {
-                int zz=(byte)in.read();
-                if(zz==-1)
+                int zz = (byte) in.read();
+                if (zz == -1)
                 {
-                    eof=true;
+                    eof = true;
                     return -1;
                 }
-                z = (byte)zz;
-            }  while( z=='\n' || z=='\r' || z==' ');
+                z = (byte) zz;
+            } while (z == NEWLINE || z == RETURN || z == SPACE);
 
-            if (z == '~' || z=='x')
+            if (z == TERMINATOR)
             {
-                eof=true;
-                ascii=b=null;
+                eof = true;
+                ascii = b = null;
                 n = 0;
                 return -1;
             }
-            else if (z == 'z')
+            else if (z == Z)
             {
-                b[0]=b[1]=b[2]=b[3]=0;
+                b[0] = b[1] = b[2] = b[3] = 0;
                 n = 4;
             }
             else
             {
-                ascii[0]=z; // may be EOF here....
-                for (k=1;k<5;++k)
+                ascii[0] = z; // may be EOF here....
+                for (k = 1; k < 5; ++k)
                 {
                     do
                     {
-                        int zz=(byte)in.read();
-                        if(zz==-1)
+                        int zz = (byte) in.read();
+                        if (zz == -1)
                         {
-                            eof=true;
+                            eof = true;
                             return -1;
                         }
-                        z=(byte)zz;
-                    } while ( z=='\n' || z=='\r' || z==' ' );
-                    ascii[k]=z;
-                    if (z == '~' || z=='x')
+                        z = (byte) zz;
+                    } while (z == NEWLINE || z == RETURN || z == SPACE);
+                    ascii[k] = z;
+                    if (z == TERMINATOR)
                     {
+                        // don't include ~ as padding byte
+                        ascii[k] = (byte) PADDING_U;
                         break;
                     }
                 }
                 n = k - 1;
-                if ( n==0 )
+                if (n == 0)
                 {
                     eof = true;
                     ascii = null;
                     b = null;
                     return -1;
                 }
-                if ( k < 5 )
+                if (k < 5)
                 {
-                    for (++k; k < 5; ++k )
+                    for (++k; k < 5; ++k)
                     {
-                        ascii[k] = 0x21;
+                        // use 'u' for padding
+                        ascii[k] = (byte) PADDING_U;
                     }
-                    eof=true;
+                    eof = true;
                 }
                 // decode stream
-                long t=0;
-                for ( k=0; k<5; ++k)
+                long t = 0;
+                for (k = 0; k < 5; ++k)
                 {
-                    z=(byte)(ascii[k] - 0x21);
+                    z = (byte) (ascii[k] - OFFSET);
                     if (z < 0 || z > 93)
                     {
                         n = 0;
@@ -143,10 +154,10 @@ public class ASCII85InputStream extends 
                     }
                     t = (t * 85L) + z;
                 }
-                for ( k = 3; k>=0; --k)
+                for (k = 3; k >= 0; --k)
                 {
-                    b[k] = (byte)(t & 0xFFL);
-                    t>>>=8;
+                    b[k] = (byte) (t & 0xFFL);
+                    t >>>= 8;
                 }
             }
         }
@@ -166,24 +177,24 @@ public class ASCII85InputStream extends 
      */
     public final int read(byte[] data, int offset, int len) throws IOException
     {
-        if(eof && index>=n)
+        if (eof && index >= n)
         {
             return -1;
         }
-        for(int i=0;i<len;i++)
+        for (int i = 0; i < len; i++)
         {
-            if(index<n)
+            if (index < n)
             {
-                data[i+offset]=b[index++];
+                data[i + offset] = b[index++];
             }
             else
             {
                 int t = read();
-                if ( t == -1 )
+                if (t == -1)
                 {
                     return i;
                 }
-                data[i+offset]=(byte)t;
+                data[i + offset] = (byte) t;
             }
         }
         return len;

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/ASCII85OutputStream.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/ASCII85OutputStream.java?rev=1517273&r1=1517272&r2=1517273&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/ASCII85OutputStream.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/ASCII85OutputStream.java Sun Aug 25 11:24:52 2013
@@ -24,7 +24,7 @@ import java.io.OutputStream;
  * This class represents an ASCII85 output stream.
  *
  * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
- * @version $Revision: 1.7 $
+ *
  */
 public class ASCII85OutputStream extends FilterOutputStream
 {
@@ -42,22 +42,25 @@ public class ASCII85OutputStream extends
     private int maxline;
     private boolean flushed;
     private char terminator;
+    private static final char OFFSET = '!';
+    private static final char NEWLINE = '\n';
+    private static final char Z = 'z';
 
     /**
      * Constructor.
      *
      * @param out The output stream to write to.
      */
-    public ASCII85OutputStream( OutputStream out )
+    public ASCII85OutputStream(OutputStream out)
     {
-        super( out );
-        lineBreak = 36*2;
-        maxline = 36*2;
-        count=0;
-        indata=new byte[4];
-        outdata=new byte[5];
-        flushed=true;
-        terminator='~';
+        super(out);
+        lineBreak = 36 * 2;
+        maxline = 36 * 2;
+        count = 0;
+        indata = new byte[4];
+        outdata = new byte[5];
+        flushed = true;
+        terminator = '~';
     }
 
     /**
@@ -67,11 +70,11 @@ public class ASCII85OutputStream extends
      */
     public void setTerminator(char term)
     {
-        if(term<118 || term>126 || term=='z')
+        if (term < 118 || term > 126 || term == Z)
         {
             throw new IllegalArgumentException("Terminator must be 118-126 excluding z");
         }
-        terminator=term;
+        terminator = term;
     }
 
     /**
@@ -91,11 +94,11 @@ public class ASCII85OutputStream extends
      */
     public void setLineLength(int l)
     {
-        if( lineBreak > l )
+        if (lineBreak > l)
         {
             lineBreak = l;
         }
-        maxline=l;
+        maxline = l;
     }
 
     /**
@@ -111,44 +114,37 @@ public class ASCII85OutputStream extends
     /**
      * This will transform the next four ascii bytes.
      */
-    private final void  transformASCII85()
+    private final void transformASCII85()
     {
-        long word;
-        word=( (((indata[0] << 8) | (indata[1] &0xFF)) << 16) |
-        ( (indata[2] & 0xFF) << 8) | (indata[3] & 0xFF)
-        )    & 0xFFFFFFFFL;
-        // System.out.println("word=0x"+Long.toString(word,16)+" "+word);
+        long word = indata[0] << 8;
+        word |= (indata[1] & 0xFF) << 16;
+        word |= (indata[2] & 0xFF) << 8;
+        word |= (indata[3] & 0xFF);
+        word &= 0xFFFFFFFFL;
 
-        if (word == 0 )
+        if (word == 0)
         {
-            outdata[0]=(byte)'z';
-            outdata[1]=0;
+            outdata[0] = (byte) Z;
+            outdata[1] = 0;
             return;
         }
         long x;
-        x=word/(85L*85L*85L*85L);
-        // System.out.println("x0="+x);
-        outdata[0]=(byte)(x+'!');
-        word-=x*85L*85L*85L*85L;
-
-        x=word/(85L*85L*85L);
-        // System.out.println("x1="+x);
-        outdata[1]=(byte)(x+'!');
-        word-=x*85L*85L*85L;
-
-        x=word/(85L*85L);
-        // System.out.println("x2="+x);
-        outdata[2]=(byte)(x+'!');
-        word-=x*85L*85L;
-
-        x=word/85L;
-        // System.out.println("x3="+x);
-        outdata[3]=(byte)(x+'!');
+        x = word / (85L * 85L * 85L * 85L);
+        outdata[0] = (byte) (x + OFFSET);
+        word -= x * 85L * 85L * 85L * 85L;
+
+        x = word / (85L * 85L * 85L);
+        outdata[1] = (byte) (x + OFFSET);
+        word -= x * 85L * 85L * 85L;
+
+        x = word / (85L * 85L);
+        outdata[2] = (byte) (x + OFFSET);
+        word -= x * 85L * 85L;
 
-        // word-=x*85L;
+        x = word / 85L;
+        outdata[3] = (byte) (x + OFFSET);
 
-        // System.out.println("x4="+(word % 85L));
-        outdata[4]=(byte)((word%85L)+'!');
+        outdata[4] = (byte) ((word % 85L) + OFFSET);
     }
 
     /**
@@ -160,97 +156,73 @@ public class ASCII85OutputStream extends
      */
     public final void write(int b) throws IOException
     {
-        flushed=false;
-        indata[count++]=(byte)b;
-        if(count < 4 )
+        flushed = false;
+        indata[count++] = (byte) b;
+        if (count < 4)
         {
             return;
         }
         transformASCII85();
-        for(int i=0;i<5;i++)
+        for (int i = 0; i < 5; i++)
         {
-            if(outdata[i]==0)
+            if (outdata[i] == 0)
             {
                 break;
             }
             out.write(outdata[i]);
-            if(--lineBreak==0)
+            if (--lineBreak == 0)
             {
-                out.write('\n');
-                lineBreak=maxline;
+                out.write(NEWLINE);
+                lineBreak = maxline;
             }
         }
         count = 0;
     }
 
     /**
-     * This will write a chunk of data to the stream.
-     *
-     * @param b The byte buffer to read from.
-     * @param off The offset into the buffer.
-     * @param sz The number of bytes to read from the buffer.
-     *
-     * @throws IOException If there is an error writing to the underlying stream.
-     */
-    public final void write(byte[] b,int off, int sz) throws IOException
-    {
-        for(int i=0;i<sz;i++)
-        {
-            if(count < 3)
-            {
-                indata[count++]=b[off+i];
-            }
-            else
-            {
-                write(b[off+i]);
-            }
-        }
-    }
-
-    /**
      * This will flush the data to the stream.
      *
      * @throws IOException If there is an error writing the data to the stream.
      */
     public final void flush() throws IOException
     {
-        if(flushed)
+        if (flushed)
         {
             return;
         }
-        if(count > 0 )
+        if (count > 0)
         {
-            for( int i=count; i<4; i++ )
+            for (int i = count; i < 4; i++)
             {
-                indata[i]=0;
+                indata[i] = 0;
             }
             transformASCII85();
-            if(outdata[0]=='z')
+            if (outdata[0] == Z)
             {
-                for(int i=0;i<5;i++) // expand 'z',
+                for (int i = 0; i < 5; i++) // expand 'z',
                 {
-                    outdata[i]=(byte)'!';
+                    outdata[i] = (byte) OFFSET;
                 }
             }
-            for(int i=0;i<count+1;i++)
+            for (int i = 0; i < count + 1; i++)
             {
                 out.write(outdata[i]);
-                if(--lineBreak==0)
+                if (--lineBreak == 0)
                 {
-                    out.write('\n');
-                    lineBreak=maxline;
+                    out.write(NEWLINE);
+                    lineBreak = maxline;
                 }
             }
         }
-        if(--lineBreak==0)
+        if (--lineBreak == 0)
         {
-            out.write('\n');
+            out.write(NEWLINE);
         }
         out.write(terminator);
-        out.write('\n');
+        out.write(NEWLINE);
         count = 0;
-        lineBreak=maxline;
-        flushed=true;
+        lineBreak = maxline;
+        flushed = true;
         super.flush();
     }
 
@@ -263,28 +235,12 @@ public class ASCII85OutputStream extends
     {
         try
         {
-            super.close();
-        }
-        finally
-        {
-            indata=outdata=null;
-        }
-    }
-
-    /**
-     * This will flush the stream.
-     *
-     * @throws Throwable If there is an error.
-     */
-    protected void finalize() throws Throwable
-    {
-        try
-        {
             flush();
+            super.close();
         }
         finally
         {
-            super.finalize();
+            indata = outdata = null;
         }
     }
 }