Example usage for java.nio ByteBuffer putInt

List of usage examples for java.nio ByteBuffer putInt

Introduction

In this page you can find the example usage for java.nio ByteBuffer putInt.

Prototype

public abstract ByteBuffer putInt(int value);

Source Link

Document

Writes the given int to the current position and increases the position by 4.

Usage

From source file:com.nridge.core.base.std.BufUtl.java

/**
 * Stores the <i>String</i> parameter value into the
 * <code>ByteBuffer</code> object.
 *
 * @param aBuffer Packet byte buffer object.
 * @param aString A non-null string.//from   ww w  .  j  av  a2  s. c o m
 */
public static void putString(ByteBuffer aBuffer, String aString) {
    int strLength;

    if ((aBuffer != null) && (aString != null)) {
        strLength = aString.length();
        aBuffer.putInt(strLength);
        for (int i = 0; i < strLength; i++)
            aBuffer.putChar(aString.charAt(i));
    }
}

From source file:org.apache.myriad.state.utils.ByteBufferSupport.java

public static void putBytes(ByteBuffer bb, byte bytes[]) {
    if (bytes != null && bytes.length > 0) {
        bb.putInt(bytes.length);
        bb.put(bytes);// ww  w . jav  a2s  . co  m
    } else {
        bb.putInt(0);
    }
}

From source file:com.inmobi.messaging.util.AuditUtil.java

public static void attachHeaders(Message m, Long timestamp) {
    byte[] b = m.getData().array();
    int messageSize = b.length;
    int totalSize = messageSize + HEADER_LENGTH;
    ByteBuffer buffer = ByteBuffer.allocate(totalSize);

    // writing version
    buffer.put((byte) currentVersion);
    // writing magic bytes
    buffer.put(magicBytes);/*w w  w .  j a v a 2s  .  c o  m*/
    // writing timestamp
    long time = timestamp;
    buffer.putLong(time);

    // writing message size
    buffer.putInt(messageSize);
    // writing message
    buffer.put(b);
    buffer.rewind();
    m.set(buffer);
    // return new Message(buffer);

}

From source file:org.apache.myriad.state.utils.ByteBufferSupport.java

public static ByteBuffer toByteBuffer(NodeTask nt) {
    // Determine the size of ByteBuffer to allocate
    // The ServiceResourceProfile toString() returns Json, if this ever changes then this
    // will fail. Json is expected.
    byte[] profile = toBytes(nt.getProfile().toString());
    int size = profile.length + INT_SIZE;

    Constraint constraint = nt.getConstraint();
    Constraint.Type type = constraint == null ? Type.NULL : constraint.getType();
    size += INT_SIZE;/*  w ww  . j a v a  2 s  .co  m*/

    byte[] constraintBytes = ZERO_BYTES;
    if (constraint != null) {
        constraintBytes = toBytes(constraint.toString());
        size += constraintBytes.length + INT_SIZE;
    } else {
        size += INT_SIZE;
    }

    byte[] hostname = toBytes(nt.getHostname());
    size += hostname.length + INT_SIZE;

    if (nt.getSlaveId() != null) {
        size += nt.getSlaveId().getSerializedSize() + INT_SIZE;
    } else {
        size += INT_SIZE;
    }

    if (nt.getTaskStatus() != null) {
        size += nt.getTaskStatus().getSerializedSize() + INT_SIZE;
    } else {
        size += INT_SIZE;
    }

    if (nt.getExecutorInfo() != null) {
        size += nt.getExecutorInfo().getSerializedSize() + INT_SIZE;
    } else {
        size += INT_SIZE;
    }

    byte[] taskPrefixBytes = ZERO_BYTES;
    if (nt.getTaskPrefix() != null) {
        taskPrefixBytes = toBytes(nt.getTaskPrefix());
        size += taskPrefixBytes.length + INT_SIZE;
    }

    // Allocate and populate the buffer.
    ByteBuffer bb = createBuffer(size);
    putBytes(bb, profile);
    bb.putInt(type.ordinal());
    putBytes(bb, constraintBytes);
    putBytes(bb, hostname);
    putBytes(bb, getSlaveBytes(nt));
    putBytes(bb, getTaskBytes(nt));
    putBytes(bb, getExecutorInfoBytes(nt));
    putBytes(bb, taskPrefixBytes);
    // Make sure the buffer is at the beginning
    bb.rewind();
    return bb;
}

From source file:org.apache.cassandra.Util.java

public static ByteBuffer getBytes(int v) {
    byte[] bytes = new byte[4];
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    bb.putInt(v);
    bb.rewind();/*from  w  w  w  .  ja  v a  2 s .  c o m*/
    return bb;
}

From source file:com.github.woz_dialog.ros_woz_dialog_project.TTSHTTPClient.java

private static byte[] addWavHeader(byte[] bytes) throws IOException {

    ByteBuffer bufferWithHeader = ByteBuffer.allocate(bytes.length + 44);
    bufferWithHeader.order(ByteOrder.LITTLE_ENDIAN);
    bufferWithHeader.put("RIFF".getBytes());
    bufferWithHeader.putInt(bytes.length + 36);
    bufferWithHeader.put("WAVE".getBytes());
    bufferWithHeader.put("fmt ".getBytes());
    bufferWithHeader.putInt(16);/*from   w w w.  jav a  2  s  .  c  o m*/
    bufferWithHeader.putShort((short) 1);
    bufferWithHeader.putShort((short) 1);
    bufferWithHeader.putInt(16000);
    bufferWithHeader.putInt(32000);
    bufferWithHeader.putShort((short) 2);
    bufferWithHeader.putShort((short) 16);
    bufferWithHeader.put("data".getBytes());
    bufferWithHeader.putInt(bytes.length);
    bufferWithHeader.put(bytes);
    return bufferWithHeader.array();
}

From source file:org.hobbit.core.rabbit.RabbitMQUtils.java

/**
 * writes the given byte arrays and puts their length in front of them.
 * Thus, they can be read using a ByteBuffer and the
 * {@link #readByteArray(ByteBuffer)} method.
 *
 * @param precedingData/*w w w .  j av  a2  s .  co  m*/
 *            data that should be written before the arrays are written.
 *            <b>Note</b> that this data is not preceded with the byte array
 *            length.
 * @param arrays
 *            arrays that should be written to a single array
 * @param subsequentData
 *            data that should be written after the arrays are written.
 *            <b>Note</b> that this data is not preceded with the byte array
 *            length.
 * @return the byte array containing all given arrays and their lengths
 */
public static byte[] writeByteArrays(byte[] precedingData, byte[][] arrays, byte[] subsequentData) {
    int length = arrays.length * 4;
    if (precedingData != null) {
        length += precedingData.length;
    }
    for (int i = 0; i < arrays.length; ++i) {
        length += arrays[i].length;
    }
    if (subsequentData != null) {
        length += subsequentData.length;
    }
    // write the data to the byte array
    ByteBuffer buffer = ByteBuffer.allocate(length);
    if (precedingData != null) {
        buffer.put(precedingData);
    }
    for (int i = 0; i < arrays.length; ++i) {
        buffer.putInt(arrays[i].length);
        buffer.put(arrays[i]);
    }
    if (subsequentData != null) {
        buffer.put(subsequentData);
    }
    return buffer.array();
}

From source file:org.energy_home.jemma.javagal.layers.data.implementations.Utils.DataManipulation.java

/**
 * Converts an {@code int} to a {@code byte[]}. An int is composed of four
 * bytes. Writes four bytes containing the given int value, in the current
 * byte order. Numbering them from 0 (the most important) to 5 (the least
 * important), the resulting array will have all them placed in the same
 * position.//  w  w  w . ja  va2s  . c  o m
 * 
 * @param x
 *            the int to convert.
 * @return the resulting array.
 */
public static byte[] intToBytes(int x) {
    ByteBuffer buffer = ByteBuffer.allocate(4);
    buffer.order(ByteOrder.BIG_ENDIAN);
    buffer.putInt(x);
    return buffer.array();
}

From source file:com.alibaba.rocketmq.common.MixAll.java

public static long createBrokerId(final String ip, final int port) {
    InetSocketAddress isa = new InetSocketAddress(ip, port);
    byte[] ipArray = isa.getAddress().getAddress();
    ByteBuffer bb = ByteBuffer.allocate(8);
    bb.put(ipArray);/*from w  ww .ja  v  a  2  s  .  c  o m*/
    bb.putInt(port);
    long value = bb.getLong(0);
    return Math.abs(value);
}

From source file:io.github.dsheirer.record.wave.WaveWriter.java

/**
 * Creates a wave file header with a format descriptor chunk
 *///w ww .ja va  2  s .c o  m
public static ByteBuffer getWaveHeader(AudioFormat format) {
    ByteBuffer header = ByteBuffer.allocate(12).order(ByteOrder.LITTLE_ENDIAN);

    //RIFF/WAVE header and size
    header.put(RIFF_ID.getBytes());
    header.putInt(INITIAL_TOTAL_LENGTH);
    header.put(WAVE_ID.getBytes());

    //Reset the buffer pointer to 0
    header.position(0);

    return header;
}