Package org.apache.pdfbox.filter

Examples of org.apache.pdfbox.filter.Filter


            {
                done = true;
            }
            else
            {
                Filter filter = FilterFactory.INSTANCE.getFilter(nextFilter);
                filter.decode(is, os, stream, i);
                IOUtils.closeQuietly(is);
                is = new ByteArrayInputStream(os.toByteArray());
                os.reset();
            }
        }
View Full Code Here


            ByteArrayOutputStream out = new ByteArrayOutputStream(data.length);
            for (int i = 0; i < filters.size(); i++)
            {
                // TODO handling of abbreviated names belongs here, rather than in other classes
                out.reset();
                Filter filter = FilterFactory.INSTANCE.getFilter(filters.get(i));
                decodeResult = filter.decode(in, out, parameters, i);
                in = new ByteArrayInputStream(out.toByteArray());
            }
            byte[] finalData = out.toByteArray();
            this.stream = new PDMemoryStream(finalData);
        }
View Full Code Here

            byte [] byteArray, int width, int height, int bitsPerComponent,
            PDColorSpace initColorSpace) throws IOException
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        Filter filter = FilterFactory.INSTANCE.getFilter(COSName.FLATE_DECODE);
        filter.encode(new ByteArrayInputStream(byteArray), baos, new COSDictionary(), 0);

        ByteArrayInputStream filteredByteStream = new ByteArrayInputStream(baos.toByteArray());
        return new PDImageXObject(document, filteredByteStream, COSName.FLATE_DECODE,
                width, height, bitsPerComponent, initColorSpace);
    }
View Full Code Here

     *
     * @throws IOException If there is an error parsing the stream.
     */
    private void doDecode( COSName filterName, int filterIndex ) throws IOException
    {
        Filter filter = FilterFactory.INSTANCE.getFilter( filterName );

        boolean done = false;
        IOException exception = null;
        long position = unFilteredStream.getPosition();
        long length = unFilteredStream.getLength();
        // in case we need it later
        long writtenLength = unFilteredStream.getLengthWritten()

        if (length == 0 && writtenLength == 0)
        {
            //if the length is zero then don't bother trying to decode
            //some filters don't work when attempting to decode
            //with a zero length stream.  See zlib_error_01.pdf
            IOUtils.closeQuietly(unFilteredStream);
            unFilteredStream = new RandomAccessFileOutputStream( buffer );
            done = true;
        }
        else
        {
            //ok this is a simple hack, sometimes we read a couple extra
            //bytes that shouldn't be there, so we encounter an error we will just
            //try again with one less byte.
            for (int tryCount = 0; length > 0 && !done && tryCount < 5; tryCount++)
            {
                InputStream input = null;
                try
                {
                    input = new BufferedInputStream(
                        new RandomAccessFileInputStream( buffer, position, length ), BUFFER_SIZE );
                    IOUtils.closeQuietly(unFilteredStream);
                    unFilteredStream = new RandomAccessFileOutputStream( buffer );
                    decodeResult = filter.decode( input, unFilteredStream, this, filterIndex );
                    done = true;
                }
                catch( IOException io )
                {
                    length--;
                    exception = io;
                }
                finally
                {
                    IOUtils.closeQuietly(input);
                }
            }
            if( !done )
            {
                //if no good stream was found then lets try again but with the
                //length of data that was actually read and not length
                //defined in the dictionary
                length = writtenLength;
                for( int tryCount=0; !done && tryCount<5; tryCount++ )
                {
                    InputStream input = null;
                    try
                    {
                        input = new BufferedInputStream(
                            new RandomAccessFileInputStream( buffer, position, length ), BUFFER_SIZE );
                        IOUtils.closeQuietly(unFilteredStream);
                        unFilteredStream = new RandomAccessFileOutputStream( buffer );
                        decodeResult = filter.decode( input, unFilteredStream, this, filterIndex);
                        done = true;
                    }
                    catch( IOException io )
                    {
                        length--;
View Full Code Here

     *
     * @throws IOException If there is an error parsing the stream.
     */
    private void doEncode( COSName filterName, int filterIndex ) throws IOException
    {
        Filter filter = FilterFactory.INSTANCE.getFilter( filterName );

        InputStream input = new BufferedInputStream(
            new RandomAccessFileInputStream( buffer, filteredStream.getPosition(),
                                                   filteredStream.getLength() ), BUFFER_SIZE );
        IOUtils.closeQuietly(filteredStream);
        filteredStream = new RandomAccessFileOutputStream( buffer );
        filter.encode( input, filteredStream, this, filterIndex );
        IOUtils.closeQuietly(input);
    }
View Full Code Here

     * @throws IOException If there is an error parsing the stream.
     */
    private void doDecode( COSName filterName, int filterIndex ) throws IOException
    {
        FilterManager manager = getFilterManager();
        Filter filter = manager.getFilter( filterName );
        InputStream input;

        boolean done = false;
        IOException exception = null;
        long position = unFilteredStream.getPosition();
        long length = unFilteredStream.getLength();
        // in case we need it later
        long writtenLength = unFilteredStream.getLengthWritten()

        if( length == 0 )
        {
            //if the length is zero then don't bother trying to decode
            //some filters don't work when attempting to decode
            //with a zero length stream.  See zlib_error_01.pdf
            unFilteredStream = new RandomAccessFileOutputStream( file );
            done = true;
        }
        else
        {
            //ok this is a simple hack, sometimes we read a couple extra
            //bytes that shouldn't be there, so we encounter an error we will just
            //try again with one less byte.
            for( int tryCount=0; !done && tryCount<5; tryCount++ )
            {
                try
                {
                    input = new BufferedInputStream(
                        new RandomAccessFileInputStream( file, position, length ), BUFFER_SIZE );
                    unFilteredStream = new RandomAccessFileOutputStream( file );
                    filter.decode( input, unFilteredStream, this, filterIndex );
                    done = true;
                }
                catch( IOException io )
                {
                    length--;
                    exception = io;
                }
            }
            if( !done )
            {
                //if no good stream was found then lets try again but with the
                //length of data that was actually read and not length
                //defined in the dictionary
                length = writtenLength;
                for( int tryCount=0; !done && tryCount<5; tryCount++ )
                {
                    try
                    {
                        input = new BufferedInputStream(
                            new RandomAccessFileInputStream( file, position, length ), BUFFER_SIZE );
                        unFilteredStream = new RandomAccessFileOutputStream( file );
                        filter.decode( input, unFilteredStream, this, filterIndex );
                        done = true;
                    }
                    catch( IOException io )
                    {
                        length--;
View Full Code Here

     * @throws IOException If there is an error parsing the stream.
     */
    private void doEncode( COSName filterName, int filterIndex ) throws IOException
    {
        FilterManager manager = getFilterManager();
        Filter filter = manager.getFilter( filterName );
        InputStream input;

        input = new BufferedInputStream(
            new RandomAccessFileInputStream( file, filteredStream.getPosition(),
                                                   filteredStream.getLength() ), BUFFER_SIZE );
        filteredStream = new RandomAccessFileOutputStream( file );
        filter.encode( input, filteredStream, this, filterIndex );
    }
View Full Code Here

            {
                done = true;
            }
            else
            {
                Filter filter = manager.getFilter(nextFilter);
                filter.decode(is, os, stream, i);
                is = new ByteArrayInputStream(os.toByteArray());
            }
        }
        return is;
    }
View Full Code Here

     * @throws IOException If there is an error parsing the stream.
     */
    private void doDecode( COSName filterName, int filterIndex ) throws IOException
    {
        FilterManager manager = getFilterManager();
        Filter filter = manager.getFilter( filterName );

        boolean done = false;
        IOException exception = null;
        long position = unFilteredStream.getPosition();
        long length = unFilteredStream.getLength();
        // in case we need it later
        long writtenLength = unFilteredStream.getLengthWritten()

        if( length == 0 )
        {
            //if the length is zero then don't bother trying to decode
            //some filters don't work when attempting to decode
            //with a zero length stream.  See zlib_error_01.pdf
            IOUtils.closeQuietly(unFilteredStream);
            unFilteredStream = new RandomAccessFileOutputStream( file );
            done = true;
        }
        else
        {
            //ok this is a simple hack, sometimes we read a couple extra
            //bytes that shouldn't be there, so we encounter an error we will just
            //try again with one less byte.
            for( int tryCount=0; !done && tryCount<5; tryCount++ )
            {
                InputStream input = null;
                try
                {
                    input = new BufferedInputStream(
                        new RandomAccessFileInputStream( file, position, length ), BUFFER_SIZE );
                  IOUtils.closeQuietly(unFilteredStream);
                    unFilteredStream = new RandomAccessFileOutputStream( file );
                    filter.decode( input, unFilteredStream, this, filterIndex );
                    done = true;
                }
                catch( IOException io )
                {
                    length--;
                    exception = io;
                }
                finally
                {
                  IOUtils.closeQuietly(input);
                }
            }
            if( !done )
            {
                //if no good stream was found then lets try again but with the
                //length of data that was actually read and not length
                //defined in the dictionary
                length = writtenLength;
                for( int tryCount=0; !done && tryCount<5; tryCount++ )
                {
                    InputStream input = null;
                    try
                    {
                        input = new BufferedInputStream(
                            new RandomAccessFileInputStream( file, position, length ), BUFFER_SIZE );
                      IOUtils.closeQuietly(unFilteredStream);
                        unFilteredStream = new RandomAccessFileOutputStream( file );
                        filter.decode( input, unFilteredStream, this, filterIndex );
                        done = true;
                    }
                    catch( IOException io )
                    {
                        length--;
View Full Code Here

     * @throws IOException If there is an error parsing the stream.
     */
    private void doEncode( COSName filterName, int filterIndex ) throws IOException
    {
        FilterManager manager = getFilterManager();
        Filter filter = manager.getFilter( filterName );

        InputStream input = new BufferedInputStream(
            new RandomAccessFileInputStream( file, filteredStream.getPosition(),
                                                   filteredStream.getLength() ), BUFFER_SIZE );
        IOUtils.closeQuietly(filteredStream);
        filteredStream = new RandomAccessFileOutputStream( file );
        filter.encode( input, filteredStream, this, filterIndex );
        IOUtils.closeQuietly(input);
    }
View Full Code Here

TOP

Related Classes of org.apache.pdfbox.filter.Filter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.