- java.lang.Object
-
- org.xnio.conduits.AbstractConduit<D>
-
- org.xnio.conduits.AbstractSinkConduit<D>
-
- org.xnio.conduits.AbstractStreamSinkConduit<StreamSinkConduit>
-
- org.xnio.conduits.BufferedStreamSinkConduit
-
- All Implemented Interfaces:
Conduit
,SinkConduit
,StreamSinkConduit
public final class BufferedStreamSinkConduit extends AbstractStreamSinkConduit<StreamSinkConduit>
A stream sink conduit that buffers output data.- Author:
- David M. Lloyd
-
-
Field Summary
-
Fields inherited from class org.xnio.conduits.AbstractConduit
next
-
-
Constructor Summary
Constructors Constructor Description BufferedStreamSinkConduit(StreamSinkConduit next, Pooled<java.nio.ByteBuffer> pooledBuffer)
Construct a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
flush()
Flush out any unwritten, buffered output.void
terminateWrites()
Signal that no more write data is forthcoming.long
transferFrom(java.nio.channels.FileChannel src, long position, long count)
Transfer bytes into this conduit from the given file.long
transferFrom(StreamSourceChannel source, long count, java.nio.ByteBuffer throughBuffer)
Transfers bytes from the given channel source.void
truncateWrites()
Terminate writes and discard any outstanding write data.int
write(java.nio.ByteBuffer src)
Writes a sequence of bytes to this conduit from the given buffer.long
write(java.nio.ByteBuffer[] srcs, int offs, int len)
Writes a sequence of bytes to this conduit from the given buffers.int
writeFinal(java.nio.ByteBuffer src)
Writes some data to the conduit, with the same semantics asStreamSinkConduit.write(java.nio.ByteBuffer)
.long
writeFinal(java.nio.ByteBuffer[] srcs, int offset, int length)
Writes some data to the conduit, with the same semantics asStreamSinkConduit.write(java.nio.ByteBuffer[], int, int)
.-
Methods inherited from class org.xnio.conduits.AbstractSinkConduit
awaitWritable, awaitWritable, getWriteThread, isWriteResumed, isWriteShutdown, resumeWrites, setWriteReadyHandler, suspendWrites, wakeupWrites
-
Methods inherited from class org.xnio.conduits.AbstractConduit
getWorker
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.xnio.conduits.SinkConduit
awaitWritable, awaitWritable, getWriteThread, isWriteResumed, isWriteShutdown, resumeWrites, setWriteReadyHandler, suspendWrites, wakeupWrites
-
-
-
-
Constructor Detail
-
BufferedStreamSinkConduit
public BufferedStreamSinkConduit(StreamSinkConduit next, Pooled<java.nio.ByteBuffer> pooledBuffer)
Construct a new instance.- Parameters:
next
- the delegate conduit to setpooledBuffer
- the pooled buffer to use
-
-
Method Detail
-
transferFrom
public long transferFrom(java.nio.channels.FileChannel src, long position, long count) throws java.io.IOException
Description copied from interface:StreamSinkConduit
Transfer bytes into this conduit from the given file.- Specified by:
transferFrom
in interfaceStreamSinkConduit
- Overrides:
transferFrom
in classAbstractStreamSinkConduit<StreamSinkConduit>
- 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:
java.io.IOException
- if an I/O error occurs
-
transferFrom
public long transferFrom(StreamSourceChannel source, long count, java.nio.ByteBuffer throughBuffer) throws java.io.IOException
Description copied from interface:StreamSinkConduit
Transfers bytes from the given channel source. 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.- Specified by:
transferFrom
in interfaceStreamSinkConduit
- Overrides:
transferFrom
in classAbstractStreamSinkConduit<StreamSinkConduit>
- 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:
java.io.IOException
- if an I/O error occurs
-
write
public int write(java.nio.ByteBuffer src) throws java.io.IOException
Description copied from interface:StreamSinkConduit
Writes a sequence of bytes to this conduit from the given buffer.- Specified by:
write
in interfaceStreamSinkConduit
- Overrides:
write
in classAbstractStreamSinkConduit<StreamSinkConduit>
- Parameters:
src
- the buffer containing data to write- Returns:
- the number of bytes written, possibly 0
- Throws:
java.nio.channels.ClosedChannelException
- if this conduit'sSinkConduit.terminateWrites()
method was previously calledjava.io.IOException
- if an error occurs
-
write
public long write(java.nio.ByteBuffer[] srcs, int offs, int len) throws java.io.IOException
Description copied from interface:StreamSinkConduit
Writes a sequence of bytes to this conduit from the given buffers.- Specified by:
write
in interfaceStreamSinkConduit
- Overrides:
write
in classAbstractStreamSinkConduit<StreamSinkConduit>
- Parameters:
srcs
- the buffers containing data to writeoffs
- the offset into the buffer arraylen
- the number of buffers to write- Returns:
- the number of bytes written, possibly 0
- Throws:
java.nio.channels.ClosedChannelException
- if this conduit'sSinkConduit.terminateWrites()
method was previously calledjava.io.IOException
- if an error occurs
-
writeFinal
public int writeFinal(java.nio.ByteBuffer src) throws java.io.IOException
Description copied from interface:StreamSinkConduit
Writes some data to the conduit, with the same semantics asStreamSinkConduit.write(java.nio.ByteBuffer)
. If all the data is written out then the conduit will have its writes terminated. Semantically this method is equivalent to:int rem = src.remaining(); int written = conduit.write(src); if(written == rem) { conduit.terminateWrites() }
- Specified by:
writeFinal
in interfaceStreamSinkConduit
- Overrides:
writeFinal
in classAbstractStreamSinkConduit<StreamSinkConduit>
- Parameters:
src
- The data to write- Returns:
- The amount of data that was actually written.
- Throws:
java.io.IOException
-
writeFinal
public long writeFinal(java.nio.ByteBuffer[] srcs, int offset, int length) throws java.io.IOException
Description copied from interface:StreamSinkConduit
Writes some data to the conduit, with the same semantics asStreamSinkConduit.write(java.nio.ByteBuffer[], int, int)
. If all the data is written out then the conduit will have its writes terminated.- Specified by:
writeFinal
in interfaceStreamSinkConduit
- Overrides:
writeFinal
in classAbstractStreamSinkConduit<StreamSinkConduit>
- 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:
java.io.IOException
-
flush
public boolean flush() throws java.io.IOException
Description copied from interface:SinkConduit
Flush out any unwritten, buffered output.- Specified by:
flush
in interfaceSinkConduit
- Overrides:
flush
in classAbstractSinkConduit<StreamSinkConduit>
- Returns:
true
if everything is flushed,false
otherwise- Throws:
java.io.IOException
- if flush fails
-
truncateWrites
public void truncateWrites() throws java.io.IOException
Description copied from interface:SinkConduit
Terminate writes and discard any outstanding write data. The conduit is terminated and flushed regardless of the outcome of this method.- Specified by:
truncateWrites
in interfaceSinkConduit
- Overrides:
truncateWrites
in classAbstractSinkConduit<StreamSinkConduit>
- Throws:
java.io.IOException
- if channel termination failed for some reason
-
terminateWrites
public void terminateWrites() throws java.io.IOException
Description copied from interface:SinkConduit
Signal that no more write data is forthcoming. The conduit must beSinkConduit.flush()
ed before it is considered to be shut down.- Specified by:
terminateWrites
in interfaceSinkConduit
- Overrides:
terminateWrites
in classAbstractSinkConduit<StreamSinkConduit>
- Throws:
java.io.IOException
-
-