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.healthmarketscience.jackcess.impl.OleUtil.java

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

    byte[] fileNameBytes = getZeroTermStrBytes(oleBuilder.getFileName());
    byte[] filePathBytes = getZeroTermStrBytes(oleBuilder.getFilePath());

    int headerLen = 6 + fileNameBytes.length + filePathBytes.length;

    if (oleBuilder.getType() == ContentType.SIMPLE_PACKAGE) {

        headerLen += 8 + filePathBytes.length;

    } else {// w w w.j a va 2 s . co m

        headerLen += 2;
    }

    byte[] headerBytes = new byte[headerLen];
    ByteBuffer bb = PageChannel.wrap(headerBytes);
    bb.putShort((short) PACKAGE_STREAM_SIGNATURE);
    bb.put(fileNameBytes);
    bb.put(filePathBytes);

    if (oleBuilder.getType() == ContentType.SIMPLE_PACKAGE) {
        bb.putInt(PS_EMBEDDED_FILE);
        bb.putInt(filePathBytes.length);
        bb.put(filePathBytes, 0, filePathBytes.length);
        bb.putInt((int) oleBuilder.getContentLength());
    } else {
        bb.putInt(PS_LINKED_FILE);
        bb.putShort((short) LINK_HEADER);
    }

    return headerBytes;
}

From source file:mil.nga.giat.geowave.datastore.accumulo.AccumuloDataStore.java

protected static byte[] getRowIdBytes(final GeowaveRowId rowElements) {
    final ByteBuffer buf = ByteBuffer.allocate(12 + rowElements.getDataId().length
            + rowElements.getAdapterId().length + rowElements.getInsertionId().length);
    buf.put(rowElements.getInsertionId());
    buf.put(rowElements.getAdapterId());
    buf.put(rowElements.getDataId());//from   w  w  w .ja  v a2  s  .  com
    buf.putInt(rowElements.getAdapterId().length);
    buf.putInt(rowElements.getDataId().length);
    buf.putInt(rowElements.getNumberOfDuplicates());
    return buf.array();
}

From source file:com.linkedin.databus.core.DbusEventV2.java

public static void setKey(ByteBuffer buf, DbusEventKey key) {
    switch (key.getKeyType()) {
    case STRING:/*  w ww  . j  a v  a  2s  .  c  o m*/
        byte[] keyBytes = key.getStringKeyInBytes();
        buf.putInt(keyBytes.length).put(keyBytes);
        break;
    case LONG:
        buf.putLong(key.getLongKey());
        break;
    case SCHEMA:
        key.getSchemaKey().encode(buf);
        break;
    default:
        throw new UnsupportedOperationException("Unimplemented key type:" + key.getKeyType());
    }
}

From source file:com.kactech.otj.Utils.java

public static ByteBuffer seal(String msg, String nymID, PublicKey nymKey, SecretKeySpec aesSecret,
        IvParameterSpec vector) throws InvalidKeyException, InvalidAlgorithmParameterException,
        IllegalBlockSizeException, BadPaddingException {
    ByteBuffer buff = ByteBuffer.allocate(msg.length() + 500);//donno?
    buff.order(ByteOrder.BIG_ENDIAN);
    buff.putShort((short) 1);//asymmetric
    buff.putInt(1);//array size
    buff.putInt(nymID.length() + 1);// ww  w .  j a  va  2 s  . c  o m
    buff.put(bytes(nymID + '\0', US_ASCII));

    // create encoded key and message
    Cipher cipher;
    try {
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    cipher.init(Cipher.ENCRYPT_MODE, aesSecret, vector);
    byte[] encrypted = cipher.doFinal(bytes(msg + '\0', UTF8));
    try {
        cipher = Cipher.getInstance(WRAP_ALGO);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    cipher.init(Cipher.WRAP_MODE, nymKey);
    byte[] encKeyBytes = cipher.wrap(aesSecret);

    buff.putInt(encKeyBytes.length);
    buff.put(encKeyBytes);
    buff.putInt(vector.getIV().length);
    buff.put(vector.getIV());
    buff.put(encrypted);
    buff.flip();

    return buff;
}

From source file:me.whitmarbut.mfa.TOTP.java

private byte[] getHmac(int timestamp, byte[] key) throws NoSuchAlgorithmException, InvalidKeyException {
    SecretKeySpec key_spec = new SecretKeySpec(key, "HmacSHA1");
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(key_spec);//from   ww w  .  j a v  a 2  s .  c o  m
    byte[] bin_timestamp = ByteBuffer.allocate(4).putInt(timestamp).array();

    ByteBuffer bbuff = ByteBuffer.allocate(8);
    bbuff.putInt(0); //Left pad 4 bytes to make a 64 bit int
    bbuff.putInt(timestamp);

    return mac.doFinal(bbuff.array());
}

From source file:com.eventsourcing.layout.binary.ByteArrayBinarySerializer.java

@Override
public void serialize(Object value, ByteBuffer buffer) {
    byte[] bytes = getPrimitiveArray(value);
    buffer.putInt(bytes.length);
    buffer.put(bytes);//www  . ja va  2s .  c  o  m
}

From source file:MainClass.java

public void run() {
    try {/*from  w w w  .  j  ava2  s  .c  o  m*/
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.putInt(this.howMany);
        buffer.flip();
        while (buffer.hasRemaining())
            out.write(buffer);

        for (int i = 0; i < howMany; i++) {
            byte[] data = new BigInteger(Integer.toString(i)).toByteArray();
            buffer = ByteBuffer.allocate(4 + data.length);

            buffer.putInt(data.length);
            buffer.put(data);
            buffer.flip();

            while (buffer.hasRemaining())
                out.write(buffer);
        }
        out.close();
        System.err.println("Closed");
    } catch (IOException ex) {
        System.err.println(ex);
    }
}

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

public void streamData(InputStream is, String name) throws IOException {
    int messageId = 0;
    int length = 0;
    byte[] nameBytes = RabbitMQUtils.writeString(name);
    byte[] array = new byte[messageSize + nameBytes.length + 8];
    ByteBuffer buffer = ByteBuffer.wrap(array);
    buffer.putInt(nameBytes.length);
    buffer.put(nameBytes);// w  ww  .ja v  a  2  s. c  om
    int messageIdPos = buffer.position();
    int dataStartPos = messageIdPos + 4;
    do {
        buffer.position(messageIdPos);
        buffer.putInt(messageId);
        length = is.read(array, dataStartPos, array.length - dataStartPos);
        queue.channel.basicPublish("", queue.name, MessageProperties.MINIMAL_PERSISTENT_BASIC,
                Arrays.copyOf(array, (length > 0) ? (dataStartPos + length) : dataStartPos));
        ++messageId;
    } while (length > 0);
}

From source file:org.onlab.packet.MplsTest.java

@Before
public void setUp() throws Exception {
    // Replace normal deserializer map with an empty map. This will cause
    // the DataDeserializer to be used which will silently handle 0-byte input.
    MPLS.protocolDeserializerMap = new HashMap<>();

    deserializer = MPLS.deserializer();//  ww  w  . java  2s  . c o m

    ByteBuffer bb = ByteBuffer.allocate(MPLS.HEADER_LENGTH);
    bb.putInt(((label & 0x000fffff) << 12) | ((bos & 0x1) << 8 | (ttl & 0xff)));

    bytes = bb.array();
}

From source file:org.springframework.integration.ip.tcp.converter.ByteArrayLengthHeaderConverter.java

/**
 * Writes the byte[] to the output stream, preceded by a 4 byte
 * length in network byte order (big endian).
 *//*from  w w w.  j a v a 2  s  . com*/
public void convert(byte[] bytes, OutputStream outputStream) throws IOException {
    ByteBuffer lengthPart = ByteBuffer.allocate(4);
    lengthPart.putInt(bytes.length);
    outputStream.write(lengthPart.array());
    outputStream.write(bytes);
    outputStream.flush();
}