org.hd.d.pg2k.webSvr.util
Class FlushableGZIPOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by net.sf.jazzlib.DeflaterOutputStream
              extended by org.hd.d.pg2k.webSvr.util.FlushableGZIPOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class FlushableGZIPOutputStream
extends net.sf.jazzlib.DeflaterOutputStream

Substitute for GZIPOutputStream that maximises compression and has a usable flush(). This is also more careful about its output writes for efficiency, and indeed buffers them to minimise the number of write()s downstream. This is especially useful where each write() potentially has a significant cost such as an OS call, a disc write, or a network packet.

This will automatically throttle back from maximum to default compression (which usually yields nearly as good results on significantly less effort) when the system is conserving power/energy.


Field Summary
private  java.util.zip.CRC32 crc
           
private static int GZIP_MAGIC
           
private static byte[] header
          Constant (10-byte) header array.
private  java.io.OutputStream os
           
private  boolean somethingWritten
          Note when input has arrived and not yet been compressed and flushed downstream.
private static int TRAILER_BYTES
          Length of the trailer in bytes.
 
Fields inherited from class net.sf.jazzlib.DeflaterOutputStream
buf, def
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
FlushableGZIPOutputStream(java.io.OutputStream os)
          Creates flush()able equivalent of GZIPOutputStream, with maximum/best compression and default output buffer size.
FlushableGZIPOutputStream(java.io.OutputStream os, int bufsize)
          Creates flush()able equivalent of GZIPOutputStream, with good compression.
 
Method Summary
 void close()
          Flush out any remaining compressed data, add the trailer, and close the stream.
 void flush()
          Flush any accumulated input downstream in compressed form.
private  byte[] generateTrailer()
          Return header to write downstream.
 void write(byte[] buf, int off, int len)
          All output is written via this routine.
private  void writeHeader()
          Write header directly downstream.
 
Methods inherited from class net.sf.jazzlib.DeflaterOutputStream
deflate, finish, write
 
Methods inherited from class java.io.FilterOutputStream
write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

crc

private final java.util.zip.CRC32 crc

GZIP_MAGIC

private static final int GZIP_MAGIC
See Also:
Constant Field Values

os

private final java.io.OutputStream os

somethingWritten

private boolean somethingWritten
Note when input has arrived and not yet been compressed and flushed downstream.


header

private static final byte[] header
Constant (10-byte) header array. All zeros except magic number and algorithm.


TRAILER_BYTES

private static final int TRAILER_BYTES
Length of the trailer in bytes.

See Also:
Constant Field Values
Constructor Detail

FlushableGZIPOutputStream

public FlushableGZIPOutputStream(java.io.OutputStream os)
                          throws java.io.IOException
Creates flush()able equivalent of GZIPOutputStream, with maximum/best compression and default output buffer size. Useful for, for example, HTTP compression.

Parameters:
os - underlying output stream; must not be null
Throws:
java.io.IOException

FlushableGZIPOutputStream

public FlushableGZIPOutputStream(java.io.OutputStream os,
                                 int bufsize)
                          throws java.io.IOException
Creates flush()able equivalent of GZIPOutputStream, with good compression. Useful for, for example, HTTP compression.

Parameters:
os - underlying output stream; must not be null
bufsize - output buffer size; must be positive
Throws:
java.io.IOException
Method Detail

write

public void write(byte[] buf,
                  int off,
                  int len)
           throws java.io.IOException
All output is written via this routine.

Overrides:
write in class net.sf.jazzlib.DeflaterOutputStream
Throws:
java.io.IOException

flush

public void flush()
           throws java.io.IOException
Flush any accumulated input downstream in compressed form. We overcome some bugs/misfeatures here so that:

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class net.sf.jazzlib.DeflaterOutputStream
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Flush out any remaining compressed data, add the trailer, and close the stream.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class net.sf.jazzlib.DeflaterOutputStream
Throws:
java.io.IOException

writeHeader

private void writeHeader()
                  throws java.io.IOException
Write header directly downstream.

Throws:
java.io.IOException

generateTrailer

private byte[] generateTrailer()
Return header to write downstream.


DHD Multimedia Gallery V1.57.21

Copyright (c) 1996-2011, Damon Hart-Davis. All rights reserved.