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.alibaba.napoli.metamorphosis.client.producer.SimpleMessageProducer.java

/**
 * ??payload</br></br> 01attribute + payload
 * //  ww  w.j  a  v a2 s  .c o m
 * @param message
 * @return
 */
public static byte[] encodeData(final Message message) {
    final byte[] payload = message.getData();
    final String attribute = message.getAttribute();
    byte[] attrData = null;
    if (attribute != null) {
        attrData = ByteUtils.getBytes(attribute);
    } else {
        return payload;
    }
    // attributenull
    final int attrLen = attrData == null ? 0 : attrData.length;
    final ByteBuffer buffer = ByteBuffer.allocate(4 + attrLen + payload.length);
    if (attribute != null) {
        buffer.putInt(attrLen);
        if (attrData != null) {
            buffer.put(attrData);
        }
    }

    buffer.put(payload);
    return buffer.array();
}

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

static public ByteBuffer describeGetRequest(ByteBuffer buf, String ifname) {
    buf.put((byte) 0);
    buf.put((byte) 0);
    buf.putShort((short) 0);
    buf.putInt(0);
    buf.putInt(0);//from   w  w w .jav a 2s. c o  m
    buf.putInt(0xffffffff);

    NetlinkMessage.writeStringAttr(buf, Attr.IFLA_IFNAME, ifname);

    return buf;
}

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

static public ByteBuffer describeDelRequest(ByteBuffer buf, Link link) {
    IfinfoMsg ifi = link.ifi;/*from ww w  .ja  v a  2  s .co m*/
    buf.put(ifi.family);
    buf.put((byte) 0);
    buf.putShort(ifi.type);
    buf.putInt(ifi.index);
    buf.putInt(ifi.flags);
    buf.putInt(ifi.change);

    NetlinkMessage.writeAttrNested(buf, Attr.IFLA_LINKINFO, link.info);

    if (link.ifname != null) {
        NetlinkMessage.writeStringAttr(buf, Attr.IFLA_IFNAME, link.ifname);
    }

    return buf;
}

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

static public ByteBuffer describeSetAddrRequest(ByteBuffer buf, Link link, MAC mac) {
    IfinfoMsg ifi = link.ifi;//from  ww w.j av a  2 s.  c  o  m
    buf.put(ifi.family);
    buf.put((byte) 0);
    buf.putShort(ifi.type);
    buf.putInt(ifi.index);
    buf.putInt(ifi.flags);
    buf.putInt(ifi.change);

    NetlinkMessage.writeRawAttribute(buf, Attr.IFLA_ADDRESS, mac.getAddress());

    return buf;
}

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

static public ByteBuffer describeSetMasterRequest(ByteBuffer buf, Link link, int masterIndex) {
    IfinfoMsg ifi = link.ifi;/*from   w ww . jav a 2  s.  c  om*/
    buf.put(ifi.family);
    buf.put((byte) 0);
    buf.putShort(ifi.type);
    buf.putInt(ifi.index);
    buf.putInt(ifi.flags);
    buf.putInt(ifi.change);

    NetlinkMessage.writeIntAttr(buf, Attr.IFLA_MASTER, masterIndex);

    return buf;
}

From source file:org.apache.bookkeeper.bookie.BookieJournalTest.java

private static void updateJournalVersion(JournalChannel jc, int journalVersion) throws IOException {
    long prevPos = jc.fc.position();
    try {/*from   ww  w  . j a v a 2 s  .  c  om*/
        ByteBuffer versionBuffer = ByteBuffer.allocate(4);
        versionBuffer.putInt(journalVersion);
        versionBuffer.flip();
        jc.fc.position(4);
        IOUtils.writeFully(jc.fc, versionBuffer);
        jc.fc.force(true);
    } finally {
        jc.fc.position(prevPos);
    }
}

From source file:com.github.seqware.queryengine.plugins.runners.hbasemr.MRHBasePluginRunner.java

public static String[] serializeParametersToString(Object[] parameters, PluginInterface mapReducePlugin,
        byte[][] sSet, byte[] dSet) {
    int num_guaranteed_parameters = 6;
    String[] str_params = new String[num_guaranteed_parameters];
    byte[] ext_serials = SerializationUtils.serialize(parameters);
    byte[] int_serials = SerializationUtils.serialize(mapReducePlugin.getInternalParameters());
    str_params[EXTERNAL_PARAMETERS] = Base64.encodeBase64String(ext_serials);
    str_params[INTERNAL_PARAMETERS] = Base64.encodeBase64String(int_serials);
    ByteBuffer bBuffer = ByteBuffer.allocate(1024 * 1024); // one MB should be enough for now
    bBuffer.putInt(sSet.length);
    for (byte[] arr : sSet) {
        bBuffer.putInt(arr.length);// www.j a  v  a2s.c  o m
        bBuffer.put(arr);
    }
    str_params[NUM_AND_SOURCE_FEATURE_SETS] = Base64.encodeBase64String(bBuffer.array());
    str_params[DESTINATION_FEATURE_SET] = Base64.encodeBase64String(dSet);
    str_params[SETTINGS_MAP] = Base64
            .encodeBase64String(SerializationUtils.serialize(new Object[] { Constants.getSETTINGS_MAP() }));
    str_params[PLUGIN_CLASS] = Base64
            .encodeBase64String(SerializationUtils.serialize(mapReducePlugin.getClass()));

    return str_params;
}

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

private static byte[] writePackageStreamFooter(OleBlob.Builder oleBuilder) {

    // note, these are _not_ zero terminated
    byte[] fileNameBytes = oleBuilder.getFileName().getBytes(OLE_UTF_CHARSET);
    byte[] filePathBytes = oleBuilder.getFilePath().getBytes(OLE_UTF_CHARSET);

    int footerLen = 12 + (filePathBytes.length * 2) + fileNameBytes.length;

    byte[] footerBytes = new byte[footerLen];
    ByteBuffer bb = PageChannel.wrap(footerBytes);

    bb.putInt(filePathBytes.length / 2);
    bb.put(filePathBytes);//from   ww  w .  j  av  a 2 s . com
    bb.putInt(fileNameBytes.length / 2);
    bb.put(fileNameBytes);
    bb.putInt(filePathBytes.length / 2);
    bb.put(filePathBytes);

    return footerBytes;
}

From source file:org.wso2.carbon.analytics.datasource.core.util.GenericUtils.java

public static byte[] encodeElement(String name, Object value) throws AnalyticsException {
    ByteBuffer buffer = ByteBuffer.allocate(calculateBufferSizePerElement(name, value));
    String strVal;// w w  w . ja  v  a  2s  .com
    boolean boolVal;
    byte[] binData;

    buffer.putInt(name.getBytes(StandardCharsets.UTF_8).length);
    buffer.put(name.getBytes(StandardCharsets.UTF_8));
    if (value instanceof String) {
        buffer.put(DATA_TYPE_STRING);
        strVal = (String) value;
        buffer.putInt(strVal.getBytes(StandardCharsets.UTF_8).length);
        buffer.put(strVal.getBytes(StandardCharsets.UTF_8));
    } else if (value instanceof Long) {
        buffer.put(DATA_TYPE_LONG);
        buffer.putLong((Long) value);
    } else if (value instanceof Double) {
        buffer.put(DATA_TYPE_DOUBLE);
        buffer.putDouble((Double) value);
    } else if (value instanceof Boolean) {
        buffer.put(DATA_TYPE_BOOLEAN);
        boolVal = (Boolean) value;
        if (boolVal) {
            buffer.put(BOOLEAN_TRUE);
        } else {
            buffer.put(BOOLEAN_FALSE);
        }
    } else if (value instanceof Integer) {
        buffer.put(DATA_TYPE_INTEGER);
        buffer.putInt((Integer) value);
    } else if (value instanceof Float) {
        buffer.put(DATA_TYPE_FLOAT);
        buffer.putFloat((Float) value);
    } else if (value instanceof byte[]) {
        buffer.put(DATA_TYPE_BINARY);
        binData = (byte[]) value;
        buffer.putInt(binData.length);
        buffer.put(binData);
    } else if (value == null) {
        buffer.put(DATA_TYPE_NULL);
    } else {
        buffer.put(DATA_TYPE_OBJECT);
        binData = GenericUtils.serializeObject(value);
        buffer.putInt(binData.length);
        buffer.put(binData);
    }

    return buffer.array();
}

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

private static byte[] writePackageHeader(OleBlob.Builder oleBuilder, long contentLen) {

    byte[] prettyNameBytes = getZeroTermStrBytes(oleBuilder.getPrettyName());
    String className = oleBuilder.getClassName();
    String typeName = oleBuilder.getTypeName();
    if (className == null) {
        className = typeName;/* w  w  w  . ja v  a  2  s .c o m*/
    } else if (typeName == null) {
        typeName = className;
    }
    byte[] classNameBytes = getZeroTermStrBytes(className);
    byte[] typeNameBytes = getZeroTermStrBytes(typeName);

    int packageHeaderLen = 20 + prettyNameBytes.length + classNameBytes.length;

    int oleHeaderLen = 24 + typeNameBytes.length;

    byte[] headerBytes = new byte[packageHeaderLen + oleHeaderLen];

    ByteBuffer bb = PageChannel.wrap(headerBytes);

    // write outer package header
    bb.putShort((short) PACKAGE_SIGNATURE);
    bb.putShort((short) packageHeaderLen);
    bb.putInt(PACKAGE_OBJECT_TYPE);
    bb.putShort((short) prettyNameBytes.length);
    bb.putShort((short) classNameBytes.length);
    int prettyNameOff = bb.position() + 8;
    bb.putShort((short) prettyNameOff);
    bb.putShort((short) (prettyNameOff + prettyNameBytes.length));
    bb.putInt(-1);
    bb.put(prettyNameBytes);
    bb.put(classNameBytes);

    // put ole header
    bb.putInt(OLE_VERSION);
    bb.putInt(OLE_FORMAT);
    bb.putInt(typeNameBytes.length);
    bb.put(typeNameBytes);
    bb.putLong(0L);
    bb.putInt((int) contentLen);

    return headerBytes;
}