< prev index next >

src/java.base/share/classes/java/util/zip/DeflaterOutputStream.java

Print this page




  41 public
  42 class DeflaterOutputStream extends FilterOutputStream {
  43     /**
  44      * Compressor for this stream.
  45      */
  46     protected Deflater def;
  47 
  48     /**
  49      * Output buffer for writing compressed data.
  50      */
  51     protected byte[] buf;
  52 
  53     /**
  54      * Indicates that the stream has been closed.
  55      */
  56 
  57     private boolean closed = false;
  58 
  59     private final boolean syncFlush;
  60 


  61     /**
  62      * Creates a new output stream with the specified compressor,
  63      * buffer size and flush mode.
  64 
  65      * @param out the output stream
  66      * @param def the compressor ("deflater")
  67      * @param size the output buffer size
  68      * @param syncFlush
  69      *        if {@code true} the {@link #flush()} method of this
  70      *        instance flushes the compressor with flush mode
  71      *        {@link Deflater#SYNC_FLUSH} before flushing the output
  72      *        stream, otherwise only flushes the output stream
  73      *
  74      * @throws IllegalArgumentException if {@code size <= 0}
  75      *
  76      * @since 1.7
  77      */
  78     public DeflaterOutputStream(OutputStream out,
  79                                 Deflater def,
  80                                 int size,


 233      * underlying stream.
 234      * @exception IOException if an I/O error has occurred
 235      */
 236     public void close() throws IOException {
 237         if (!closed) {
 238             finish();
 239             if (usesDefaultDeflater)
 240                 def.end();
 241             out.close();
 242             closed = true;
 243         }
 244     }
 245 
 246     /**
 247      * Writes next block of compressed data to the output stream.
 248      * @throws IOException if an I/O error has occurred
 249      */
 250     protected void deflate() throws IOException {
 251         int len = def.deflate(buf, 0, buf.length);
 252         if (len > 0) {





 253             out.write(buf, 0, len);
 254         }
 255     }
 256 
 257     /**
 258      * Flushes the compressed output stream.
 259      *
 260      * If {@link #DeflaterOutputStream(OutputStream, Deflater, int, boolean)
 261      * syncFlush} is {@code true} when this compressed output stream is
 262      * constructed, this method first flushes the underlying {@code compressor}
 263      * with the flush mode {@link Deflater#SYNC_FLUSH} to force
 264      * all pending data to be flushed out to the output stream and then
 265      * flushes the output stream. Otherwise this method only flushes the
 266      * output stream without flushing the {@code compressor}.
 267      *
 268      * @throws IOException if an I/O error has occurred
 269      *
 270      * @since 1.7
 271      */
 272     public void flush() throws IOException {
 273         if (syncFlush && !def.finished()) {
 274             int len = 0;
 275             while ((len = def.deflate(buf, 0, buf.length, Deflater.SYNC_FLUSH)) > 0)
 276             {





 277                 out.write(buf, 0, len);
 278                 if (len < buf.length)
 279                     break;
 280             }
 281         }
 282         out.flush();
 283     }









 284 }


  41 public
  42 class DeflaterOutputStream extends FilterOutputStream {
  43     /**
  44      * Compressor for this stream.
  45      */
  46     protected Deflater def;
  47 
  48     /**
  49      * Output buffer for writing compressed data.
  50      */
  51     protected byte[] buf;
  52 
  53     /**
  54      * Indicates that the stream has been closed.
  55      */
  56 
  57     private boolean closed = false;
  58 
  59     private final boolean syncFlush;
  60 
  61     private ZipCryption zipCryption;
  62 
  63     /**
  64      * Creates a new output stream with the specified compressor,
  65      * buffer size and flush mode.
  66 
  67      * @param out the output stream
  68      * @param def the compressor ("deflater")
  69      * @param size the output buffer size
  70      * @param syncFlush
  71      *        if {@code true} the {@link #flush()} method of this
  72      *        instance flushes the compressor with flush mode
  73      *        {@link Deflater#SYNC_FLUSH} before flushing the output
  74      *        stream, otherwise only flushes the output stream
  75      *
  76      * @throws IllegalArgumentException if {@code size <= 0}
  77      *
  78      * @since 1.7
  79      */
  80     public DeflaterOutputStream(OutputStream out,
  81                                 Deflater def,
  82                                 int size,


 235      * underlying stream.
 236      * @exception IOException if an I/O error has occurred
 237      */
 238     public void close() throws IOException {
 239         if (!closed) {
 240             finish();
 241             if (usesDefaultDeflater)
 242                 def.end();
 243             out.close();
 244             closed = true;
 245         }
 246     }
 247 
 248     /**
 249      * Writes next block of compressed data to the output stream.
 250      * @throws IOException if an I/O error has occurred
 251      */
 252     protected void deflate() throws IOException {
 253         int len = def.deflate(buf, 0, buf.length);
 254         if (len > 0) {
 255 
 256             if (zipCryption != null) {
 257                 zipCryption.encryptBytes(buf, 0, len);
 258             }
 259 
 260             out.write(buf, 0, len);
 261         }
 262     }
 263 
 264     /**
 265      * Flushes the compressed output stream.
 266      *
 267      * If {@link #DeflaterOutputStream(OutputStream, Deflater, int, boolean)
 268      * syncFlush} is {@code true} when this compressed output stream is
 269      * constructed, this method first flushes the underlying {@code compressor}
 270      * with the flush mode {@link Deflater#SYNC_FLUSH} to force
 271      * all pending data to be flushed out to the output stream and then
 272      * flushes the output stream. Otherwise this method only flushes the
 273      * output stream without flushing the {@code compressor}.
 274      *
 275      * @throws IOException if an I/O error has occurred
 276      *
 277      * @since 1.7
 278      */
 279     public void flush() throws IOException {
 280         if (syncFlush && !def.finished()) {
 281             int len = 0;
 282             while ((len = def.deflate(buf, 0, buf.length, Deflater.SYNC_FLUSH)) > 0)
 283             {
 284 
 285                 if (zipCryption != null) {
 286                     zipCryption.encryptBytes(buf, 0, len);
 287                 }
 288 
 289                 out.write(buf, 0, len);
 290                 if (len < buf.length)
 291                     break;
 292             }
 293         }
 294         out.flush();
 295     }
 296 
 297     /**
 298      * Set ZIP encryption/decryption engine to this deflater.
 299      * @param zipCryption ZIP encrypt/decrypt engine.
 300      */
 301     public void setZipCryption(ZipCryption zipCryption) {
 302         this.zipCryption = zipCryption;
 303     }
 304 
 305 }
< prev index next >