public final class FixedLengthStreamSinkChannel extends Object implements StreamSinkChannel, ProtectedWrappedChannel<StreamSinkChannel>, WriteListenerSettable<FixedLengthStreamSinkChannel>, CloseListenerSettable<FixedLengthStreamSinkChannel>
WriteListenerSettable.Setter<C extends Channel>
CloseListenerSettable.Setter<C extends Channel>
EMPTY
Constructor and Description |
---|
FixedLengthStreamSinkChannel(StreamSinkChannel delegate,
long contentLength,
boolean configurable,
boolean propagateClose,
ChannelListener<? super FixedLengthStreamSinkChannel> finishListener,
Object guard)
Construct a new instance.
|
Modifier and Type | Method and Description |
---|---|
void |
awaitWritable()
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.
|
StreamSinkChannel |
getChannel(Object guard)
Get the channel which is wrapped by this object, or
null if the guard is invalid. |
ChannelListener<? super FixedLengthStreamSinkChannel> |
getCloseListener()
Get the close listener.
|
ChannelListener.Setter<FixedLengthStreamSinkChannel> |
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.
|
long |
getRemaining()
Get the number of remaining bytes in this fixed length channel.
|
XnioWorker |
getWorker()
Get the worker for this channel.
|
ChannelListener<? super FixedLengthStreamSinkChannel> |
getWriteListener()
Get the write listener.
|
ChannelListener.Setter<FixedLengthStreamSinkChannel> |
getWriteSetter()
Get the setter which can be used to change the write listener for this channel.
|
XnioExecutor |
getWriteThread()
Deprecated.
|
boolean |
isOpen()
Determine whether this channel is open.
|
boolean |
isWriteResumed()
Determine whether writes are resumed.
|
void |
resumeWrites()
Resume writes on this channel.
|
void |
setCloseListener(ChannelListener<? super FixedLengthStreamSinkChannel> listener)
Set the close listener.
|
<T> T |
setOption(Option<T> option,
T value)
Set an option for this channel.
|
void |
setWriteListener(ChannelListener<? super FixedLengthStreamSinkChannel> listener)
Set the write listener.
|
void |
shutdownWrites()
Indicate that writing is complete for this channel.
|
boolean |
supportsOption(Option<?> option)
Determine whether an option is supported on this channel.
|
void |
suspendWrites()
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.
|
void |
wakeupWrites()
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 as
WritableByteChannel.write(java.nio.ByteBuffer) . |
long |
writeFinal(ByteBuffer[] srcs)
Writes some data to the channel, with the same semantics as
GatheringByteChannel.write(java.nio.ByteBuffer[]) . |
long |
writeFinal(ByteBuffer[] srcs,
int offset,
int length)
Writes some data to the channel, with the same semantics as
GatheringByteChannel.write(java.nio.ByteBuffer[], int, int) . |
public FixedLengthStreamSinkChannel(StreamSinkChannel delegate, long contentLength, boolean configurable, boolean propagateClose, ChannelListener<? super FixedLengthStreamSinkChannel> finishListener, Object guard)
delegate
- the delegate channelcontentLength
- the content lengthconfigurable
- true
if this instance should pass configuration to the delegatepropagateClose
- true
if this instance should pass close to the delegatefinishListener
- the listener to call when the channel is closed or the length is reachedguard
- the guard object to usepublic void setWriteListener(ChannelListener<? super FixedLengthStreamSinkChannel> listener)
WriteListenerSettable
setWriteListener
in interface WriteListenerSettable<FixedLengthStreamSinkChannel>
listener
- the write listenerpublic ChannelListener<? super FixedLengthStreamSinkChannel> getWriteListener()
WriteListenerSettable
getWriteListener
in interface WriteListenerSettable<FixedLengthStreamSinkChannel>
public void setCloseListener(ChannelListener<? super FixedLengthStreamSinkChannel> listener)
CloseListenerSettable
setCloseListener
in interface CloseListenerSettable<FixedLengthStreamSinkChannel>
listener
- the close listenerpublic ChannelListener<? super FixedLengthStreamSinkChannel> getCloseListener()
CloseListenerSettable
getCloseListener
in interface CloseListenerSettable<FixedLengthStreamSinkChannel>
public ChannelListener.Setter<FixedLengthStreamSinkChannel> getWriteSetter()
StreamSinkChannel
getWriteSetter
in interface StreamSinkChannel
getWriteSetter
in interface SuspendableWriteChannel
public ChannelListener.Setter<FixedLengthStreamSinkChannel> getCloseSetter()
StreamSinkChannel
getCloseSetter
in interface CloseableChannel
getCloseSetter
in interface StreamSinkChannel
getCloseSetter
in interface SuspendableWriteChannel
public int writeFinal(ByteBuffer src) throws IOException
StreamSinkChannel
WritableByteChannel.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.writeFinal
in interface StreamSinkChannel
src
- The data to writeIOException
WritableByteChannel.write(java.nio.ByteBuffer)
,
SuspendableWriteChannel.shutdownWrites()
public long writeFinal(ByteBuffer[] srcs, int offset, int length) throws IOException
StreamSinkChannel
GatheringByteChannel.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.writeFinal
in interface StreamSinkChannel
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 - offsetIOException
GatheringByteChannel.write(java.nio.ByteBuffer[], int, int)
,
SuspendableWriteChannel.shutdownWrites()
public long writeFinal(ByteBuffer[] srcs) throws IOException
StreamSinkChannel
GatheringByteChannel.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.writeFinal
in interface StreamSinkChannel
srcs
- The buffers from which bytes are to be retrievedIOException
GatheringByteChannel.write(java.nio.ByteBuffer[])
,
SuspendableWriteChannel.shutdownWrites()
public StreamSinkChannel getChannel(Object guard)
ProtectedWrappedChannel
null
if the guard is invalid.getChannel
in interface ProtectedWrappedChannel<StreamSinkChannel>
guard
- the guard object@Deprecated public XnioExecutor getWriteThread()
SuspendableWriteChannel
getWriteThread
in interface SuspendableWriteChannel
null
if none is configured or availablepublic XnioIoThread getIoThread()
CloseableChannel
getIoThread
in interface CloseableChannel
public XnioWorker getWorker()
CloseableChannel
getWorker
in interface CloseableChannel
public int write(ByteBuffer src) throws IOException
write
in interface WritableByteChannel
IOException
public long write(ByteBuffer[] srcs) throws IOException
write
in interface GatheringByteChannel
IOException
public long write(ByteBuffer[] srcs, int offset, int length) throws IOException
write
in interface GatheringByteChannel
IOException
public long transferFrom(FileChannel src, long position, long count) throws IOException
StreamSinkChannel
FileChannel.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, the InterruptedIOException.bytesTransferred
field is
guaranteed to be 0.
transferFrom
in interface StreamSinkChannel
src
- the file to read fromposition
- the position within the file from which the transfer is to begincount
- the number of bytes to be transferredIOException
- if an I/O error occurspublic long transferFrom(StreamSourceChannel source, long count, ByteBuffer throughBuffer) throws IOException
StreamSinkChannel
throughBuffer
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 than
count
, then the remaining data in throughBuffer
may contain data read from source
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, the InterruptedIOException.bytesTransferred
field is
guaranteed to be 0.
transferFrom
in interface StreamSinkChannel
source
- the source to read fromcount
- the number of bytes to be transferredthroughBuffer
- the buffer to copy through.IOException
- if an I/O error occurspublic boolean flush() throws IOException
SuspendableWriteChannel
true
. If there is data to flush which cannot be immediately written, this method
will return false
. If this method returns true
after SuspendableWriteChannel.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.flush
in interface SuspendableWriteChannel
true
if the message was flushed, or false
if the result would blockIOException
- if an I/O error occurspublic void suspendWrites()
SuspendableWriteChannel
suspendWrites
in interface SuspendableWriteChannel
public void resumeWrites()
SuspendableWriteChannel
resumeWrites
in interface SuspendableWriteChannel
public boolean isWriteResumed()
SuspendableWriteChannel
isWriteResumed
in interface SuspendableWriteChannel
true
if writes are resumed, false
if writes are suspendedpublic void wakeupWrites()
SuspendableWriteChannel
Resume writes
on this channel, and force the write listener to be triggered even if the
channel isn't actually writable.wakeupWrites
in interface SuspendableWriteChannel
public void shutdownWrites() throws IOException
SuspendableWriteChannel
SuspendableWriteChannel.flush()
method; once this is done, if the read side of the channel was shut down, the channel will
automatically close.shutdownWrites
in interface SuspendableWriteChannel
IOException
- if an I/O error occurspublic void awaitWritable() throws IOException
SuspendableWriteChannel
awaitWritable
in interface SuspendableWriteChannel
InterruptedIOException
- if the operation is interrupted; the thread's interrupt flag will be set as wellIOException
- if an I/O error occurspublic void awaitWritable(long time, TimeUnit timeUnit) throws IOException
SuspendableWriteChannel
awaitWritable
in interface SuspendableWriteChannel
time
- the time to waittimeUnit
- the time unitInterruptedIOException
- if the operation is interrupted; the thread's interrupt flag will be set as wellIOException
- if an I/O error occurspublic boolean isOpen()
SuspendableWriteChannel
false
if all directions are shut down,
even if there is unflushed write data pending.isOpen
in interface Channel
isOpen
in interface SuspendableWriteChannel
true
if the channel is open, false
otherwisepublic void close() throws IOException
SuspendableWriteChannel
close
in interface Closeable
close
in interface AutoCloseable
close
in interface Channel
close
in interface InterruptibleChannel
close
in interface CloseableChannel
close
in interface SuspendableWriteChannel
IOException
- if the close failedpublic boolean supportsOption(Option<?> option)
Configurable
supportsOption
in interface Configurable
option
- the optiontrue
if it is supportedpublic <T> T getOption(Option<T> option) throws IOException
Configurable
getOption
in interface Configurable
T
- the type of the option valueoption
- the option to getnull
if it is not setIOException
- if an I/O error occurred when reading the optionpublic <T> T setOption(Option<T> option, T value) throws IllegalArgumentException, IOException
Configurable
setOption
in interface Configurable
T
- the type of the option valueoption
- the option to setvalue
- the value of the option to setIllegalArgumentException
- if the value is not acceptable for this optionIOException
- if an I/O error occurred when modifying the optionpublic long getRemaining()
Copyright © 2017 JBoss by Red Hat. All rights reserved.