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.linkedin.databus.core.DbusEventV2.java

public static int serializeEvent(DbusEventKey key, ByteBuffer buf, DbusEventInfo dbusEventInfo) {
    // Serialize a DbusEventV2 that has exact same contents as a DbusEventV1.
    final int start = buf.position();
    buf.put(DbusEventFactory.DBUS_EVENT_V2);
    buf.putInt(MAGIC);
    buf.putInt(0); // Header len placeholder
    buf.putInt(0); // Header crc placeholder
    buf.putInt(0); // Body CRC placeholder
    buf.putInt(0); // total length placeholder

    short attributes = 0;
    attributes = setOpCode(dbusEventInfo.getOpCode(), attributes, dbusEventInfo.getSrcId());
    attributes = setKeyType(key, attributes);
    if (dbusEventInfo.isEnableTracing()) {
        attributes |= FLAG_TRACE_ON;/*w w  w  . j a  va  2 s  .  co  m*/
    }

    if (dbusEventInfo.isReplicated()) {
        attributes |= FLAG_IS_REPLICATED;
    }

    DbusEventPart metadata = dbusEventInfo.getMetadata();
    if (shouldEncodePayloadPart(dbusEventInfo)) {
        attributes |= FLAG_HAS_PAYLOAD_PART;
    }
    if (metadata != null) {
        attributes |= FLAG_HAS_PAYLOAD_METADATA_PART;
    }
    buf.putShort(attributes);
    buf.putLong(dbusEventInfo.getTimeStampInNanos());
    buf.putInt(dbusEventInfo.getSrcId());
    buf.putShort(dbusEventInfo.getpPartitionId());
    buf.putLong(dbusEventInfo.getSequenceId());

    // Fixed part of header is done. Now for the variable header part
    setKey(buf, key);
    final int hdrEndPos = buf.position();

    if (metadata != null) {
        metadata.encode(buf);
    }

    if ((attributes & FLAG_HAS_PAYLOAD_PART) != 0) {
        ByteBuffer bb = dbusEventInfo.getValueByteBuffer();
        if (bb == null) {
            // Special case to encode when there is no data.
            bb = ByteBuffer.allocate(1).order(buf.order());
            bb.limit(0);
        }
        DbusEventPart valuePart = new DbusEventPart(SchemaDigestType.MD5, dbusEventInfo.getSchemaId(),
                dbusEventInfo.getPayloadSchemaVersion(), bb);
        valuePart.encode(buf);
    }
    final int end = buf.position();
    buf.putInt(start + HeaderLenOffset, hdrEndPos - start);
    buf.putInt(start + TotalLenOffset, end - start);

    long bodyCrc = ByteBufferCRC32.getChecksum(buf, hdrEndPos, end - hdrEndPos);
    Utils.putUnsignedInt(buf, start + BodyCrcOffset, bodyCrc);
    // Header CRC
    if (dbusEventInfo.isAutocommit()) {
        // Do the body CRC first, since that is included in the header CRC
        long hdrCrc = ByteBufferCRC32.getChecksum(buf, start + BodyCrcOffset,
                hdrEndPos - start - BodyCrcOffset);
        Utils.putUnsignedInt(buf, start + HeaderCrcOffset, hdrCrc);
    }
    return buf.position() - start;
}

From source file:net.jenet.BandwidthLimit.java

@Override
public void toBuffer(ByteBuffer buffer) {
    super.toBuffer(buffer);
    buffer.putInt(incomingBandwidth);
    buffer.putInt(outgoingBandwidth);//ww  w .ja  v a2s .c  om
}

From source file:com.mapr.storm.test.TailSpoutTest.java

private byte[] payload(String msg) {
    byte[] content = msg.getBytes(Charsets.UTF_8);
    ByteBuffer buf = ByteBuffer.allocate(4 + content.length);
    buf.putInt(content.length);
    buf.put(content);//from   w  ww.  j  av a 2s. c  om
    buf.flip();
    return buf.array();
}

From source file:org.opendaylight.lispflowmapping.implementation.serializer.EidToLocatorRecordSerializer.java

public void serialize(ByteBuffer replyBuffer, EidToLocatorRecord record) {
    replyBuffer.putInt(NumberUtil.asInt(record.getRecordTtl()));
    if (record.getLocatorRecord() != null) {
        replyBuffer.put((byte) record.getLocatorRecord().size());
    } else {/* w  w  w. j  a v a  2  s.c  o m*/
        replyBuffer.put((byte) 0);
    }
    replyBuffer.put((byte) NumberUtil.asShort(record.getMaskLength()));
    Action act = Action.NoAction;
    if (record.getAction() != null) {
        act = record.getAction();
    }
    replyBuffer.put((byte) ((act.getIntValue() << 5) | //
            ByteUtil.boolToBit(BooleanUtils.isTrue(record.isAuthoritative()), Flags.AUTHORITATIVE)));
    replyBuffer.position(replyBuffer.position() + Length.RESERVED);
    replyBuffer.putShort(NumberUtil.asShort(record.getMapVersion()));
    if (record.getLispAddressContainer() != null && record.getLispAddressContainer().getAddress() != null) {
        LispAddressSerializer.getInstance().serialize(replyBuffer,
                LispAFIConvertor.toAFI(record.getLispAddressContainer()));
    }

    if (record.getLocatorRecord() != null) {
        for (LocatorRecord locatorRecord : record.getLocatorRecord()) {
            LocatorRecordSerializer.getInstance().serialize(replyBuffer, locatorRecord);
        }
    }
}

From source file:org.opendaylight.lispflowmapping.lisp.serializer.MappingRecordSerializer.java

public void serialize(ByteBuffer replyBuffer, MappingRecord record) {
    replyBuffer.putInt(NumberUtil.asInt(record.getRecordTtl()));
    if (record.getLocatorRecord() != null) {
        replyBuffer.put((byte) record.getLocatorRecord().size());
    } else {//from   ww w  . j a  v  a  2 s.co  m
        replyBuffer.put((byte) 0);
    }
    if (record.getEid() != null && MaskUtil.getMaskForAddress(record.getEid().getAddress()) != -1) {
        replyBuffer.put((byte) NumberUtil.asShort(MaskUtil.getMaskForAddress(record.getEid().getAddress())));
    } else {
        replyBuffer.put((byte) 0);
    }
    Action act = Action.NoAction;
    if (record.getAction() != null) {
        act = record.getAction();
    }
    replyBuffer.put((byte) ((act.getIntValue() << 5) | //
            ByteUtil.boolToBit(BooleanUtils.isTrue(record.isAuthoritative()), Flags.AUTHORITATIVE)));
    replyBuffer.position(replyBuffer.position() + Length.RESERVED);
    replyBuffer.putShort(NumberUtil.asShort(record.getMapVersion()));
    if (record.getEid() != null && record.getEid().getAddress() != null) {
        LispAddressSerializer.getInstance().serialize(replyBuffer, record.getEid());
    }

    if (record.getLocatorRecord() != null) {
        for (LocatorRecord locatorRecord : record.getLocatorRecord()) {
            LocatorRecordSerializer.getInstance().serialize(replyBuffer, locatorRecord);
        }
    }
}

From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6StatsRequest.java

@Override
public void writeTo(ByteBuffer data) {
    data.putInt(this.vendor);
    data.putInt(this.msgsubtype);
    data.putInt((int) 0x0);//pad0
    data.putShort(this.outPort);
    data.putShort(this.match_len);
    data.put(this.tableId);
    for (int i = 0; i < 3; i++)
        data.put((byte) 0x0);//pad byte
}

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

private void performStringTestUsingByteBuffer(String original) {
    byte[] data = RabbitMQUtils.writeString(original);
    ByteBuffer buffer = ByteBuffer.allocate(data.length + 4);
    buffer.putInt(data.length);
    buffer.put(data);/*from   w w  w.j  a va 2 s  .co m*/
    buffer.position(0);
    String readString = RabbitMQUtils.readString(buffer);
    Assert.assertEquals(original, readString);
}

From source file:us.ihmc.pubsub.types.ByteBufferPubSubType.java

@Override
public void serialize(ByteBuffer data, SerializedPayload serializedPayload) throws IOException {
    if (CDR.getTypeSize(data.remaining() + 4) > maxSize) {
        throw new IOException("Data size is larger than the maximum size");
    }/*  ww  w. j  a v a  2s . c  om*/
    CDR.writeEncapsulation(serializedPayload);

    ByteBuffer target = serializedPayload.getData();
    target.putInt(data.remaining());
    target.put(data);
    serializedPayload.setLength(align(target.position()));
}

From source file:org.cosmo.common.file.VariableFilePartition.java

/**
 *   Writes an entry in which first 8 bytes stores the size of this entry follow by the actual content
 *   so an entry of 100 will take 108 bytes.
 *
 *   Also this operation appends to the end of the last file and returns the file position
 *//*  w w w  .  ja  v  a  2s.co  m*/
public synchronized long writeSizedEntry(byte[] b, int off, int len) throws IOException {
    // 8 bytes extra for Len (int4) + Marker (int4)
    int chunkId = chunkIdForWrite(size(), MetaBytes + len);
    long writePosition = _writeChannels[chunkId].position();
    ByteBuffer buf = ByteBuffer.allocate(MetaBytes + len);
    buf.putInt(len);
    buf.put(b, off, len);
    buf.putInt(StoreCompleteMarker);
    buf.rewind();
    _writeChannels[chunkId].write(buf);
    return writePosition + (_sizePerFile * chunkId);
}

From source file:edu.tsinghua.lumaqq.qq.packets.out._05.RequestAgentPacket.java

@Override
protected void putBody(ByteBuffer buf) {
    buf.putLong(0x0100000000000000L);//from   ww w  .ja  v a 2  s. c  o m
    buf.putInt(0);
    buf.putChar((char) user.getFileAgentToken().length);
    buf.put(user.getFileAgentToken());

    buf.put((byte) 0x04);
    buf.put((byte) 0x4C);
    buf.putInt(clusterId);
    buf.putInt(imageLength);
    buf.put(md5);
    buf.put(md5(fileName.getBytes()));
    buf.putChar((char) 0);
}