Home » pdfbox-1.1.0-src » org.apache.pdfbox.filter » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.pdfbox.filter;
   18   
   19   import java.io.IOException;
   20   import java.io.InputStream;
   21   import java.io.OutputStream;
   22   
   23   import org.apache.pdfbox.cos.COSDictionary;
   24   
   25   /**
   26    * This is a filter for the RunLength Decoder.
   27    *
   28    * From the PDF Reference
   29    * <pre>
   30    * The RunLengthDecode filter decodes data that has been encoded in a simple
   31    * byte-oriented format based on run length. The encoded data is a sequence of
   32    * runs, where each run consists of a length byte followed by 1 to 128 bytes of data. If
   33    * the length byte is in the range 0 to 127, the following length + 1 (1 to 128) bytes
   34    * are copied literally during decompression. If length is in the range 129 to 255, the
   35    * following single byte is to be copied 257 ? length (2 to 128) times during decompression.
   36    * A length value of 128 denotes EOD.
   37    *
   38    * The compression achieved by run-length encoding depends on the input data. In
   39    * the best case (all zeros), a compression of approximately 64:1 is achieved for long
   40    * files. The worst case (the hexadecimal sequence 00 alternating with FF) results in
   41    * an expansion of 127:128.
   42    * </pre>
   43    *
   44    * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
   45    * @version $Revision: 1.6 $
   46    */
   47   public class RunLengthDecodeFilter implements Filter
   48   {
   49       private static final int RUN_LENGTH_EOD = 128;
   50   
   51       /**
   52        * Constructor.
   53        */
   54       public RunLengthDecodeFilter()
   55       {
   56           //default constructor
   57       }
   58   
   59       /**
   60        * {@inheritDoc}
   61        */
   62       public void decode( InputStream compressedData, OutputStream result, COSDictionary options, int filterIndex ) 
   63           throws IOException
   64       {
   65           int dupAmount = -1;
   66           byte[] buffer = new byte[128];
   67           while( (dupAmount = compressedData.read()) != -1 && dupAmount != RUN_LENGTH_EOD )
   68           {
   69               if( dupAmount <= 127 )
   70               {
   71                   int amountToCopy = dupAmount+1;
   72                   int compressedRead = 0;
   73                   while( amountToCopy > 0 )
   74                   {
   75                       compressedRead = compressedData.read( buffer, 0, amountToCopy );
   76                       result.write( buffer, 0, compressedRead );
   77                       amountToCopy -= compressedRead;
   78                   }
   79               }
   80               else
   81               {
   82                   int dupByte = compressedData.read();
   83                   for( int i=0; i<257-dupAmount; i++ )
   84                   {
   85                       result.write( dupByte );
   86                   }
   87               }
   88           }
   89       }
   90   
   91        /**
   92        * {@inheritDoc}
   93        */
   94       public void encode( InputStream rawData, OutputStream result, COSDictionary options, int filterIndex ) 
   95           throws IOException
   96       {
   97           System.err.println( "Warning: RunLengthDecodeFilter.encode is not implemented yet, skipping this stream." );
   98       }
   99   }

Home » pdfbox-1.1.0-src » org.apache.pdfbox.filter » [javadoc | source]