ByteBuffer.Compact Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Compacts this buffer (optional operation).
[Android.Runtime.Register("compact", "()Ljava/nio/ByteBuffer;", "GetCompactHandler")]
public abstract Java.Nio.ByteBuffer Compact ();
[<Android.Runtime.Register("compact", "()Ljava/nio/ByteBuffer;", "GetCompactHandler")>]
abstract member Compact : unit -> Java.Nio.ByteBuffer
Returns
This buffer
- Attributes
Exceptions
if no changes may be made to the contents of this buffer.
Remarks
Compacts this buffer (optional operation).
The bytes between the buffer's current position and its limit, if any, are copied to the beginning of the buffer. That is, the byte at index p = position()
is copied to index zero, the byte at index p + 1 is copied to index one, and so forth until the byte at index limit()
- 1 is copied to index n = limit()
- 1
- p. The buffer's position is then set to n+1 and its limit is set to its capacity. The mark, if defined, is discarded.
The buffer's position is set to the number of bytes copied, rather than to zero, so that an invocation of this method can be followed immediately by an invocation of another relative put method.
Invoke this method after writing data from a buffer in case the write was incomplete. The following loop, for example, copies bytes from one channel to another via the buffer buf
:
<blockquote>
{@code
buf.clear(); // Prepare buffer for use
while (in.read(buf) >= 0 || buf.position != 0) {
buf.flip();
out.write(buf);
buf.compact(); // In case of partial write
}
}
</blockquote>
Java documentation for java.nio.ByteBuffer.compact()
.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.