- java.lang.Object
-
- org.xnio.channels.SplitStreamSourceChannel
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.nio.channels.Channel
,java.nio.channels.InterruptibleChannel
,java.nio.channels.ReadableByteChannel
,java.nio.channels.ScatteringByteChannel
,CloseableChannel
,CloseListenerSettable<SplitStreamSourceChannel>
,Configurable
,ReadListenerSettable<SplitStreamSourceChannel>
,StreamSourceChannel
,SuspendableReadChannel
public final class SplitStreamSourceChannel extends java.lang.Object implements StreamSourceChannel, ReadListenerSettable<SplitStreamSourceChannel>, CloseListenerSettable<SplitStreamSourceChannel>
A half-duplex (read side) wrapper for a full-duplex channel.- Author:
- David M. Lloyd
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.xnio.channels.CloseListenerSettable
CloseListenerSettable.Setter<C extends java.nio.channels.Channel>
-
Nested classes/interfaces inherited from interface org.xnio.channels.ReadListenerSettable
ReadListenerSettable.Setter<C extends java.nio.channels.Channel>
-
-
Field Summary
-
Fields inherited from interface org.xnio.channels.Configurable
EMPTY
-
-
Constructor Summary
Constructors Constructor Description SplitStreamSourceChannel(StreamSourceChannel delegate)
Construct a new instance which doesn't delegate configuration.SplitStreamSourceChannel(StreamSourceChannel delegate, boolean delegateConfig)
Construct a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
awaitReadable()
Block until this channel becomes readable again.void
awaitReadable(long time, java.util.concurrent.TimeUnit timeUnit)
Block until this channel becomes readable again, or until the timeout expires.void
close()
Close this channel.ChannelListener<? super SplitStreamSourceChannel>
getCloseListener()
Get the close listener.ChannelListener.Setter<? extends SplitStreamSourceChannel>
getCloseSetter()
Get the setter which can be used to change the close listener for this channel.XnioIoThread
getIoThread()
Get the I/O thread associated with this channel.<T> T
getOption(Option<T> option)
Get the value of a channel option.ChannelListener<? super SplitStreamSourceChannel>
getReadListener()
Get the read listener.ChannelListener.Setter<? extends SplitStreamSourceChannel>
getReadSetter()
Get the setter which can be used to change the read listener for this channel.XnioExecutor
getReadThread()
Deprecated.XnioWorker
getWorker()
Get the worker for this channel.boolean
isOpen()
boolean
isReadResumed()
Determine whether reads are resumed.int
read(java.nio.ByteBuffer dst)
long
read(java.nio.ByteBuffer[] dsts)
long
read(java.nio.ByteBuffer[] dsts, int offset, int length)
void
resumeReads()
Resume reads on this channel.void
setCloseListener(ChannelListener<? super SplitStreamSourceChannel> closeListener)
Set the close listener.<T> T
setOption(Option<T> option, T value)
Set an option for this channel.void
setReadListener(ChannelListener<? super SplitStreamSourceChannel> readListener)
Set the read listener.void
shutdownReads()
Places this readable channel at "end of stream".boolean
supportsOption(Option<?> option)
Determine whether an option is supported on this channel.void
suspendReads()
Suspend further read notifications on this channel.long
transferTo(long position, long count, java.nio.channels.FileChannel target)
Transfers bytes into the given file from this channel.long
transferTo(long count, java.nio.ByteBuffer throughBuffer, StreamSinkChannel target)
Transfers bytes into the given channel target.void
wakeupReads()
Resume reads
on this channel, and force the read listener to be triggered even if the channel isn't actually readable.
-
-
-
Constructor Detail
-
SplitStreamSourceChannel
public SplitStreamSourceChannel(StreamSourceChannel delegate, boolean delegateConfig)
Construct a new instance.- Parameters:
delegate
- the delegate channeldelegateConfig
-true
to delegate configuration,false
otherwise
-
SplitStreamSourceChannel
public SplitStreamSourceChannel(StreamSourceChannel delegate)
Construct a new instance which doesn't delegate configuration.- Parameters:
delegate
- the delegate channel
-
-
Method Detail
-
setReadListener
public void setReadListener(ChannelListener<? super SplitStreamSourceChannel> readListener)
Description copied from interface:ReadListenerSettable
Set the read listener.- Specified by:
setReadListener
in interfaceReadListenerSettable<SplitStreamSourceChannel>
- Parameters:
readListener
- the read listener
-
getReadListener
public ChannelListener<? super SplitStreamSourceChannel> getReadListener()
Description copied from interface:ReadListenerSettable
Get the read listener.- Specified by:
getReadListener
in interfaceReadListenerSettable<SplitStreamSourceChannel>
- Returns:
- the read listener
-
setCloseListener
public void setCloseListener(ChannelListener<? super SplitStreamSourceChannel> closeListener)
Description copied from interface:CloseListenerSettable
Set the close listener.- Specified by:
setCloseListener
in interfaceCloseListenerSettable<SplitStreamSourceChannel>
- Parameters:
closeListener
- the close listener
-
getCloseListener
public ChannelListener<? super SplitStreamSourceChannel> getCloseListener()
Description copied from interface:CloseListenerSettable
Get the close listener.- Specified by:
getCloseListener
in interfaceCloseListenerSettable<SplitStreamSourceChannel>
- Returns:
- the close listener
-
getReadSetter
public ChannelListener.Setter<? extends SplitStreamSourceChannel> getReadSetter()
Description copied from interface:StreamSourceChannel
Get the setter which can be used to change the read listener for this channel.- Specified by:
getReadSetter
in interfaceStreamSourceChannel
- Specified by:
getReadSetter
in interfaceSuspendableReadChannel
- Returns:
- the setter
-
getCloseSetter
public ChannelListener.Setter<? extends SplitStreamSourceChannel> getCloseSetter()
Description copied from interface:StreamSourceChannel
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 interfaceStreamSourceChannel
- Specified by:
getCloseSetter
in interfaceSuspendableReadChannel
- Returns:
- the setter
-
transferTo
public long transferTo(long position, long count, java.nio.channels.FileChannel target) throws java.io.IOException
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:
java.io.IOException
- if an I/O error occurs
-
transferTo
public long transferTo(long count, java.nio.ByteBuffer throughBuffer, StreamSinkChannel target) throws java.io.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:
java.io.IOException
- if an I/O error occurs
-
read
public int read(java.nio.ByteBuffer dst) throws java.io.IOException
- Specified by:
read
in interfacejava.nio.channels.ReadableByteChannel
- Throws:
java.io.IOException
-
read
public long read(java.nio.ByteBuffer[] dsts, int offset, int length) throws java.io.IOException
- Specified by:
read
in interfacejava.nio.channels.ScatteringByteChannel
- Throws:
java.io.IOException
-
read
public long read(java.nio.ByteBuffer[] dsts) throws java.io.IOException
- Specified by:
read
in interfacejava.nio.channels.ScatteringByteChannel
- Throws:
java.io.IOException
-
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
-
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
-
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
-
awaitReadable
public void awaitReadable() throws java.io.IOException
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:
java.io.InterruptedIOException
- if the operation is interrupted; the thread's interrupt flag will be set as welljava.io.IOException
- if an I/O error occurs
-
awaitReadable
public void awaitReadable(long time, java.util.concurrent.TimeUnit timeUnit) throws java.io.IOException
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:
java.io.InterruptedIOException
- if the operation is interrupted; the thread's interrupt flag will be set as welljava.io.IOException
- if an I/O error occurs
-
getWorker
public XnioWorker getWorker()
Description copied from interface:CloseableChannel
Get the worker for this channel.- Specified by:
getWorker
in interfaceCloseableChannel
- Returns:
- the worker
-
supportsOption
public boolean supportsOption(Option<?> option)
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
public <T> T getOption(Option<T> option) throws java.io.IOException
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:
java.io.IOException
- if an I/O error occurred when reading the option
-
setOption
public <T> T setOption(Option<T> option, T value) throws java.lang.IllegalArgumentException, java.io.IOException
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:
java.lang.IllegalArgumentException
- if the value is not acceptable for this optionjava.io.IOException
- if an I/O error occurred when modifying the option
-
shutdownReads
public void shutdownReads() throws java.io.IOException
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:
java.io.IOException
- if an I/O error occurs
-
getReadThread
@Deprecated public XnioExecutor 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
public XnioIoThread 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
-
isOpen
public boolean isOpen()
- Specified by:
isOpen
in interfacejava.nio.channels.Channel
-
close
public void close() throws java.io.IOException
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 interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.nio.channels.Channel
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in interfaceCloseableChannel
- Specified by:
close
in interfacejava.nio.channels.InterruptibleChannel
- Throws:
java.io.IOException
- if the close failed
-
-