Java Utililty Methods ByteBuffer Write

List of utility methods to do ByteBuffer Write

Description

The list of methods to do ByteBuffer Write are organized into topic(s).

Method

voidwriteStringArray(ByteBuffer buf, String[] array)
write String Array
buf.putInt(array.length);
for (int i = 0; i < array.length; i++) {
    writeString(buf, array[i]);
voidwriteStringData(ByteBuffer buff, String s, int len)
Write characters from a string (without the length).
for (int i = 0; i < len; i++) {
    int c = s.charAt(i);
    if (c < 0x80) {
        buff.put((byte) c);
    } else if (c >= 0x800) {
        buff.put((byte) (0xe0 | (c >> 12)));
        buff.put((byte) (((c >> 6) & 0x3f)));
        buff.put((byte) (c & 0x3f));
...
voidwriteStringToBuffer(ByteBuffer buffer, String message)
write bytes to buffer and flip
buffer.clear();
buffer.put(message.getBytes());
buffer.flip();
voidwriteTo(ByteBuffer buffer, File file)
write To
FileChannel out = null;
try {
    out = new FileOutputStream(file).getChannel();
    out.write(buffer);
} finally {
    closeQuietly(out);
voidwriteToChannel(final WritableByteChannel destChannel, final ByteBuffer buffer)
write To Channel
try {
    destChannel.write(buffer);
    buffer.compact();
    buffer.flip();
    while (buffer.hasRemaining()) {
        destChannel.write(buffer);
} catch (IOException ex) {
...
voidwriteToChannel(WritableByteChannel chan, ByteBuffer buffer)
Writes the content of the buffer to the given channel.
buffer.rewind();
while (buffer.hasRemaining()) {
    chan.write(buffer);
intwriteToChannel(WritableByteChannel dst, ByteBuffer src)
Reads available data from the stream until the stream is empty or the java.nio.ByteBuffer is full
int totalBytesWritten = 0;
int bytesWritten;
do {
    bytesWritten = dst.write(src);
    totalBytesWritten += bytesWritten;
} while (bytesWritten > 0);
return totalBytesWritten;
voidwriteToFile(String name, ByteBuffer bb)
write To File
try {
    File file = new File(name);
    boolean append = false;
    FileOutputStream fos = new FileOutputStream(file, append);
    FileChannel wChannel = fos.getChannel();
    bb.rewind();
    wChannel.write(bb);
    wChannel.force(true);
...
voidwriteTs(ByteBuffer is, long ts)
write Ts
is.put((byte) ((ts >> 29) << 1));
is.put((byte) (ts >> 22));
is.put((byte) ((ts >> 15) << 1));
is.put((byte) (ts >> 7));
is.put((byte) (ts >> 1));
voidwritetUnsignedInt(ByteBuffer buffer, long value)
Write the given long value as a 4 byte unsigned integer.
buffer.putInt((int) (value & 0xffffffffL));