Class ServletOutputStreamImpl
- java.lang.Object
-
- java.io.OutputStream
-
- jakarta.servlet.ServletOutputStream
-
- io.undertow.servlet.spec.ServletOutputStreamImpl
-
- All Implemented Interfaces:
io.undertow.io.BufferWritableOutputStream
,java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
public class ServletOutputStreamImpl extends jakarta.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 Summary
Constructors Constructor Description ServletOutputStreamImpl(ServletRequestContext servletRequestContext)
Construct a new instance.ServletOutputStreamImpl(ServletRequestContext servletRequestContext, int bufferSize)
Construct a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
void
closeAsync()
Closes the channel, and flushes any data out using async IOvoid
flush()
void
flushInternal()
boolean
isClosed()
boolean
isReady()
void
resetBuffer()
void
setBufferSize(int size)
void
setWriteListener(jakarta.servlet.WriteListener writeListener)
void
transferFrom(java.nio.channels.FileChannel source)
void
write(byte[] b)
void
write(byte[] b, int off, int len)
void
write(int b)
void
write(java.nio.ByteBuffer byteBuffer)
void
write(java.nio.ByteBuffer[] buffers)
-
-
-
Constructor Detail
-
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 Detail
-
write
public void write(int b) throws java.io.IOException
- Specified by:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
write
public void write(byte[] b) throws java.io.IOException
- Overrides:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException
- Overrides:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
write
public void write(java.nio.ByteBuffer[] buffers) throws java.io.IOException
- Specified by:
write
in interfaceio.undertow.io.BufferWritableOutputStream
- Throws:
java.io.IOException
-
write
public void write(java.nio.ByteBuffer byteBuffer) throws java.io.IOException
- Specified by:
write
in interfaceio.undertow.io.BufferWritableOutputStream
- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOException
- Specified by:
flush
in interfacejava.io.Flushable
- Overrides:
flush
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
flushInternal
public void flushInternal() throws java.io.IOException
- Throws:
java.io.IOException
-
transferFrom
public void transferFrom(java.nio.channels.FileChannel source) throws java.io.IOException
- Specified by:
transferFrom
in interfaceio.undertow.io.BufferWritableOutputStream
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
closeAsync
public void closeAsync() throws java.io.IOException
Closes the channel, and flushes any data out using async IOThis 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:
java.io.IOException
-
resetBuffer
public void resetBuffer()
-
setBufferSize
public void setBufferSize(int size)
-
isClosed
public boolean isClosed()
-
isReady
public boolean isReady()
- Specified by:
isReady
in classjakarta.servlet.ServletOutputStream
-
setWriteListener
public void setWriteListener(jakarta.servlet.WriteListener writeListener)
- Specified by:
setWriteListener
in classjakarta.servlet.ServletOutputStream
-
-