Java ByteBuffer Fill fillHlaBuffer(ByteBuffer buf)

Here you can find the source of fillHlaBuffer(ByteBuffer buf)

Description

fill Hla Buffer

License

Apache License

Declaration

public static ByteBuffer fillHlaBuffer(ByteBuffer buf) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.nio.ByteBuffer;

public class Main {
    public static final int MAX_BUFFER_SIZE = 150;
    public static final boolean hlaOk = true;
    public static final int hlaValue = 66;
    public static final String hlaName = "Command";

    public static ByteBuffer fillHlaBuffer(ByteBuffer buf) {
        // Actually, format of SUPPORTED_CLASS topic is:
        // a boolean (ok)
        // an int (value)
        // a String (name)
        String name = hlaName;/*w  ww  . j a  v a  2s  .co  m*/
        // Check size of message. Must be less than the MAX_BUFFER_SIZE
        int messageLength = 1 + 4 + 4 + 2 * name.length();
        if (messageLength > MAX_BUFFER_SIZE) {
            int extraMessageLength = messageLength - MAX_BUFFER_SIZE;
            // Remove extra length / 2 char from the name variable
            name = name.substring(0, name.length() - (extraMessageLength / 2));
        }

        // Rewind the buffer
        buf.rewind();

        // Put every value into the buffer
        // A boolean
        buf.put((byte) (hlaOk ? 1 : 0));
        // An integer
        buf.putInt(hlaValue);
        // A String. 1/ size of the string, 2/ array of char
        buf.putInt(name.length());
        for (int i = 0; i < name.length(); i++) {
            buf.putChar(name.charAt(i));
        }

        buf.rewind(); //Don't forget to rewind the buffer. The interface processor won't do it...

        return buf;
    }
}

Related

  1. fill(ByteBuffer to, byte[] b, int off, int len)
  2. fillBuffer(ByteBuffer buffer, byte[] bytes)
  3. fillBuffer(ByteBuffer buffer, int seed)
  4. fillBufFromTime(ByteBuffer buf, Calendar cal)
  5. fillDdsBuffer(ByteBuffer buf)
  6. fillRange(ByteBuffer buffer, int start, int end)