net.i2p.i2ptunnel

## Class HTTPResponseOutputStream

- java.lang.Object
- - java.io.OutputStream
  - - java.io.FilterOutputStream
    - - net.i2p.i2ptunnel.HTTPResponseOutputStream

- All Implemented Interfaces:
    Closeable, Flushable, AutoCloseable

      class HTTPResponseOutputStream
      extends FilterOutputStream

  HTTP response stream with transparent gzip decompression (client side).

  Extended in I2PTunnelHTTPServer for server-side compression. Simple filtered response stream ensuring "Connection: close" header. Could handle Content-Encoding: x-i2p-gzip transparently, adjusting to Content-Encoding: identity. gzip encoding trivial, but chunked transfer encoding adds complexity.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected String` | `_contentEncoding` lower-case, trimmed
    `protected String` | `_contentType` lower-case, trimmed
    `protected long` | `_dataExpected`
    `protected boolean` | `_gzip`
    `protected ByteArray` | `_headerBuffer`
    `protected boolean` | `_keepAliveIn`
    `protected boolean` | `_keepAliveOut`

    - ### Fields inherited from class java.io.FilterOutputStream

      `out`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `HTTPResponseOutputStream(OutputStream raw)`
    `HTTPResponseOutputStream(OutputStream raw, boolean allowKeepAliveIn, boolean allowKeepAliveOut, boolean isHead, LimitOutputStream.DoneCallback cb)` Optionally keep sockets alive and call callback when we're done.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `beginProcessing()`
    `void` | `close()`
    `protected String` | `filterResponseLine(String line)` Possibly tweak that first HTTP response line (HTTP/1.0 200 OK, etc).
    `protected void` | `finishHeaders()`
    `boolean` | `getKeepAliveIn()` Should we keep the input stream alive when done?
    `boolean` | `getKeepAliveOut()` Should we keep the output stream alive when done? Only supported for the browser socket side.
    `protected boolean` | `shouldCompress()`
    `void` | `write(byte[] buf, int off, int len)`
    `void` | `write(int c)`

    - ### Methods inherited from class java.io.FilterOutputStream

      `flush, write`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### _contentEncoding

          protected String _contentEncoding

      lower-case, trimmed

    - #### _contentType

          protected String _contentType

      lower-case, trimmed

    - #### _dataExpected

          protected volatile long _dataExpected

    - #### _gzip

          protected volatile boolean _gzip

    - #### _headerBuffer

          protected ByteArray _headerBuffer

    - #### _keepAliveIn

          protected volatile boolean _keepAliveIn

    - #### _keepAliveOut

          protected volatile boolean _keepAliveOut

  - ### Constructor Detail

    - #### HTTPResponseOutputStream

          public HTTPResponseOutputStream(OutputStream raw)

    - #### HTTPResponseOutputStream

          public HTTPResponseOutputStream(OutputStream raw,
                                          boolean allowKeepAliveIn,
                                          boolean allowKeepAliveOut,
                                          boolean isHead,
                                          LimitOutputStream.DoneCallback cb)

      Optionally keep sockets alive and call callback when we're done.
      Parameters:
        `allowKeepAliveIn` - We may, but are not required to, keep the input socket alive. This is the server on the server side and I2P on the client side.
        `allowKeepAliveOut` - We may, but are not required to, keep the output socket alive. This is I2P on the server side and the browser on the client side.
        `isHead` - is this a response to a HEAD, and thus no data is expected (RFC 2616 sec. 4.4)
        `cb` - non-null if allowKeepAlive is true

  - ### Method Detail

    - #### beginProcessing

          protected void beginProcessing()
                                  throws IOException

      Throws:
        `IOException`

    - #### close

          public void close()
                     throws IOException

      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Overrides:
        `close` in class `FilterOutputStream`
      Throws:
        `IOException`

    - #### filterResponseLine

          protected String filterResponseLine(String line)

      Possibly tweak that first HTTP response line (HTTP/1.0 200 OK, etc). Overridden on server side.

    - #### finishHeaders

          protected void finishHeaders()
                                throws IOException

      Throws:
        `IOException`

    - #### getKeepAliveIn

          public boolean getKeepAliveIn()

      Should we keep the input stream alive when done?
      Returns:
        false before the headers are written

    - #### getKeepAliveOut

          public boolean getKeepAliveOut()

      Should we keep the output stream alive when done? Only supported for the browser socket side. I2P socket on server side not supported yet.
      Returns:
        false before the headers are written

    - #### shouldCompress

          protected boolean shouldCompress()

    - #### write

          public void write(byte[] buf,
                            int off,
                            int len)
                     throws IOException

      Overrides:
        `write` in class `FilterOutputStream`
      Throws:
        `IOException`

    - #### write

          public void write(int c)
                     throws IOException

      Overrides:
        `write` in class `FilterOutputStream`
      Throws:
        `IOException`