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[] sumByte(byte[] bytes1, byte[] bytes2) {
    ByteBuffer byteBuffer = ByteBuffer.allocate(bytes1.length + bytes2.length);
    byteBuffer.put(bytes1);
    byteBuffer.put(bytes2);//from   ww w .ja va 2 s .  co m
    return byteBuffer.array();
}

From source file:Main.java

/**
 * Split short value into two byte buffer. First byte of short will be written to first byte buffer and second to second one.
 * /*  ww w  .ja va  2  s. c  om*/
 * @param buffer
 *          to write first part of value
 * @param buffer1
 *          to write second part of value
 */
public static void splitShortToBuffers(ByteBuffer buffer, ByteBuffer buffer1, short iValue) {
    buffer.put((byte) (MASK & (iValue >>> SIZE_OF_BYTE_IN_BITS)));
    buffer1.put((byte) (MASK & iValue));
}

From source file:Main.java

public final static ByteBuffer toNALFileFormat(final ByteBuffer buffer) {
    ByteBuffer result = ByteBuffer.allocate(buffer.remaining());
    result.put(buffer);
    result.flip();/*w  w  w  .j av  a 2  s. c om*/
    int length = 0;
    int position = -1;
    int remaining = result.remaining() - 3;
    for (int i = 0; i < remaining; ++i) {
        if (result.get(i) == 0x00 && result.get(i + 1) == 0x00 && result.get(i + 2) == 0x00
                && result.get(i + 3) == 0x01) {
            if (0 <= position) {
                result.putInt(position, length - 3);
            }
            position = i;
            length = 0;
        } else {
            ++length;
        }
    }
    result.putInt(position, length);
    return result;
}

From source file:Main.java

private static short stream2Short(byte[] stream, int offset) {
    ByteBuffer buffer = ByteBuffer.allocate(2);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.put(stream[offset]);
    buffer.put(stream[offset + 1]);/*from www  . j  av a  2s.com*/
    return buffer.getShort(0);
}

From source file:Main.java

public static ByteBuffer copy(ByteBuffer buffer) {

    final int length = buffer.limit() - buffer.position();
    final ByteOrder o = buffer.order();
    final ByteBuffer r = ByteBuffer.allocate(length);
    r.order(o);/*w w  w  .  j a v a 2  s  .com*/

    r.put(buffer);
    r.clear(); // Reset position and limit after the put()

    return r;

}

From source file:Main.java

public static ByteBuffer copyByteBuffer(ByteBuffer buf) {
    ByteBuffer dest = newByteBuffer(buf.remaining());
    buf.mark();/*  w w w .  ja  v a2 s .c  om*/
    dest.put(buf);
    buf.reset();
    dest.rewind();
    return dest;
}

From source file:Main.java

public static ByteBuffer cloneOnHeap(final ByteBuffer buf) {
    if (buf == null) {
        return null;
    }//from   w  w  w .java  2s  .c om
    buf.rewind();

    final ByteBuffer copy = createByteBufferOnHeap(buf.limit());
    copy.put(buf);

    return copy;
}

From source file:Main.java

/**
 * Make a direct NIO ByteBuffer from an array of floats
 * @param arr The array/* www.ja v a  2  s  .  c  om*/
 * @return The newly created FloatBuffer
 */
public static ByteBuffer makeByteBuffer(byte[] arr) {
    ByteBuffer bb = ByteBuffer.allocateDirect(arr.length);
    bb.order(ByteOrder.nativeOrder());
    bb.put(arr);
    bb.position(0);
    return bb;
}

From source file:Main.java

public static ByteBuffer clone(ByteBuffer original) {
    ByteBuffer clone = ByteBuffer.allocate(original.capacity());
    original.rewind();//copy from the beginning
    clone.put(original);
    original.rewind();/*from   w  w w. j av  a2s  . c  o  m*/
    clone.flip();
    return clone;
}

From source file:com.astamuse.asta4d.web.util.SecureIdGenerator.java

public static String createEncryptedURLSafeId() {
    try {/*from  w w  w  . jav a  2 s  .c  o m*/
        byte[] idBytes = IdGenerator.createIdBytes();

        ByteBuffer bb = ByteBuffer.allocate(idBytes.length + 4);
        bb.put(idBytes);

        // add random salt
        bb.putInt(sr.nextInt());

        MessageDigest crypt = MessageDigest.getInstance("SHA-1");
        return Base64.encodeBase64URLSafeString(crypt.digest(bb.array()));

    } catch (NoSuchAlgorithmException e) {
        // impossible
        throw new RuntimeException(e);
    }
}