- All Implemented Interfaces:
Closeable
,AutoCloseable
,ByteChannel
,Channel
,GatheringByteChannel
,InterruptibleChannel
,ReadableByteChannel
,ScatteringByteChannel
,WritableByteChannel
,ByteChannel
,CloseableChannel
,Configurable
,StreamChannel
,StreamSinkChannel
,StreamSourceChannel
,SuspendableChannel
,SuspendableReadChannel
,SuspendableWriteChannel
- Direct Known Subclasses:
AssembledConnectedStreamChannel
- Author:
- David M. Lloyd
-
Field Summary
Fields inherited from interface org.xnio.channels.Configurable
EMPTY
-
Constructor Summary
ConstructorsConstructorDescriptionAssembledStreamChannel
(CloseableChannel closeable, StreamSourceChannel source, StreamSinkChannel sink) Construct a new instance.AssembledStreamChannel
(StreamSourceChannel source, StreamSinkChannel sink) Construct a new instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Block until this channel becomes readable again.void
awaitReadable
(long time, TimeUnit timeUnit) Block until this channel becomes readable again, or until the timeout expires.void
Block until this channel becomes writable again.void
awaitWritable
(long time, TimeUnit timeUnit) Block until this channel becomes writable again, or until the timeout expires.void
close()
Close this channel.boolean
flush()
Flush any waiting partial send or write.ChannelListener.Setter<? extends AssembledStreamChannel>
Get the setter which can be used to change the close listener for this channel.Get the I/O thread associated with this channel.<T> T
Get the value of a channel option.ChannelListener.Setter<? extends AssembledStreamChannel>
Get the setter which can be used to change the read listener for this channel.Deprecated.Get the worker for this channel.ChannelListener.Setter<? extends AssembledStreamChannel>
Get the setter which can be used to change the write listener for this channel.Deprecated.boolean
isOpen()
Determine whether this channel is open.boolean
Determine whether reads are resumed.boolean
Determine whether writes are resumed.int
read
(ByteBuffer dst) long
read
(ByteBuffer[] dsts) long
read
(ByteBuffer[] dsts, int offset, int length) void
Resume reads on this channel.void
Resume writes on this channel.<T> T
Set an option for this channel.void
Places this readable channel at "end of stream".void
Indicate that writing is complete for this channel.boolean
supportsOption
(Option<?> option) Determine whether an option is supported on this channel.void
Suspend further read notifications on this channel.void
Suspend further write notifications on this channel.long
transferFrom
(FileChannel src, long position, long count) Transfer bytes into this channel from the given file.long
transferFrom
(StreamSourceChannel source, long count, ByteBuffer throughBuffer) Transfers bytes from the given channel source.long
transferTo
(long position, long count, FileChannel target) Transfers bytes into the given file from this channel.long
transferTo
(long count, ByteBuffer throughBuffer, StreamSinkChannel target) Transfers bytes into the given channel target.void
Resume reads
on this channel, and force the read listener to be triggered even if the channel isn't actually readable.void
Resume writes
on this channel, and force the write listener to be triggered even if the channel isn't actually writable.int
write
(ByteBuffer src) long
write
(ByteBuffer[] srcs) long
write
(ByteBuffer[] srcs, int offset, int length) int
writeFinal
(ByteBuffer src) Writes some data to the channel, with the same semantics asWritableByteChannel.write(java.nio.ByteBuffer)
.long
writeFinal
(ByteBuffer[] srcs) Writes some data to the channel, with the same semantics asGatheringByteChannel.write(java.nio.ByteBuffer[])
.long
writeFinal
(ByteBuffer[] srcs, int offset, int length) Writes some data to the channel, with the same semantics asGatheringByteChannel.write(java.nio.ByteBuffer[], int, int)
.
-
Constructor Details
-
AssembledStreamChannel
public AssembledStreamChannel(CloseableChannel closeable, StreamSourceChannel source, StreamSinkChannel sink) Construct a new instance.- Parameters:
closeable
- the single central closeable channelsource
- the stream source channelsink
- the stream sink channel
-
AssembledStreamChannel
Construct a new instance.- Parameters:
source
- the stream source channelsink
- the stream sink channel
-
-
Method Details
-
getReadSetter
Description copied from interface:StreamChannel
Get the setter which can be used to change the read listener for this channel.- Specified by:
getReadSetter
in interfaceStreamChannel
- Specified by:
getReadSetter
in interfaceStreamSourceChannel
- Specified by:
getReadSetter
in interfaceSuspendableChannel
- Specified by:
getReadSetter
in interfaceSuspendableReadChannel
- Returns:
- the setter
-
suspendReads
public void suspendReads()Description copied from interface:SuspendableReadChannel
Suspend further read notifications on this channel.- Specified by:
suspendReads
in interfaceSuspendableReadChannel
-
resumeReads
public void resumeReads()Description copied from interface:SuspendableReadChannel
Resume reads on this channel. The read listener will be called as soon as there is data available to be read.- Specified by:
resumeReads
in interfaceSuspendableReadChannel
-
isReadResumed
public boolean isReadResumed()Description copied from interface:SuspendableReadChannel
Determine whether reads are resumed.- Specified by:
isReadResumed
in interfaceSuspendableReadChannel
- Returns:
true
if reads are resumed,false
if reads are suspended
-
wakeupReads
public void wakeupReads()Description copied from interface:SuspendableReadChannel
Resume reads
on this channel, and force the read listener to be triggered even if the channel isn't actually readable.- Specified by:
wakeupReads
in interfaceSuspendableReadChannel
-
shutdownReads
Description copied from interface:SuspendableReadChannel
Places this readable channel at "end of stream". Further reads will result in EOF. Shutting down all directions of a channel will causeCloseableChannel.close()
to be called automatically.- Specified by:
shutdownReads
in interfaceSuspendableReadChannel
- Throws:
IOException
- if an I/O error occurs
-
awaitReadable
Description copied from interface:SuspendableReadChannel
Block until this channel becomes readable again. This method may return spuriously before the channel becomes readable.- Specified by:
awaitReadable
in interfaceSuspendableReadChannel
- Throws:
InterruptedIOException
- if the operation is interrupted; the thread's interrupt flag will be set as wellIOException
- if an I/O error occurs
-
awaitReadable
Description copied from interface:SuspendableReadChannel
Block until this channel becomes readable again, or until the timeout expires. This method may return spuriously before the channel becomes readable or the timeout expires.- Specified by:
awaitReadable
in interfaceSuspendableReadChannel
- Parameters:
time
- the time to waittimeUnit
- the time unit- Throws:
InterruptedIOException
- if the operation is interrupted; the thread's interrupt flag will be set as wellIOException
- if an I/O error occurs
-
getReadThread
Deprecated.Description copied from interface:SuspendableReadChannel
Get the read thread for this channel.- Specified by:
getReadThread
in interfaceSuspendableReadChannel
- Returns:
- the thread, or
null
if none is configured or available
-
getIoThread
Description copied from interface:CloseableChannel
Get the I/O thread associated with this channel.- Specified by:
getIoThread
in interfaceCloseableChannel
- Returns:
- the I/O thread associated with this channel
-
read
- Specified by:
read
in interfaceReadableByteChannel
- Throws:
IOException
-
read
- Specified by:
read
in interfaceScatteringByteChannel
- Throws:
IOException
-
read
- Specified by:
read
in interfaceScatteringByteChannel
- Throws:
IOException
-
transferTo
Description copied from interface:StreamSourceChannel
Transfers bytes into the given file from this channel. Using this method in preference toFileChannel.transferFrom(ReadableByteChannel, long, long)
may provide a performance advantage on some platforms.If the current thread is interrupted when this method is called, it may throw a
InterruptedIOException
; however, if this exception is thrown, theInterruptedIOException.bytesTransferred
field is guaranteed to be 0.- Specified by:
transferTo
in interfaceStreamSourceChannel
- Parameters:
position
- the position within the file from which the transfer is to begincount
- the number of bytes to be transferredtarget
- the file to write to- Returns:
- the number of bytes (possibly 0) that were actually transferred
- Throws:
IOException
- if an I/O error occurs
-
transferTo
public long transferTo(long count, ByteBuffer throughBuffer, StreamSinkChannel target) throws IOException Description copied from interface:StreamSourceChannel
Transfers bytes into the given channel target. On some platforms, this may avoid copying bytes between user and kernel space. On other platforms, bytes are passed through thethroughBuffer
parameter's buffer space. On entry,throughBuffer
will be cleared. On exit, the buffer will be flipped for emptying, and may possibly be empty or may contain data. If this method returns a value less thancount
, then the remaining data inthroughBuffer
may contain data read from this channel which must be written totarget
to complete the operation. Note that using a direct buffer may provide an intermediate performance gain on platforms without zero-copy facilities.If the current thread is interrupted when this method is called, it may throw a
InterruptedIOException
; however, if this exception is thrown, theInterruptedIOException.bytesTransferred
field is guaranteed to be 0. Note that the return value is the amount of data that was actually transferred to theStreamSinkChannel
. The actual amount of data read could be larger than this, and can be calculated by adding the return value and the amount of data left inthroughBuffer
.- Specified by:
transferTo
in interfaceStreamSourceChannel
- Parameters:
count
- the number of bytes to be transferredthroughBuffer
- the buffer to copy through.target
- the destination to write to- Returns:
- the number of bytes (possibly 0) that were actually transferred, or -1 if the end of input was reached
- Throws:
IOException
- if an I/O error occurs
-
transferFrom
Description copied from interface:StreamSinkChannel
Transfer bytes into this channel from the given file. Using this method in preference toFileChannel.transferTo(long, long, WritableByteChannel)
may provide a performance advantage on some platforms.If the current thread is interrupted when this method is called, it may throw a
InterruptedIOException
; however, if this exception is thrown, theInterruptedIOException.bytesTransferred
field is guaranteed to be 0.- Specified by:
transferFrom
in interfaceStreamSinkChannel
- Parameters:
src
- the file to read fromposition
- the position within the file from which the transfer is to begincount
- the number of bytes to be transferred- Returns:
- the number of bytes (possibly 0) that were actually transferred
- Throws:
IOException
- if an I/O error occurs
-
transferFrom
public long transferFrom(StreamSourceChannel source, long count, ByteBuffer throughBuffer) throws IOException Description copied from interface:StreamSinkChannel
Transfers bytes from the given channel source. On some platforms, this may avoid copying bytes between user and kernel space. On other platforms, bytes are passed through thethroughBuffer
parameter's buffer space. On entry,throughBuffer
will be cleared. On exit, the buffer will be flipped for emptying, and may be empty or may contain data. If this method returns a value less thancount
, then the remaining data inthroughBuffer
may contain data read fromsource
which must be written to this channel to complete the operation. Note that using a direct buffer may provide an intermediate performance gain on platforms without zero-copy facilities.If the current thread is interrupted when this method is called, it may throw a
InterruptedIOException
; however, if this exception is thrown, theInterruptedIOException.bytesTransferred
field is guaranteed to be 0.- Specified by:
transferFrom
in interfaceStreamSinkChannel
- Parameters:
source
- the source to read fromcount
- the number of bytes to be transferredthroughBuffer
- the buffer to copy through.- Returns:
- the number of bytes (possibly 0) that were actually transferred, or -1 if the end of input was reached
- Throws:
IOException
- if an I/O error occurs
-
getWriteSetter
Description copied from interface:StreamChannel
Get the setter which can be used to change the write listener for this channel.- Specified by:
getWriteSetter
in interfaceStreamChannel
- Specified by:
getWriteSetter
in interfaceStreamSinkChannel
- Specified by:
getWriteSetter
in interfaceSuspendableChannel
- Specified by:
getWriteSetter
in interfaceSuspendableWriteChannel
- Returns:
- the setter
-
write
- Specified by:
write
in interfaceWritableByteChannel
- Throws:
IOException
-
write
- Specified by:
write
in interfaceGatheringByteChannel
- Throws:
IOException
-
write
- Specified by:
write
in interfaceGatheringByteChannel
- Throws:
IOException
-
suspendWrites
public void suspendWrites()Description copied from interface:SuspendableWriteChannel
Suspend further write notifications on this channel.- Specified by:
suspendWrites
in interfaceSuspendableWriteChannel
-
resumeWrites
public void resumeWrites()Description copied from interface:SuspendableWriteChannel
Resume writes on this channel. The write listener will be called as soon as the channel becomes writable.- Specified by:
resumeWrites
in interfaceSuspendableWriteChannel
-
isWriteResumed
public boolean isWriteResumed()Description copied from interface:SuspendableWriteChannel
Determine whether writes are resumed.- Specified by:
isWriteResumed
in interfaceSuspendableWriteChannel
- Returns:
true
if writes are resumed,false
if writes are suspended
-
wakeupWrites
public void wakeupWrites()Description copied from interface:SuspendableWriteChannel
Resume writes
on this channel, and force the write listener to be triggered even if the channel isn't actually writable.- Specified by:
wakeupWrites
in interfaceSuspendableWriteChannel
-
shutdownWrites
Description copied from interface:SuspendableWriteChannel
Indicate that writing is complete for this channel. Further attempts to write data to this channel after this method is invoked will result in an exception. If this method was already called, calling this method again will have no additional effect. After this method is called, any remaining data still must be flushed out via theSuspendableWriteChannel.flush()
method; once this is done, if the read side of the channel was shut down, the channel will automatically close.- Specified by:
shutdownWrites
in interfaceSuspendableWriteChannel
- Throws:
IOException
- if an I/O error occurs
-
awaitWritable
Description copied from interface:SuspendableWriteChannel
Block until this channel becomes writable again. This method may return spuriously before the channel becomes writable.- Specified by:
awaitWritable
in interfaceSuspendableWriteChannel
- Throws:
InterruptedIOException
- if the operation is interrupted; the thread's interrupt flag will be set as wellIOException
- if an I/O error occurs
-
awaitWritable
Description copied from interface:SuspendableWriteChannel
Block until this channel becomes writable again, or until the timeout expires. This method may return spuriously before the channel becomes writable or the timeout expires.- Specified by:
awaitWritable
in interfaceSuspendableWriteChannel
- Parameters:
time
- the time to waittimeUnit
- the time unit- Throws:
InterruptedIOException
- if the operation is interrupted; the thread's interrupt flag will be set as wellIOException
- if an I/O error occurs
-
getWriteThread
Deprecated.Description copied from interface:SuspendableWriteChannel
Get the write thread for this channel.- Specified by:
getWriteThread
in interfaceSuspendableWriteChannel
- Returns:
- the thread, or
null
if none is configured or available
-
flush
Description copied from interface:SuspendableWriteChannel
Flush any waiting partial send or write. If there is no data to flush, or if the flush completed successfully, this method will returntrue
. If there is data to flush which cannot be immediately written, this method will returnfalse
. If this method returnstrue
afterSuspendableWriteChannel.shutdownWrites()
was called on this channel, the write listener will no longer be invoked on this channel. If this is case and additionally this is a write-only channel or the read side was previously shut down, then the channel will automatically be closed.- Specified by:
flush
in interfaceSuspendableWriteChannel
- Returns:
true
if the message was flushed, orfalse
if the result would block- Throws:
IOException
- if an I/O error occurs
-
getCloseSetter
Description copied from interface:StreamChannel
Get the setter which can be used to change the close listener for this channel. If the channel is already closed, then the listener will not be called.- Specified by:
getCloseSetter
in interfaceCloseableChannel
- Specified by:
getCloseSetter
in interfaceStreamChannel
- Specified by:
getCloseSetter
in interfaceStreamSinkChannel
- Specified by:
getCloseSetter
in interfaceStreamSourceChannel
- Specified by:
getCloseSetter
in interfaceSuspendableChannel
- Specified by:
getCloseSetter
in interfaceSuspendableReadChannel
- Specified by:
getCloseSetter
in interfaceSuspendableWriteChannel
- Returns:
- the setter
-
writeFinal
Description copied from interface:StreamSinkChannel
Writes some data to the channel, with the same semantics asWritableByteChannel.write(java.nio.ByteBuffer)
. If all the data is written out then the channel will have its writes shutdown. Semantically this method is equivalent to:int rem = src.remaining(); int written = channel.write(src); if(written == rem) { channel.shutdownWrites() }
If an exception is thrown the caller is still responsible for closing the channel.- Specified by:
writeFinal
in interfaceStreamSinkChannel
- Parameters:
src
- The data to write- Returns:
- The amount of data that was actually written.
- Throws:
IOException
- See Also:
-
writeFinal
Description copied from interface:StreamSinkChannel
Writes some data to the channel, with the same semantics asGatheringByteChannel.write(java.nio.ByteBuffer[], int, int)
. If all the data is written out then the channel will have its writes shutdown. If an exception is thrown the caller is still responsible for closing the channel.- Specified by:
writeFinal
in interfaceStreamSinkChannel
- Parameters:
srcs
- The buffers from which bytes are to be retrievedoffset
- The offset within the buffer array of the first buffer from which bytes are to be retrieved; must be non-negative and no larger than srcs.lengthlength
- The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset- Returns:
- The amount of data that was actually written
- Throws:
IOException
- See Also:
-
writeFinal
Description copied from interface:StreamSinkChannel
Writes some data to the channel, with the same semantics asGatheringByteChannel.write(java.nio.ByteBuffer[])
. If all the data is written out then the channel will have its writes shutdown. If an exception is thrown the caller is still responsible for closing the channel.- Specified by:
writeFinal
in interfaceStreamSinkChannel
- Parameters:
srcs
- The buffers from which bytes are to be retrieved- Returns:
- The amount of data that was actually written
- Throws:
IOException
- See Also:
-
getWorker
Description copied from interface:CloseableChannel
Get the worker for this channel.- Specified by:
getWorker
in interfaceCloseableChannel
- Returns:
- the worker
-
close
Description copied from interface:CloseableChannel
Close this channel. When a channel is closed, its close listener is invoked. Invoking this method more than once has no additional effect.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceChannel
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceCloseableChannel
- Specified by:
close
in interfaceInterruptibleChannel
- Specified by:
close
in interfaceSuspendableWriteChannel
- Throws:
IOException
- if the close failed
-
isOpen
public boolean isOpen()Description copied from interface:SuspendableWriteChannel
Determine whether this channel is open. This method will returnfalse
if all directions are shut down, even if there is unflushed write data pending.- Specified by:
isOpen
in interfaceChannel
- Specified by:
isOpen
in interfaceSuspendableWriteChannel
- Returns:
true
if the channel is open,false
otherwise
-
supportsOption
Description copied from interface:Configurable
Determine whether an option is supported on this channel.- Specified by:
supportsOption
in interfaceConfigurable
- Parameters:
option
- the option- Returns:
true
if it is supported
-
getOption
Description copied from interface:Configurable
Get the value of a channel option.- Specified by:
getOption
in interfaceConfigurable
- Type Parameters:
T
- the type of the option value- Parameters:
option
- the option to get- Returns:
- the value of the option, or
null
if it is not set - Throws:
IOException
- if an I/O error occurred when reading the option
-
setOption
Description copied from interface:Configurable
Set an option for this channel. Unsupported options are ignored.- Specified by:
setOption
in interfaceConfigurable
- Type Parameters:
T
- the type of the option value- Parameters:
option
- the option to setvalue
- the value of the option to set- Returns:
- the previous option value, if any
- Throws:
IllegalArgumentException
- if the value is not acceptable for this optionIOException
- if an I/O error occurred when modifying the option
-