001: /**
002: * Copyright (c) 2004, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.filter;
031:
032: import java.io.IOException;
033: import java.io.InputStream;
034: import java.io.OutputStream;
035:
036: import org.pdfbox.cos.COSDictionary;
037:
038: /**
039: * This is a filter for the RunLength Decoder.
040: *
041: * From the PDF Reference
042: * <pre>
043: * The RunLengthDecode filter decodes data that has been encoded in a simple
044: * byte-oriented format based on run length. The encoded data is a sequence of
045: * runs, where each run consists of a length byte followed by 1 to 128 bytes of data. If
046: * the length byte is in the range 0 to 127, the following length + 1 (1 to 128) bytes
047: * are copied literally during decompression. If length is in the range 129 to 255, the
048: * following single byte is to be copied 257 ? length (2 to 128) times during decompression.
049: * A length value of 128 denotes EOD.
050: *
051: * The compression achieved by run-length encoding depends on the input data. In
052: * the best case (all zeros), a compression of approximately 64:1 is achieved for long
053: * files. The worst case (the hexadecimal sequence 00 alternating with FF) results in
054: * an expansion of 127:128.
055: * </pre>
056: *
057: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
058: * @version $Revision: 1.5 $
059: */
060: public class RunLengthDecodeFilter implements Filter {
061: private static final int RUN_LENGTH_EOD = 128;
062:
063: /**
064: * Constructor.
065: */
066: public RunLengthDecodeFilter() {
067: //default constructor
068: }
069:
070: /**
071: * This will decode some compressed data.
072: *
073: * @param compressedData The compressed byte stream.
074: * @param result The place to write the uncompressed byte stream.
075: * @param options The options to use to encode the data.
076: *
077: * @throws IOException If there is an error decompressing the stream.
078: */
079: public void decode(InputStream compressedData, OutputStream result,
080: COSDictionary options) throws IOException {
081: int dupAmount = -1;
082: byte[] buffer = new byte[128];
083: while ((dupAmount = compressedData.read()) != -1
084: && dupAmount != RUN_LENGTH_EOD) {
085: if (dupAmount <= 127) {
086: int amountToCopy = dupAmount + 1;
087: int compressedRead = 0;
088: while (amountToCopy > 0) {
089: compressedRead = compressedData.read(buffer, 0,
090: amountToCopy);
091: result.write(buffer, 0, compressedRead);
092: amountToCopy -= compressedRead;
093: }
094: } else {
095: int dupByte = compressedData.read();
096: for (int i = 0; i < 257 - dupAmount; i++) {
097: result.write(dupByte);
098: }
099: }
100: }
101: }
102:
103: /**
104: * This will encode some data.
105: *
106: * @param rawData The raw data to encode.
107: * @param result The place to write to encoded results to.
108: * @param options The options to use to encode the data.
109: *
110: * @throws IOException If there is an error compressing the stream.
111: */
112: public void encode(InputStream rawData, OutputStream result,
113: COSDictionary options) throws IOException {
114: System.err
115: .println("Warning: RunLengthDecodeFilter.encode is not implemented yet, skipping this stream.");
116: }
117: }
|