Example usage for java.nio ByteBuffer put

List of usage examples for java.nio ByteBuffer put

Introduction

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

Prototype

public ByteBuffer put(ByteBuffer src) 

Source Link

Document

Writes all the remaining bytes of the src byte buffer to this buffer's current position, and increases both buffers' position by the number of bytes copied.

Usage

From source file:Main.java

public static byte[] padRight(byte[] tgt, int len, byte padding) {
    if (tgt.length >= len) {
        return tgt;
    }/*from  w  ww . ja v  a2  s. c o  m*/
    byte[] paddings = new byte[len - tgt.length];
    Arrays.fill(paddings, padding);
    ByteBuffer buffer = ByteBuffer.allocate(len);

    buffer.put(tgt);
    buffer.put(paddings);

    return buffer.array();
}

From source file:Main.java

public static ByteBuffer makeByteBuffer(byte[] array) {
    if (array == null)
        throw new IllegalArgumentException();

    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length);
    byteBuffer.order(ByteOrder.nativeOrder());
    byteBuffer.put(array);
    byteBuffer.position(0);/*from ww  w.ja v a  2  s  .c  o m*/
    return byteBuffer;
}

From source file:Main.java

/** create AdvertiseDate for iBeacon */
public static AdvertiseData createEmployeeIDAdvertiseData(byte[] data, int length) {
    byte[] manufacturerData = new byte[23];
    ByteBuffer bb = ByteBuffer.wrap(manufacturerData);
    bb.order(ByteOrder.BIG_ENDIAN);
    bb.put((byte) 0x4d);
    bb.put((byte) 0x44);
    bb.put((byte) 0x43);
    bb.put(data);//from  w  w w. j  a v a  2  s.  com
    for (int i = 0; i < length; i++) {
        bb.put((byte) 0x00);
    }

    AdvertiseData.Builder builder = new AdvertiseData.Builder();
    builder.addManufacturerData(0x006d, manufacturerData);
    AdvertiseData adv = builder.build();
    return adv;
}

From source file:Main.java

public static ByteBuffer makeByteBuffer(byte[] array) {
    final int SIZE = Byte.SIZE / 8;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length * SIZE);
    byteBuffer.order(ByteOrder.nativeOrder());
    byteBuffer.put(array);
    byteBuffer.position(0);// w  ww  .ja  v  a2 s.co m
    return byteBuffer;
}

From source file:Main.java

/**
 * @param columnarKeyBlockData/*from w  ww. j ava 2s.  c  o m*/
 * @param columnarKeyStoreMetadata
 * @return
 * @author s71955 The high cardinality dimensions rows will be send in byte
 * array with its data length appended in the
 * ColumnarKeyStoreDataHolder byte array since high cardinality dim
 * data will not be part of MDKey/Surrogate keys. In this method the
 * byte array will be scanned and the length which is stored in
 * short will be removed.
 */
public static List<byte[]> readColumnarKeyBlockDataForNoDictionaryCols(byte[] columnarKeyBlockData) {
    List<byte[]> columnarKeyBlockDataList = new ArrayList<byte[]>(50);
    ByteBuffer noDictionaryValKeyStoreDataHolder = ByteBuffer.allocate(columnarKeyBlockData.length);
    noDictionaryValKeyStoreDataHolder.put(columnarKeyBlockData);
    noDictionaryValKeyStoreDataHolder.flip();
    while (noDictionaryValKeyStoreDataHolder.hasRemaining()) {
        short dataLength = noDictionaryValKeyStoreDataHolder.getShort();
        byte[] noDictionaryValKeyData = new byte[dataLength];
        noDictionaryValKeyStoreDataHolder.get(noDictionaryValKeyData);
        columnarKeyBlockDataList.add(noDictionaryValKeyData);
    }
    return columnarKeyBlockDataList;

}

From source file:Main.java

public static ByteBuffer makeByteBuffer(byte[] array)
/*     */ {/* w w w  .  ja v  a  2  s . c om*/
    /*  63 */int SIZE = 1;
    /*  64 */ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length * 1);
    /*  65 */byteBuffer.order(ByteOrder.nativeOrder());
    /*  66 */byteBuffer.put(array);
    /*  67 */byteBuffer.position(0);
    /*  68 */return byteBuffer;
    /*     */}

From source file:com.offbynull.portmapper.pcp.ThirdPartyPcpOption.java

private static ByteBuffer toDataSection(InetAddress internalIpAddress) {
    Validate.notNull(internalIpAddress);
    ByteBuffer buffer = ByteBuffer.allocate(16);
    buffer.put(NetworkUtils.convertToIpv6Array(internalIpAddress));

    return buffer;
}

From source file:Main.java

public static int writeVLong(ByteBuffer bb, long l) {
    int initPos = bb.position();
    if (l >= -112 && l <= 127) {
        bb.put((byte) l);
        return 1;
    }/*from   w w  w  . ja  v  a  2  s  .  co  m*/

    int len = -112;
    if (l < 0) {
        l ^= -1L; // take one's complement'
        len = -120;
    }

    long tmp = l;
    while (tmp != 0) {
        tmp = tmp >> 8;
        len--;
    }

    bb.put((byte) len);

    len = (len < -120) ? -(len + 120) : -(len + 112);

    for (int idx = len; idx != 0; idx--) {
        int shiftbits = (idx - 1) * 8;
        long mask = 0xFFL << shiftbits;
        bb.put((byte) ((l & mask) >> shiftbits));
    }
    return bb.position() - initPos;
}

From source file:com.offbynull.portmapper.common.ByteBufferUtils.java

/**
 * Copy the remaining content of a {@link ByteBuffer} in to a new array.
 * @param src buffer to copy/*from w ww.  j a v  a 2 s. c  o  m*/
 * @param incrementSrc of {@code true} increments {@code src}'s position
 * @return new buffer with the remaining content in {@code src}
 * @throws NullPointerException if any arguments are {@code null}
 */
public static byte[] copyContentsToArray(ByteBuffer src, boolean incrementSrc) {
    Validate.notNull(src);
    if (!incrementSrc) {
        src.mark();
    }

    ByteBuffer dst = ByteBuffer.allocate(src.remaining());
    dst.put(src);

    if (!incrementSrc) {
        src.reset();
    }

    return dst.array();
}

From source file:com.alibaba.otter.shared.common.utils.ByteUtils.java

public static int bytes2int(byte[] b) {
    ByteBuffer buf = ByteBuffer.allocate(4);
    buf.put(b);
    buf.flip();/*from   w  ww. ja v  a2  s . co  m*/
    return buf.getInt();
}