- java.lang.Object
-
- org.xnio.channels.BlockingByteChannel
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
,java.nio.channels.ByteChannel
,java.nio.channels.Channel
,java.nio.channels.GatheringByteChannel
,java.nio.channels.ReadableByteChannel
,java.nio.channels.ScatteringByteChannel
,java.nio.channels.WritableByteChannel
public class BlockingByteChannel extends java.lang.Object implements java.nio.channels.ScatteringByteChannel, java.nio.channels.GatheringByteChannel, java.nio.channels.ByteChannel, java.io.Flushable
A blocking wrapper for aStreamChannel
. Read and write operations will block until some data may be transferred. Once any amount of data is read or written, the operation will return. If a read timeout is specified, then the read methods will throw aReadTimeoutException
if the timeout expires without reading any data. If a write timeout is specified, then the write methods will throw aWriteTimeoutException
if the timeout expires without writing any data.
-
-
Constructor Summary
Constructors Constructor Description BlockingByteChannel(StreamChannel delegate)
Construct a new instance.BlockingByteChannel(StreamChannel delegate, long timeout, java.util.concurrent.TimeUnit timeoutUnit)
Construct a new instance.BlockingByteChannel(StreamChannel delegate, long readTimeout, java.util.concurrent.TimeUnit readTimeoutUnit, long writeTimeout, java.util.concurrent.TimeUnit writeTimeoutUnit)
Construct a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
void
flush()
boolean
isOpen()
int
read(java.nio.ByteBuffer dst)
Perform a blocking read operation.long
read(java.nio.ByteBuffer[] dsts)
Perform a blocking, scattering read operation.long
read(java.nio.ByteBuffer[] dsts, int offset, int length)
Perform a blocking, scattering read operation.void
setReadTimeout(long readTimeout, java.util.concurrent.TimeUnit readTimeoutUnit)
Set the read timeout.void
setWriteTimeout(long writeTimeout, java.util.concurrent.TimeUnit writeTimeoutUnit)
Set the write timeout.int
write(java.nio.ByteBuffer src)
Perform a blocking write operation.long
write(java.nio.ByteBuffer[] srcs)
Perform a blocking, gathering write operation.long
write(java.nio.ByteBuffer[] srcs, int offset, int length)
Perform a blocking, gathering write operation.
-
-
-
Constructor Detail
-
BlockingByteChannel
public BlockingByteChannel(StreamChannel delegate)
Construct a new instance.- Parameters:
delegate
- the channel to forward I/O operations to
-
BlockingByteChannel
public BlockingByteChannel(StreamChannel delegate, long timeout, java.util.concurrent.TimeUnit timeoutUnit)
Construct a new instance.- Parameters:
delegate
- the channel to forward I/O operations totimeout
- the read/write timeouttimeoutUnit
- the read/write timeout unit
-
BlockingByteChannel
public BlockingByteChannel(StreamChannel delegate, long readTimeout, java.util.concurrent.TimeUnit readTimeoutUnit, long writeTimeout, java.util.concurrent.TimeUnit writeTimeoutUnit)
Construct a new instance.- Parameters:
delegate
- the channel to forward I/O operations toreadTimeout
- the read timeoutreadTimeoutUnit
- the read timeout unitwriteTimeout
- the write timeoutwriteTimeoutUnit
- the write timeout unit
-
-
Method Detail
-
setReadTimeout
public void setReadTimeout(long readTimeout, java.util.concurrent.TimeUnit readTimeoutUnit)
Set the read timeout.- Parameters:
readTimeout
- the read timeoutreadTimeoutUnit
- the read timeout unit
-
setWriteTimeout
public void setWriteTimeout(long writeTimeout, java.util.concurrent.TimeUnit writeTimeoutUnit)
Set the write timeout.- Parameters:
writeTimeout
- the write timeoutwriteTimeoutUnit
- the write timeout unit
-
read
public long read(java.nio.ByteBuffer[] dsts, int offset, int length) throws java.io.IOException
Perform a blocking, scattering read operation.- Specified by:
read
in interfacejava.nio.channels.ScatteringByteChannel
- Parameters:
dsts
- the destination buffersoffset
- the offset into the destination buffer arraylength
- the number of buffers to read into- Returns:
- the number of bytes actually read (will be greater than zero)
- Throws:
java.io.IOException
- if an I/O error occurs
-
read
public long read(java.nio.ByteBuffer[] dsts) throws java.io.IOException
Perform a blocking, scattering read operation.- Specified by:
read
in interfacejava.nio.channels.ScatteringByteChannel
- Parameters:
dsts
- the destination buffers- Returns:
- the number of bytes actually read (will be greater than zero)
- Throws:
java.io.IOException
- if an I/O error occurs
-
read
public int read(java.nio.ByteBuffer dst) throws java.io.IOException
Perform a blocking read operation.- Specified by:
read
in interfacejava.nio.channels.ReadableByteChannel
- Parameters:
dst
- the destination buffer- Returns:
- the number of bytes actually read (will be greater than zero)
- Throws:
java.io.IOException
- if an I/O error occurs
-
write
public long write(java.nio.ByteBuffer[] srcs, int offset, int length) throws java.io.IOException
Perform a blocking, gathering write operation.- Specified by:
write
in interfacejava.nio.channels.GatheringByteChannel
- Parameters:
srcs
- the source buffersoffset
- the offset into the destination buffer arraylength
- the number of buffers to write from- Returns:
- the number of bytes actually written (will be greater than zero)
- Throws:
java.io.IOException
- if an I/O error occurs
-
write
public long write(java.nio.ByteBuffer[] srcs) throws java.io.IOException
Perform a blocking, gathering write operation.- Specified by:
write
in interfacejava.nio.channels.GatheringByteChannel
- Parameters:
srcs
- the source buffers- Returns:
- the number of bytes actually written (will be greater than zero)
- Throws:
java.io.IOException
- if an I/O error occurs
-
write
public int write(java.nio.ByteBuffer src) throws java.io.IOException
Perform a blocking write operation.- Specified by:
write
in interfacejava.nio.channels.WritableByteChannel
- Parameters:
src
- the source buffer- Returns:
- the number of bytes actually written (will be greater than zero)
- Throws:
java.io.IOException
- if an I/O error occurs
-
isOpen
public boolean isOpen()
- Specified by:
isOpen
in interfacejava.nio.channels.Channel
-
flush
public void flush() throws java.io.IOException
- Specified by:
flush
in interfacejava.io.Flushable
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.nio.channels.Channel
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
-