public void flateCompress ( int compressionLevel ) { if ( ! Document . compress ) return ; // check if the flateCompress-method has already been if ( compressed ) { return ; } this . compressionLevel = compressionLevel ; if ( inputStream != null ) { compressed = true ; return ; } // check if a filter already exists PdfObject filter = PdfReader . getPdfObject ( get ( PdfName . FILTER ) ) ; if ( filter != null ) { if ( filter . isName ( ) ) { if ( PdfName . FLATEDECODE . equals ( filter ) ) return ; } else if ( filter . isArray ( ) ) { if ( ( ( PdfArray ) filter ) . contains ( PdfName . FLATEDECODE ) ) return ; } else { throw new RuntimeException ( "Stream could not be compressed: filter is not a name or array." ) ; } } try { // compress ByteArrayOutputStream stream = new ByteArrayOutputStream ( ) ; Deflater deflater = new Deflater ( compressionLevel ) ; DeflaterOutputStream zip = new DeflaterOutputStream ( stream , deflater ) ; if ( streamBytes != null ) streamBytes . writeTo ( zip ) ; else zip . write ( bytes ) ; zip . close ( ) ; deflater . end ( ) ; // update the object streamBytes = stream ; bytes = null ; put ( PdfName . LENGTH , new PdfNumber ( streamBytes . size ( ) ) ) ; if ( filter == null ) { put ( PdfName . FILTER , PdfName . FLATEDECODE ) ; } else { PdfArray filters = new PdfArray ( filter ) ; filters . add ( PdfName . FLATEDECODE ) ; put ( PdfName . FILTER , filters ) ; } compressed = true ; } catch ( IOException ioe ) { throw new ExceptionConverter ( ioe ) ; } }
Compresses the stream .