Java ByteBuffer Append append(ByteBuffer buffer, String s)

Here you can find the source of append(ByteBuffer buffer, String s)

Description

append

License

Open Source License

Declaration

static void append(ByteBuffer buffer, String s) 

Method Source Code


//package com.java2s;
import java.nio.ByteBuffer;

public class Main {
    static void append(ByteBuffer buffer, String s) {
        int len = s.length();
        for (int i = 0; i < len; i++)
            buffer.put((byte) s.charAt(i));
        buffer.put((byte) 0);
        align(buffer);//  w w w .  j av a2 s  .com
    }

    static void append(ByteBuffer buffer, byte[] data) {
        buffer.putInt(data.length);
        buffer.put(data);
        align(buffer);
    }

    static void append(ByteBuffer buffer, Object o) {
        if (o instanceof Integer) {
            buffer.putInt((Integer) o);
        } else if (o instanceof Float) {
            buffer.putFloat((Float) o);
        } else if (o instanceof Double) {
            buffer.putDouble((Double) o);
        } else if (o instanceof String) {
            append(buffer, (String) o);
        } else if (o instanceof byte[]) {
            append(buffer, (byte[]) o);
        } else if (o instanceof Boolean) {
            // Nothing to do. The value (true or false) is already sent in the
            // type string
        } else if (o == null) {
            // Nothing to do. The type string alreday specifies a null
        } else
            throw new IllegalArgumentException("Unsupported OSC Type:" + o.getClass().getName());
    }

    static void align(ByteBuffer buffer) {
        buffer.position(((buffer.position() + 3) / 4) * 4);
    }
}

Related

  1. append(ByteBuffer source, int index, byte[] dest)
  2. append(ByteBuffer to, byte[] b, int off, int len)
  3. appendBuffers(ByteBuffer buffer, ByteBuffer buffer1, int incomingBufferSize, int BUFFER_STEP_SIZE)
  4. appendHex(StringBuilder sb, ByteBuffer bb)