Class ServletOutputStreamImpl

java.lang.Object
java.io.OutputStream
javax.servlet.ServletOutputStream
io.undertow.servlet.spec.ServletOutputStreamImpl
All Implemented Interfaces:
io.undertow.io.BufferWritableOutputStream, Closeable, Flushable, AutoCloseable

public class ServletOutputStreamImpl extends javax.servlet.ServletOutputStream implements io.undertow.io.BufferWritableOutputStream
This stream essentially has two modes. When it is being used in standard blocking mode then it will buffer in the pooled buffer. If the stream is closed before the buffer is full it will set a content-length header if one has not been explicitly set.

If a content-length header was present when the stream was created then it will automatically close and flush itself once the appropriate amount of data has been written.

Once the listener has been set it goes into async mode, and writes become non blocking. Most methods have two different code paths, based on if the listener has been set or not

Once the write listener has been set operations must only be invoked on this stream from the write listener callback. Attempting to invoke from a different thread will result in an IllegalStateException.

Async listener tasks are queued in the AsyncContextImpl. At most one listener can be active at one time, which simplifies the thread safety requirements.

Author:
Stuart Douglas
  • Constructor Details

    • ServletOutputStreamImpl

      public ServletOutputStreamImpl(ServletRequestContext servletRequestContext)
      Construct a new instance. No write timeout is configured.
    • ServletOutputStreamImpl

      public ServletOutputStreamImpl(ServletRequestContext servletRequestContext, int bufferSize)
      Construct a new instance. No write timeout is configured.
  • Method Details

    • write

      public void write(int b) throws IOException
      Specified by:
      write in class OutputStream
      Throws:
      IOException
    • write

      public void write(byte[] b) throws IOException
      Overrides:
      write in class OutputStream
      Throws:
      IOException
    • write

      public void write(byte[] b, int off, int len) throws IOException
      Overrides:
      write in class OutputStream
      Throws:
      IOException
    • write

      public void write(ByteBuffer[] buffers) throws IOException
      Specified by:
      write in interface io.undertow.io.BufferWritableOutputStream
      Throws:
      IOException
    • write

      public void write(ByteBuffer byteBuffer) throws IOException
      Specified by:
      write in interface io.undertow.io.BufferWritableOutputStream
      Throws:
      IOException
    • flush

      public void flush() throws IOException
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      IOException
    • flushInternal

      public void flushInternal() throws IOException
      Throws:
      IOException
    • transferFrom

      public void transferFrom(FileChannel source) throws IOException
      Specified by:
      transferFrom in interface io.undertow.io.BufferWritableOutputStream
      Throws:
      IOException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException
    • closeAsync

      public void closeAsync() throws IOException
      Closes the channel, and flushes any data out using async IO

      This is used in two situations, if an output stream is not closed when a request is done, and when performing a close on a stream that is in async mode

      Throws:
      IOException
    • resetBuffer

      public void resetBuffer()
    • setBufferSize

      public void setBufferSize(int size)
    • isClosed

      public boolean isClosed()
    • isReady

      public boolean isReady()
      Specified by:
      isReady in class javax.servlet.ServletOutputStream
    • setWriteListener

      public void setWriteListener(javax.servlet.WriteListener writeListener)
      Specified by:
      setWriteListener in class javax.servlet.ServletOutputStream