Class LazyGzipCompressingOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable

    public class LazyGzipCompressingOutputStream
    extends java.io.FilterOutputStream
    A filter output stream for an HttpServletResponse's output stream which starts out by writing to a size-limited byte buffer and defers the decision of whether or not to use GZIP compression to the point where the content written to it exceeds the buffer size or the stream is flushed or closed. If the stream is closed before the buffer size is exceeded, the buffer is written to the HttpServletResponse's output stream without compression activated. If the stream is flushed before compression was activated, the buffer content is flushed to the response's output stream, the buffer is released, and compression will not be activated for this stream ever. Flushing the stream after compression has been activated will delegate to the GZIPOutputStream's flush method.

    The stream is not thread-safe. Multiple threads trying to write to it concurrently may end up in a race condition, may overwrite buffer content and may cause inconsistencies when crossing the size threshold for compression.

    • Field Summary

      • Fields inherited from class java.io.FilterOutputStream

        out
    • Constructor Summary

      Constructors 
      Constructor Description
      LazyGzipCompressingOutputStream​(javax.servlet.http.HttpServletResponse response, int sizeLimitForNoCompression)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      void flush()  
      void write​(byte[] b, int off, int len)  
      void write​(int b)  
      • Methods inherited from class java.io.FilterOutputStream

        write
      • Methods inherited from class java.io.OutputStream

        nullOutputStream
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • LazyGzipCompressingOutputStream

        public LazyGzipCompressingOutputStream​(javax.servlet.http.HttpServletResponse response,
                                               int sizeLimitForNoCompression)
                                        throws java.io.IOException
        Throws:
        java.io.IOException
    • Method Detail

      • write

        public void write​(int b)
                   throws java.io.IOException
        Overrides:
        write in class java.io.FilterOutputStream
        Throws:
        java.io.IOException
      • write

        public void write​(byte[] b,
                          int off,
                          int len)
                   throws java.io.IOException
        Overrides:
        write in class java.io.FilterOutputStream
        Throws:
        java.io.IOException
      • flush

        public void flush()
                   throws java.io.IOException
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.FilterOutputStream
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.FilterOutputStream
        Throws:
        java.io.IOException