Name

WritableByteChannel

Synopsis

This subinterface of Channel defines a single key write( ) method which writes bytes from a specified ByteBuffer (updating the buffer position as it goes) to the channel. If possible, it writes all remaining bytes in the buffer (see Buffer.remaining( )). This is not always possible (with nonblocking channels, for example) so the write( ) method returns the number of bytes that it was actually able to write to the channel.

write( ) is declared to throw an IOException. More specifically, it may throw a ClosedChannelException if the channel is closed. If the channel is closed asynchronously, or if a blocked thread is interrupted, the write( ) method may terminate with an AsynchronousCloseException or a ClosedByInterruptException. write( ) may also throw an unchecked NonWritableChannelException if it is called on a channel (such as a FileChannel) that was not opened or configured to allow writing.

WritableByteChannel implementations are required to be thread-safe: only one thread may perform a write operation on a channel at a time. If a write operation is in progress, then any call to write( ) will block until the in-progress operation completes. Some channel implementations may allow read and write operations to proceed concurrently; some may not.

java.nio.channels.WritableByteChannel

Figure 13-42. java.nio.channels.WritableByteChannel

public interface WritableByteChannel extends Channel { ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.