Top Description Inners Fields Constructors Methods
io.undertow.servlet.spec

public Class ServletOutputStreamImpl

extends ServletOutputStream
implements BufferWritableOutputStream
Class Inheritance
All Implemented Interfaces
io.undertow.io.BufferWritableOutputStream
Static Imports
org.xnio.Bits.allAreClear, .Bits.anyAreClear, .Bits.anyAreSet

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

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private AsyncContextImpl
private volatile boolean
private ByteBuffer
private Integer
private ByteBuffer[]
buffersToWrite

buffers that are queued up to be written via async writes.

private StreamSinkChannel
private static final int
private static final int
private static final int
private static final int
private static final int
private ServletOutputStreamImpl.WriteChannelListener
private WriteListener
private static final int
private FileChannel
private PooledByteBuffer
private final ServletRequestContext
private volatile int
private static final AtomicIntegerFieldUpdater<ServletOutputStreamImpl>
private long

Constructor Summary

AccessConstructor and Description
public
ServletOutputStreamImpl(final ServletRequestContext servletRequestContext)

Construct a new instance.

public
ServletOutputStreamImpl(final ServletRequestContext servletRequestContext, int bufferSize)

Construct a new instance.

Method Summary

Modifier and TypeMethod and Description
private ByteBuffer
private void
clearFlags(int flags)

public void
close()

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Closes this output stream and releases any system resources associated with this stream.

public void
closeAsync()

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

private void
public void
flush()

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Flushes this output stream and forces any buffered output bytes to be written out.

private boolean
flushBufferAsync(final boolean writeFinal)

public void
pack-priv ServletRequestContext
public boolean
public boolean
isReady()

Implements abstract jakarta.servlet.ServletOutputStream.isReady.

Returns true if data can be written without blocking else returns false.

pack-priv long
public void
public void
setBufferSize(final int size)

private void
setFlags(int flags)

public void
setWriteListener(final WriteListener
the WriteListener that should be notified when it's possible to write
writeListener
)

Implements abstract jakarta.servlet.ServletOutputStream.setWriteListener.

Instructs the ServletOutputStream to invoke the provided WriteListener when it is possible to write

public void
pack-priv ByteBuffer

Returns:

The underlying buffer
underlyingBuffer
()

Returns the underlying buffer.

pack-priv void
updateWritten(final long len)

pack-priv void
updateWrittenAsync(final long len)

public void
write(final int
the byte.
b
)

Implements abstract java.io.OutputStream.write.

Writes the specified byte to this output stream.

public void
write(final byte[]
the data.
b
)

Overrides java.io.OutputStream.write.

Writes b.length bytes from the specified byte array to this output stream.

public void
write(final byte[]
the data.
b
,
final int
the start offset in the data.
off
,
final int
the number of bytes to write.
len
)

Overrides java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream.

public void
public void
private void
writeAsync(byte[] b, int off, int len)

private void
writeBufferBlocking(final boolean writeFinal)

private void
writeTooLargeForBuffer(byte[] b, int off, int len, ByteBuffer buffer)

Inherited from jakarta.servlet.ServletOutputStream:
printprintprintprintprintprintprintprintlnprintlnprintlnprintlnprintlnprintlnprintlnprintln

Field Detail

asyncContextback to summary
private AsyncContextImpl asyncContext
asyncIoStartedback to summary
private volatile boolean asyncIoStarted
bufferback to summary
private ByteBuffer buffer
bufferSizeback to summary
private Integer bufferSize
buffersToWriteback to summary
private ByteBuffer[] buffersToWrite

buffers that are queued up to be written via async writes. This will include buffer as the first element, and maybe a user supplied buffer that did not fit

channelback to summary
private StreamSinkChannel channel
FLAG_CLOSEDback to summary
private static final int FLAG_CLOSED
FLAG_DELEGATE_SHUTDOWNback to summary
private static final int FLAG_DELEGATE_SHUTDOWN
FLAG_IN_CALLBACKback to summary
private static final int FLAG_IN_CALLBACK
FLAG_READYback to summary
private static final int FLAG_READY
FLAG_WRITE_STARTEDback to summary
private static final int FLAG_WRITE_STARTED
internalListenerback to summary
private ServletOutputStreamImpl.WriteChannelListener internalListener
listenerback to summary
private WriteListener listener
MAX_BUFFERS_TO_ALLOCATEback to summary
private static final int MAX_BUFFERS_TO_ALLOCATE
pendingFileback to summary
private FileChannel pendingFile
pooledBufferback to summary
private PooledByteBuffer pooledBuffer
servletRequestContextback to summary
private final ServletRequestContext servletRequestContext
stateback to summary
private volatile int state
stateUpdaterback to summary
private static final AtomicIntegerFieldUpdater<ServletOutputStreamImpl> stateUpdater
writtenback to summary
private long written

Constructor Detail

ServletOutputStreamImplback to summary
public ServletOutputStreamImpl(final ServletRequestContext servletRequestContext)

Construct a new instance. No write timeout is configured.

ServletOutputStreamImplback to summary
public ServletOutputStreamImpl(final ServletRequestContext servletRequestContext, int bufferSize)

Construct a new instance. No write timeout is configured.

Method Detail

bufferback to summary
private ByteBuffer buffer()
clearFlagsback to summary
private void clearFlags(int flags)
closeback to summary
public void close() throws IOException

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Doc from java.io.OutputStream.close.

Closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.

Exceptions
IOException:
if an I/O error occurs.
closeAsyncback to summary
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

createChannelback to summary
private void createChannel()
flushback to summary
public void flush() throws IOException

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Doc from java.io.OutputStream.flush.

Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

Exceptions
IOException:
if an I/O error occurs.
flushBufferAsyncback to summary
private boolean flushBufferAsync(final boolean writeFinal) throws IOException
flushInternalback to summary
public void flushInternal() throws IOException
getServletRequestContextback to summary
pack-priv ServletRequestContext getServletRequestContext()
isClosedback to summary
public boolean isClosed()
isReadyback to summary
public boolean isReady()

Implements abstract jakarta.servlet.ServletOutputStream.isReady.

Doc from jakarta.servlet.ServletOutputStream.isReady.

Returns true if data can be written without blocking else returns false.

If this method returns false and a WriteListener has been set with setWriteListener(WriteListener), then container will subsequently invoke WriteListener#onWritePossible() once a write operation becomes possible without blocking. Other than the initial call, WriteListener#onWritePossible() will only be called if and only if this method is called and returns false.

Returns:boolean

true if data can be written without blocking, otherwise returns false.

Annotations
@Override
remainingContentLengthback to summary
pack-priv long remainingContentLength() throws IOException
resetBufferback to summary
public void resetBuffer()
setBufferSizeback to summary
public void setBufferSize(final int size)
setFlagsback to summary
private void setFlags(int flags)
setWriteListenerback to summary
public void setWriteListener(final WriteListener writeListener)

Implements abstract jakarta.servlet.ServletOutputStream.setWriteListener.

Doc from jakarta.servlet.ServletOutputStream.setWriteListener.

Instructs the ServletOutputStream to invoke the provided WriteListener when it is possible to write

Parameters
writeListener:WriteListener

the WriteListener that should be notified when it's possible to write

Annotations
@Override
transferFromback to summary
public void transferFrom(FileChannel source) throws IOException

Implements io.undertow.io.BufferWritableOutputStream.transferFrom.

Annotations
@Override
underlyingBufferback to summary
pack-priv ByteBuffer underlyingBuffer()

Returns the underlying buffer. If this has not been created yet then it is created.

Callers that use this method must call updateWritten(long) to update the written amount.

This allows the buffer to be filled directly, which can be more efficient.

This method is basically a hack that should only be used by the print writer

Returns:ByteBuffer

The underlying buffer

updateWrittenback to summary
pack-priv void updateWritten(final long len) throws IOException
updateWrittenAsyncback to summary
pack-priv void updateWrittenAsync(final long len) throws IOException
writeback to summary
public void write(final int b) throws IOException

Implements abstract java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Parameters
b:int

the byte.

Exceptions
IOException:
if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
writeback to summary
public void write(final byte[] b) throws IOException

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes b.length bytes from the specified byte array to this output stream. The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length).

Parameters
b:byte[]

the data.

Exceptions
IOException:
if an I/O error occurs.
writeback to summary
public void write(final byte[] b, final int off, final int len) throws IOException

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.

If b is null, a NullPointerException is thrown.

If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.

Parameters
b:byte[]

the data.

off:int

the start offset in the data.

len:int

the number of bytes to write.

Exceptions
IOException:
if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.
writeback to summary
public void write(ByteBuffer[] buffers) throws IOException

Implements io.undertow.io.BufferWritableOutputStream.write.

Annotations
@Override
writeback to summary
public void write(ByteBuffer byteBuffer) throws IOException

Implements io.undertow.io.BufferWritableOutputStream.write.

Annotations
@Override
writeAsyncback to summary
private void writeAsync(byte[] b, int off, int len) throws IOException
writeBufferBlockingback to summary
private void writeBufferBlocking(final boolean writeFinal) throws IOException
writeTooLargeForBufferback to summary
private void writeTooLargeForBuffer(byte[] b, int off, int len, ByteBuffer buffer) throws IOException
io.undertow.servlet.spec back to summary

private Class ServletOutputStreamImpl.WriteChannelListener

extends Object
implements ChannelListener<StreamSinkChannel>
Class Inheritance
  • java.lang.Object
  • io.undertow.servlet.spec.ServletOutputStreamImpl.WriteChannelListener
All Implemented Interfaces
org.xnio.ChannelListener, java.util.EventListener

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private void
public void
handleEvent(final StreamSinkChannel
the channel event
aChannel
)

Implements org.xnio.ChannelListener.handleEvent.

Handle the event on this channel.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

WriteChannelListenerback to summary
private WriteChannelListener()

Method Detail

handleErrorback to summary
private void handleError(final Throwable t)
handleEventback to summary
public void handleEvent(final StreamSinkChannel aChannel)

Implements org.xnio.ChannelListener.handleEvent.

Doc from org.xnio.ChannelListener.handleEvent.

Handle the event on this channel.

Parameters
aChannel:StreamSinkChannel

the channel event

Annotations
@Override