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:org.texai.util.ByteUtils.java

/** Returns a 4-byte array built from an int.
 *
 * @param number the int number to convert
 * @return a byte array//w  ww.  j a v  a 2  s .c  om
 */
public static byte[] toBytes(final int number) {
    final ByteBuffer byteBuffer = ByteBuffer.allocate(4);
    byteBuffer.putInt(number);
    return byteBuffer.array();
}

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

/**
 * Creates an audio format chunk/*from   w  w  w  .  j  a  va 2  s. co m*/
 */
public static ByteBuffer getFormatChunk(AudioFormat format) {
    ByteBuffer header = ByteBuffer.allocate(24).order(ByteOrder.LITTLE_ENDIAN);

    //Format descriptor
    header.put(FORMAT_CHUNK_ID.getBytes());
    header.putInt(FORMAT_CHUNK_LENGTH);
    header.putShort(FORMAT_UNCOMPRESSED_PCM);
    header.putShort((short) format.getChannels());
    header.putInt((int) format.getSampleRate());

    //Byte Rate = sample rate * channels * bits per sample / 8
    int frameByteRate = format.getChannels() * format.getSampleSizeInBits() / 8;
    int byteRate = (int) (format.getSampleRate() * frameByteRate);
    header.putInt(byteRate);

    //Block Align
    header.putShort((short) frameByteRate);

    //Bits per Sample
    header.putShort((short) format.getSampleSizeInBits());

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

    return header;
}

From source file:org.hashtrees.test.HashTreesImplTest.java

private static ByteBuffer generateRandomKeyWithPrefix(int prefixValue) {
    byte[] randomBytes = randomBytes();
    byte[] key = new byte[ByteUtils.SIZEOF_INT + randomBytes.length];
    ByteBuffer bb = ByteBuffer.wrap(key);
    bb.putInt(prefixValue);
    bb.put(randomBytes);//from w  ww  .  j  a v  a 2 s .  c  o m
    return bb;
}

From source file:com.aerohive.nms.engine.admin.task.licensemgr.license.processor2.PacketUtil.java

public static byte[] join(Header header, byte[] content) {
    ByteBuffer buf = ByteBuffer.allocate(8192);

    byte[] outBytes;
    if (content.length == 0) {
        outBytes = new byte[0];
    } else {//w  ww  .j a  v a  2  s .  c o m
        if (header.isSecretFlag()) {
            // encrypt data
            outBytes = encryptData(content);
        } else {
            outBytes = new byte[content.length];
            System.arraycopy(content, 0, outBytes, 0, content.length);
        }
    }
    buf.put(header.getType());
    buf.putInt(outBytes.length);
    buf.put(header.getProtocolVersion());
    buf.put(header.isSecretFlag() ? CommConst.Secret_Flag_Yes : CommConst.Secret_Flag_No);
    buf.put(outBytes);

    buf.flip();

    byte[] dst = new byte[buf.limit()];
    buf.get(dst);
    return dst;
}

From source file:org.wso2.carbon.analytics.data.commons.utils.AnalyticsCommonUtils.java

public static byte[] serializeObject(Object obj) {
    Kryo kryo = kryoTL.get();/*  ww w.  ja  v a  2s  .co m*/
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    try (Output out = new Output(byteOut)) {
        kryo.writeClassAndObject(out, obj);
        out.flush();
        byte[] data = byteOut.toByteArray();
        ByteBuffer result = ByteBuffer.allocate(data.length + Integer.SIZE / 8);
        result.putInt(data.length);
        result.put(data);
        return result.array();
    }
}

From source file:com.healthmarketscience.jackcess.impl.IndexImpl.java

/**
 * Writes the logical index definitions into a table definition buffer.
 * @param buffer Buffer to write to/*from w  w w.ja v a2 s . co m*/
 * @param indexes List of IndexBuilders to write definitions for
 */
protected static void writeDefinitions(TableCreator creator, ByteBuffer buffer) throws IOException {
    // write logical index information
    for (IndexBuilder idx : creator.getIndexes()) {
        TableCreator.IndexState idxState = creator.getIndexState(idx);
        buffer.putInt(TableImpl.MAGIC_TABLE_NUMBER); // seemingly constant magic value which matches the table def
        buffer.putInt(idxState.getIndexNumber()); // index num
        buffer.putInt(idxState.getIndexDataNumber()); // index data num
        buffer.put((byte) 0); // related table type
        buffer.putInt(INVALID_INDEX_NUMBER); // related index num
        buffer.putInt(0); // related table definition page number
        buffer.put((byte) 0); // cascade updates flag
        buffer.put((byte) 0); // cascade deletes flag
        buffer.put(idx.getType()); // index type flags
        buffer.putInt(0); // unknown
    }

    // write index names
    for (IndexBuilder idx : creator.getIndexes()) {
        TableImpl.writeName(buffer, idx.getName(), creator.getCharset());
    }
}

From source file:org.hashtrees.test.HashTreesImplTest.java

private static byte[] generateBytesFrom(int... values) {
    byte[] result = new byte[values.length * ByteUtils.SIZEOF_INT];
    ByteBuffer bb = ByteBuffer.wrap(result);
    for (int value : values)
        bb.putInt(value);
    return result;
}

From source file:org.apache.htrace.impl.PackedBuffer.java

/**
 * Write the fixed-length request frame which starts packed RPC messages.
 *///from w  w w.  j  a  va 2s  .c  om
static void writeReqFrame(ByteBuffer bb, int methodId, long seq, int length) throws IOException {
    int oldPos = bb.position();
    boolean success = false;
    try {
        bb.order(ByteOrder.LITTLE_ENDIAN);
        bb.putInt(HRPC_MAGIC);
        bb.putInt(methodId);
        bb.putLong(seq);
        bb.putInt(length);
        success = true;
    } finally {
        if (!success) {
            bb.position(oldPos);
        }
    }
}

From source file:com.healthmarketscience.jackcess.Index.java

/**
 * Writes the logical index definitions into a table definition buffer.
 * @param buffer Buffer to write to//from ww  w .j a  va 2s.c o m
 * @param indexes List of IndexBuilders to write definitions for
 */
protected static void writeDefinitions(TableCreator creator, ByteBuffer buffer) throws IOException {
    // write logical index information
    for (IndexBuilder idx : creator.getIndexes()) {
        TableCreator.IndexState idxState = creator.getIndexState(idx);
        buffer.putInt(Table.MAGIC_TABLE_NUMBER); // seemingly constant magic value which matches the table def
        buffer.putInt(idxState.getIndexNumber()); // index num
        buffer.putInt(idxState.getIndexDataNumber()); // index data num
        buffer.put((byte) 0); // related table type
        buffer.putInt(INVALID_INDEX_NUMBER); // related index num
        buffer.putInt(0); // related table definition page number
        buffer.put((byte) 0); // cascade updates flag
        buffer.put((byte) 0); // cascade deletes flag
        buffer.put(idx.getType()); // index type flags
        buffer.putInt(0); // unknown
    }

    // write index names
    for (IndexBuilder idx : creator.getIndexes()) {
        Table.writeName(buffer, idx.getName(), creator.getCharset());
    }
}

From source file:org.midonet.netlink.rtnetlink.Link.java

static public ByteBuffer describeGetRequest(ByteBuffer buf, int index) {
    buf.put((byte) 0);
    buf.put((byte) 0);
    buf.putShort((short) 0);
    buf.putInt(index);
    buf.putInt(0);//  www.j  av  a2  s  .  c o m
    buf.putInt(0xffffffff);

    return buf;
}