Top Inners Fields Constructors Methods
org.apache.tomcat.util.net

public abstract Class SocketWrapperBase<E>

extends Object
Class Inheritance
Known Direct Subclasses
org.apache.tomcat.util.net.Nio2Endpoint.Nio2SocketWrapper, org.apache.tomcat.util.net.NioEndpoint.NioSocketWrapper
Imports
java.io.EOFException, .IOException, java.net.SocketTimeoutException, java.nio.ByteBuffer, java.nio.channels.CompletionHandler, .InterruptedByTimeoutException, .ReadPendingException, .WritePendingException, java.util.concurrent.Executor, .RejectedExecutionException, .Semaphore, .TimeUnit, java.util.concurrent.atomic.AtomicBoolean, .AtomicLong, .AtomicReference, java.util.concurrent.locks.Lock, .ReentrantLock, jakarta.servlet.ServletConnection, org.apache.juli.logging.Log, .LogFactory, org.apache.tomcat.util.ExceptionUtils, org.apache.tomcat.util.res.StringManager

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
public static interface
public static enum
public static enum
protected abstract class
SocketWrapperBase.OperationState<A>

Internal state tracker for vectored operations.

protected class
SocketWrapperBase.VectoredIOCompletionHandler<A>

Completion handler for vectored operations.

Field Summary

Modifier and TypeField and Description
protected int
bufferedWriteSize

The max size of the individual buffered write buffers

protected final AtomicBoolean
public static final SocketWrapperBase.CompletionCheck
COMPLETE_READ

This utility CompletionCheck will cause the completion handler to be called once the given buffers are full.

public static final SocketWrapperBase.CompletionCheck
COMPLETE_READ_WITH_COMPLETION

This utility CompletionCheck will cause the completion handler to be called once the given buffers are full.

public static final SocketWrapperBase.CompletionCheck
COMPLETE_WRITE

This utility CompletionCheck will cause the write to fully write all remaining data.

public static final SocketWrapperBase.CompletionCheck
COMPLETE_WRITE_WITH_COMPLETION

This utility CompletionCheck will cause the write to fully write all remaining data.

private final String
private static final AtomicLong
private final AtomicReference<Object>
currentProcessor

The org.apache.coyote.Processor instance currently associated with the wrapper.

private final AbstractEndpoint<E, ?>
private volatile IOException
error

Used to record the first IOException that occurs during non-blocking read/writes that can't be usefully propagated up the stack since there is no user code or appropriate container code in the stack to handle it.

private volatile int
protected String
protected String
protected int
private final Lock
private static final Log
private String
protected final WriteBuffer
nonBlockingWriteBuffer

Additional buffer used for non-blocking writes.

protected volatile IOException
public static final SocketWrapperBase.CompletionCheck
READ_DATA

This utility CompletionCheck will cause the completion handler to be called once some data has been read.

protected volatile SocketWrapperBase<E>.OperationState<?>
protected final Semaphore
private volatile long
protected String
protected String
protected int
protected volatile ServletConnection
protected static final StringManager
private E
protected volatile SocketBufferHandler
socketBufferHandler

The buffers used for communicating with the socket.

protected volatile SocketWrapperBase<E>.OperationState<?>
protected final Semaphore
private volatile long

Constructor Summary

AccessConstructor and Description
public
SocketWrapperBase(E socket, AbstractEndpoint<E, ?> endpoint)

Method Summary

Modifier and TypeMethod and Description
protected static boolean
buffersArrayHasRemaining(ByteBuffer[] buffers, int offset, int length)

public boolean
public void
public void
close()

Close the socket wrapper.

public abstract SendfileDataBase
createSendfileData(String filename, long pos, long length)

public int
public abstract void
doClientAuth(SSLSupport
The SSL/TLS support instance currently being used by the connection that may need updating after the client authentication
sslSupport
)

Require the client to perform CLIENT-CERT authentication if it hasn't already done so.

protected abstract void
doClose()

Perform the actual close.

protected void
doWrite(boolean
Should the write be blocking or not?
block
)

Write the contents of the socketWriteBuffer to the socket.

protected abstract void
doWrite(boolean
Should the write be blocking or not?
block
,
ByteBuffer
the ByteBuffer containing the data to be written
from
)

Write the contents of the ByteBuffer to the socket.

public void
execute(Runnable
The actions to process on a container thread
runnable
)

Transfers processing to a container thread.

public boolean

Returns:

true if data remains to be flushed after this method completes, otherwise false. In blocking mode therefore, the return value should always be false
flush
(boolean
true if a blocking write should be used, otherwise a non-blocking write will be used
block
)

Writes as much data as possible from any that remains in the buffers.

protected void
flushBlocking()

Writes all remaining data from the buffers and blocks until the write is complete.

protected abstract boolean

Returns:

true if data remains to be flushed after this method completes, otherwise false.
flushNonBlocking
()

Writes as much data as possible from any that remains in the buffers.

public Object
protected AbstractEndpoint<E, ?>
public IOException
public String
public String
public int
public Lock
public String
public long
public String
public String
public int
public ServletConnection
getServletConnection(String protocol, String protocolConnectionId)

public E
public SocketBufferHandler
public abstract SSLSupport

Returns:

An SSLSupport instance for this socket.
getSslSupport
()

Obtain an SSLSupport instance for this socket.

private String
getTimeoutMsg(boolean read)

public long
public boolean

Returns:

true if the connector has the capability enabled
hasAsyncIO
()

Allows using NIO2 style read/write.

public boolean
public boolean
public boolean

Returns:

This default implementation always returns false
hasPerOperationTimeout
()

Allows indicating if the connector supports per operation timeout.

public boolean

Returns:

true if the wrapper has been closed
isClosed
()

public boolean

Returns:

true if the endpoint supports asynchronous IO and a read operation is being processed asynchronously
isReadPending
()

Allows checking if an asynchronous read operation is currently pending.

public abstract boolean
public boolean

Returns:

true if no writes are pending and data can be written otherwise false
isReadyForWrite
()

Checks to see if there are any writes pending and if there are calls registerWriteInterest() to trigger a callback once the pending writes have completed.

public boolean

Returns:

true if the endpoint supports asynchronous IO and a write operation is being processed asynchronously
isWritePending
()

Allows checking if an asynchronous write operation is currently pending.

public boolean

Returns:

This default implementation always returns false
needSemaphores
()

Allows indicating if the connector needs semaphores.

protected abstract <A> SocketWrapperBase<E>.OperationState<A>
newOperationState(boolean read, ByteBuffer[] buffers, int offset, int length, SocketWrapperBase.BlockingMode block, long timeout, TimeUnit unit, A attachment, SocketWrapperBase.CompletionCheck check, CompletionHandler<Long, ? super A> handler, Semaphore semaphore, SocketWrapperBase<E>.VectoredIOCompletionHandler<A> completion)

protected abstract void
protected abstract void
protected abstract void
protected int
populateReadBuffer(byte[] b, int off, int len)

protected int
protected abstract void
protected abstract void
protected abstract void
public abstract SendfileState

Returns:

The state of the sendfile process after the first write.
processSendfile
(SendfileDataBase
Data representing the file to send
sendfileData
)

Starts the sendfile process.

public void
processSocket(SocketEvent socketStatus, boolean dispatch)

public abstract int
read(boolean block, byte[] b, int off, int len)

public abstract int
read(boolean block, ByteBuffer to)

public final <
The attachment type
A
>
SocketWrapperBase.CompletionState

Returns:

the completion state (done, done inline, or still pending)
read
(long
timeout duration for the read
timeout
,
TimeUnit
units for the timeout duration
unit
,
A
an object to attach to the I/O operation that will be used when calling the completion handler
attachment
,
CompletionHandler<Long, ? super A>
to call when the IO is complete
handler
,
ByteBuffer...
buffers
dsts
)

Scatter read.

public final <
The attachment type
A
>
SocketWrapperBase.CompletionState

Returns:

the completion state (done, done inline, or still pending)
read
(SocketWrapperBase.BlockingMode
is the blocking mode that will be used for this operation
block
,
long
timeout duration for the read
timeout
,
TimeUnit
units for the timeout duration
unit
,
A
an object to attach to the I/O operation that will be used when calling the completion handler
attachment
,
SocketWrapperBase.CompletionCheck
for the IO operation completion
check
,
CompletionHandler<Long, ? super A>
to call when the IO is complete
handler
,
ByteBuffer...
buffers
dsts
)

Scatter read.

public final <
The attachment type
A
>
SocketWrapperBase.CompletionState

Returns:

the completion state (done, done inline, or still pending)
read
(ByteBuffer[]
buffers
dsts
,
int
in the buffer array
offset
,
int
in the buffer array
length
,
SocketWrapperBase.BlockingMode
is the blocking mode that will be used for this operation
block
,
long
timeout duration for the read
timeout
,
TimeUnit
units for the timeout duration
unit
,
A
an object to attach to the I/O operation that will be used when calling the completion handler
attachment
,
SocketWrapperBase.CompletionCheck
for the IO operation completion
check
,
CompletionHandler<Long, ? super A>
to call when the IO is complete
handler
)

Scatter read.

public abstract void
public abstract void
protected void
reset(E closedSocket)

public abstract void
public void
setCurrentProcessor(Object currentProcessor)

public void
public void
setKeepAliveLeft(int keepAliveLeft)

public void
setNegotiatedProtocol(String negotiatedProtocol)

public void
setReadTimeout(long
The timeout in milliseconds. A value of -1 indicates an infinite timeout.
readTimeout
)

Set the timeout for reading.

public void
setWriteTimeout(long
The timeout in milliseconds. A value of zero or less indicates an infinite timeout.
writeTimeout
)

Set the timeout for writing.

public Object
public String
toString()

Overrides java.lang.Object.toString.

Overridden for debug purposes.

protected static int
transfer(byte[] from, int offset, int length, ByteBuffer to)

protected static int
public void
unRead(ByteBuffer
The input to return to the input buffer.
returnedInput
)

Return input that has been read to the input buffer for re-reading by the correct component.

protected final <
The attachment type
A
>
SocketWrapperBase.CompletionState

Returns:

the completion state (done, done inline, or still pending)
vectoredOperation
(boolean
true if the operation is a read, false if it is a write
read
,
ByteBuffer[]
buffers
buffers
,
int
in the buffer array
offset
,
int
in the buffer array
length
,
SocketWrapperBase.BlockingMode
is the blocking mode that will be used for this operation
block
,
long
timeout duration for the write
timeout
,
TimeUnit
units for the timeout duration
unit
,
A
an object to attach to the I/O operation that will be used when calling the completion handler
attachment
,
SocketWrapperBase.CompletionCheck
for the IO operation completion
check
,
CompletionHandler<Long, ? super A>
to call when the IO is complete
handler
)

Vectored operation.

public final void
write(boolean
true if a blocking write should be used, otherwise a non-blocking write will be used
block
,
byte[]
The byte array containing the data to be written
buf
,
int
The offset within the byte array of the data to be written
off
,
int
The length of the data to be written
len
)

Writes the provided data to the socket write buffer.

public final void
write(boolean
true if a blocking write should be used, otherwise a non-blocking write will be used
block
,
ByteBuffer
The ByteBuffer containing the data to be written
from
)

Writes the provided data to the socket write buffer.

public final <
The attachment type
A
>
SocketWrapperBase.CompletionState

Returns:

the completion state (done, done inline, or still pending)
write
(long
timeout duration for the write
timeout
,
TimeUnit
units for the timeout duration
unit
,
A
an object to attach to the I/O operation that will be used when calling the completion handler
attachment
,
CompletionHandler<Long, ? super A>
to call when the IO is complete
handler
,
ByteBuffer...
buffers
srcs
)

Gather write.

public final <
The attachment type
A
>
SocketWrapperBase.CompletionState

Returns:

the completion state (done, done inline, or still pending)
write
(SocketWrapperBase.BlockingMode
is the blocking mode that will be used for this operation
block
,
long
timeout duration for the write
timeout
,
TimeUnit
units for the timeout duration
unit
,
A
an object to attach to the I/O operation that will be used when calling the completion handler
attachment
,
SocketWrapperBase.CompletionCheck
for the IO operation completion
check
,
CompletionHandler<Long, ? super A>
to call when the IO is complete
handler
,
ByteBuffer...
buffers
srcs
)

Gather write.

public final <
The attachment type
A
>
SocketWrapperBase.CompletionState

Returns:

the completion state (done, done inline, or still pending)
write
(ByteBuffer[]
buffers
srcs
,
int
in the buffer array
offset
,
int
in the buffer array
length
,
SocketWrapperBase.BlockingMode
is the blocking mode that will be used for this operation
block
,
long
timeout duration for the write
timeout
,
TimeUnit
units for the timeout duration
unit
,
A
an object to attach to the I/O operation that will be used when calling the completion handler
attachment
,
SocketWrapperBase.CompletionCheck
for the IO operation completion
check
,
CompletionHandler<Long, ? super A>
to call when the IO is complete
handler
)

Gather write.

protected void
writeBlocking(byte[]
The byte array containing the data to be written
buf
,
int
The offset within the byte array of the data to be written
off
,
int
The length of the data to be written
len
)

Writes the provided data to the socket write buffer.

protected void
writeBlocking(ByteBuffer
The ByteBuffer containing the data to be written
from
)

Writes the provided data to the socket write buffer.

protected void
writeNonBlocking(byte[]
The byte array containing the data to be written
buf
,
int
The offset within the byte array of the data to be written
off
,
int
The length of the data to be written
len
)

Transfers the data to the socket write buffer (writing that data to the socket if the buffer fills up using a non-blocking write) until either all the data has been transferred and space remains in the socket write buffer or a non-blocking write leaves data in the socket write buffer.

protected void
writeNonBlocking(ByteBuffer
The ByteBuffer containing the data to be written
from
)

Transfers the data to the socket write buffer (writing that data to the socket if the buffer fills up using a non-blocking write) until either all the data has been transferred and space remains in the socket write buffer or a non-blocking write leaves data in the socket write buffer.

protected void
writeNonBlockingInternal(ByteBuffer
The ByteBuffer containing the data to be written
from
)

Separate method so it can be re-used by the socket write buffer to write data to the network

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait