WritableByteChannel
Stay organized with collections
Save and categorize content based on your preferences.
Known Indirect Subclasses
|
A channel that can write bytes.
Only one write operation upon a writable channel may be in progress at
any given time. If one thread initiates a write operation upon a channel
then any other thread that attempts to initiate another write operation will
block until the first operation is complete. Whether or not other kinds of
I/O operations may proceed concurrently with a write operation depends upon
the type of the channel.
Public Method Summary
abstract
int
|
write( ByteBuffer src)
Writes a sequence of bytes to this channel from the given buffer.
|
Inherited Method Summary
From interface
java.io.Closeable
abstract
void
|
close()
Closes this stream and releases any system resources associated
with it.
|
Public Methods
public
abstract
int
write
(ByteBuffer src)
Writes a sequence of bytes to this channel from the given buffer.
An attempt is made to write up to r bytes to the channel,
where r is the number of bytes remaining in the buffer, that is,
src.remaining(), at the moment this method is invoked.
Suppose that a byte sequence of length n is written, where
0 <= n <= r.
This byte sequence will be transferred from the buffer starting at index
p, where p is the buffer's position at the moment this
method is invoked; the index of the last byte written will be
p + n - 1.
Upon return the buffer's position will be equal to
p + n; its limit will not have changed.
Unless otherwise specified, a write operation will return only after
writing all of the r requested bytes. Some types of channels,
depending upon their state, may write only some of the bytes or possibly
none at all. A socket channel in non-blocking mode, for example, cannot
write any more bytes than are free in the socket's output buffer.
This method may be invoked at any time. If another thread has
already initiated a write operation upon this channel, however, then an
invocation of this method will block until the first operation is
complete.
Parameters
src |
The buffer from which bytes are to be retrieved |
Returns
- The number of bytes written, possibly zero
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003e\u003ccode\u003eWritableByteChannel\u003c/code\u003e is an interface that defines channels for writing bytes, extending the \u003ccode\u003eChannel\u003c/code\u003e interface.\u003c/p\u003e\n"],["\u003cp\u003eOnly one write operation can be in progress at a time on a \u003ccode\u003eWritableByteChannel\u003c/code\u003e, ensuring thread safety during writes.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003ewrite()\u003c/code\u003e method attempts to write bytes from a given \u003ccode\u003eByteBuffer\u003c/code\u003e to the channel, blocking if another write is in progress.\u003c/p\u003e\n"],["\u003cp\u003eWrite operations might not write all requested bytes depending on the channel's state, and specific exceptions can be thrown.\u003c/p\u003e\n"],["\u003cp\u003eSeveral inherited methods are available from \u003ccode\u003eChannel\u003c/code\u003e, \u003ccode\u003eCloseable\u003c/code\u003e, and \u003ccode\u003eAutoCloseable\u003c/code\u003e interfaces for managing the channel's state and resources.\u003c/p\u003e\n"]]],["`WritableByteChannel` is an interface for writing bytes. Key actions include writing a sequence of bytes from a buffer using the `write(ByteBuffer src)` method. This method writes up to *r* bytes, updating the buffer's position accordingly. Only one write operation can occur at a time, with subsequent attempts blocking until completion. It can throw exceptions like `NonWritableChannelException` and `ClosedChannelException`. Also, provides methods for closing the channel: `close()`, and determining its status: `isOpen()`.\n"],null,["# WritableByteChannel\n\npublic interface **WritableByteChannel** implements [Channel](../../../../reference/java/nio/channels/Channel.html) \n\n|---|---|---|\n| Known Indirect Subclasses [ByteChannel](../../../../reference/java/nio/channels/ByteChannel.html), [DatagramChannel](../../../../reference/java/nio/channels/DatagramChannel.html), [FileChannel](../../../../reference/java/nio/channels/FileChannel.html), [GatheringByteChannel](../../../../reference/java/nio/channels/GatheringByteChannel.html), [Pipe.SinkChannel](../../../../reference/java/nio/channels/Pipe.SinkChannel.html), [SeekableByteChannel](../../../../reference/java/nio/channels/SeekableByteChannel.html), [SocketChannel](../../../../reference/java/nio/channels/SocketChannel.html) |-------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------| | [ByteChannel](../../../../reference/java/nio/channels/ByteChannel.html) | A channel that can read and write bytes. | | [DatagramChannel](../../../../reference/java/nio/channels/DatagramChannel.html) | A selectable channel for datagram-oriented sockets. | | [FileChannel](../../../../reference/java/nio/channels/FileChannel.html) | A channel for reading, writing, mapping, and manipulating a file. | | [GatheringByteChannel](../../../../reference/java/nio/channels/GatheringByteChannel.html) | A channel that can write bytes from a sequence of buffers. | | [Pipe.SinkChannel](../../../../reference/java/nio/channels/Pipe.SinkChannel.html) | A channel representing the writable end of a [Pipe](../../../../reference/java/nio/channels/Pipe.html). | | [SeekableByteChannel](../../../../reference/java/nio/channels/SeekableByteChannel.html) | A byte channel that maintains a current *position* and allows the position to be changed. | | [SocketChannel](../../../../reference/java/nio/channels/SocketChannel.html) | A selectable channel for stream-oriented connecting sockets. | |||\n\nA channel that can write bytes.\n\nOnly one write operation upon a writable channel may be in progress at\nany given time. If one thread initiates a write operation upon a channel\nthen any other thread that attempts to initiate another write operation will\nblock until the first operation is complete. Whether or not other kinds of\nI/O operations may proceed concurrently with a write operation depends upon\nthe type of the channel.\n\n\u003cbr /\u003e\n\n### Public Method Summary\n\n|--------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract int | [write](../../../../reference/java/nio/channels/WritableByteChannel.html#write(java.nio.ByteBuffer))([ByteBuffer](../../../../reference/java/nio/ByteBuffer.html) src) Writes a sequence of bytes to this channel from the given buffer. |\n\n### Inherited Method Summary\n\nFrom interface [java.nio.channels.Channel](../../../../reference/java/nio/channels/Channel.html) \n\n|------------------|----------------------------------------------------------------------------------------------------------------------|\n| abstract void | [close](../../../../reference/java/nio/channels/Channel.html#close())() Closes this channel. |\n| abstract boolean | [isOpen](../../../../reference/java/nio/channels/Channel.html#isOpen())() Tells whether or not this channel is open. |\n\nFrom interface [java.io.Closeable](../../../../reference/java/io/Closeable.html) \n\n|---------------|------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract void | [close](../../../../reference/java/io/Closeable.html#close())() Closes this stream and releases any system resources associated with it. |\n\nFrom interface [java.lang.AutoCloseable](../../../../reference/java/lang/AutoCloseable.html) \n\n|---------------|-------------------------------------------------------------------------------------------------------------------------------------|\n| abstract void | [close](../../../../reference/java/lang/AutoCloseable.html#close())() Closes this resource, relinquishing any underlying resources. |\n\nPublic Methods\n--------------\n\n#### public abstract int\n**write**\n([ByteBuffer](../../../../reference/java/nio/ByteBuffer.html) src)\n\nWrites a sequence of bytes to this channel from the given buffer.\n\nAn attempt is made to write up to *r* bytes to the channel,\nwhere *r* is the number of bytes remaining in the buffer, that is,\nsrc.remaining(), at the moment this method is invoked.\n\nSuppose that a byte sequence of length *n* is written, where\n0 \\\u003c= *n* \\\u003c= *r* .\nThis byte sequence will be transferred from the buffer starting at index\n*p* , where *p* is the buffer's position at the moment this\nmethod is invoked; the index of the last byte written will be\n*p* + *n* - 1.\nUpon return the buffer's position will be equal to\n*p* + *n*; its limit will not have changed.\n\nUnless otherwise specified, a write operation will return only after\nwriting all of the *r* requested bytes. Some types of channels,\ndepending upon their state, may write only some of the bytes or possibly\nnone at all. A socket channel in non-blocking mode, for example, cannot\nwrite any more bytes than are free in the socket's output buffer.\n\nThis method may be invoked at any time. If another thread has\nalready initiated a write operation upon this channel, however, then an\ninvocation of this method will block until the first operation is\ncomplete.\n\n\u003cbr /\u003e\n\n##### Parameters\n\n| src | The buffer from which bytes are to be retrieved |\n|-----|-------------------------------------------------|\n\n##### Returns\n\n- The number of bytes written, possibly zero \n\n##### Throws\n\n| [NonWritableChannelException](../../../../reference/java/nio/channels/NonWritableChannelException.html) | If this channel was not opened for writing |\n| [ClosedChannelException](../../../../reference/java/nio/channels/ClosedChannelException.html) | If this channel is closed |\n| [AsynchronousCloseException](../../../../reference/java/nio/channels/AsynchronousCloseException.html) | If another thread closes this channel while the write operation is in progress |\n| [ClosedByInterruptException](../../../../reference/java/nio/channels/ClosedByInterruptException.html) | If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status |\n| [IOException](../../../../reference/java/io/IOException.html) | If some other I/O error occurs |\n|---------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|"]]