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:com.offbynull.portmapper.pcp.FilterPcpOption.java

private static ByteBuffer toDataSection(int prefixLength, int remotePeerPort, InetAddress remotePeerIpAddress) {
    Validate.inclusiveBetween(0, 128, prefixLength); // 0 indicates 'no filter'
    Validate.inclusiveBetween(0, 65535, remotePeerPort); // 0 indicates 'all ports'
    Validate.notNull(remotePeerIpAddress);

    ByteBuffer buffer = ByteBuffer.allocate(20);
    buffer.put((byte) 0); // reserved
    buffer.put((byte) prefixLength);
    buffer.putShort((short) remotePeerPort);
    buffer.put(NetworkUtils.convertToIpv6Array(remotePeerIpAddress));

    return buffer;
}

From source file:com.oneguy.recognize.Util.java

public static ByteBuffer doubleSize(ByteBuffer buffer) {
    if (buffer == null) {
        return null;
    }/*w ww . j  a v  a2 s. c o m*/
    byte[] content = new byte[buffer.position()];
    buffer.flip();
    buffer.get(content);
    ByteBuffer newBuffer = ByteBuffer.allocate(buffer.capacity() * 2);
    newBuffer.put(content);
    return newBuffer;
}

From source file:com.smartitengineering.cms.api.impl.Utils.java

public static String readStringInUTF8(DataInput in) throws IOException, UnsupportedEncodingException {
    int allocationBlockSize = 2000;
    int capacity = allocationBlockSize;
    int length = 0;
    ByteBuffer buffer = ByteBuffer.allocate(allocationBlockSize);
    boolean notEof = true;
    while (notEof) {
        try {/*from   w  w w.ja va  2  s. co m*/
            buffer.put(in.readByte());
            if (++length >= capacity) {
                capacity += allocationBlockSize;
                buffer.limit(capacity);
            }
        } catch (EOFException ex) {
            notEof = false;
        }
    }
    String string = StringUtils.newStringUtf8(Arrays.copyOf(buffer.array(), length));
    return string;
}

From source file:com.inmobi.messaging.util.AuditUtil.java

public static void attachHeaders(Message m, Long timestamp) {
    byte[] b = m.getData().array();
    int messageSize = b.length;
    int totalSize = messageSize + HEADER_LENGTH;
    ByteBuffer buffer = ByteBuffer.allocate(totalSize);

    // writing version
    buffer.put((byte) currentVersion);
    // writing magic bytes
    buffer.put(magicBytes);//from  w  ww  .  j ava  2s .  c  o  m
    // writing timestamp
    long time = timestamp;
    buffer.putLong(time);

    // writing message size
    buffer.putInt(messageSize);
    // writing message
    buffer.put(b);
    buffer.rewind();
    m.set(buffer);
    // return new Message(buffer);

}

From source file:Main.java

public final static void write(final byte[] array, final int offset, final int end, final ByteBuffer outBB) {
    byte b;/*from   ww w .  jav a  2s.  c  o m*/
    for (int i = offset; i < end; i++) {
        b = array[i];
        if (b == '|') {
            outBB.put((byte) ',').put((byte) ' ');
        } else if (b == '\'') {
            boolean found = false;
            while (++i < end) {
                b = array[i];
                if (b == '\'') {
                    found = true;
                    continue;
                } else {
                    if (!found) {
                        outBB.put((byte) '\'');
                    }
                    outBB.put(b);
                    break;
                }
            }
            continue;
        } else {
            outBB.put(b);
        }
    }
}

From source file:Main.java

public static byte[] getAESkey(byte[] authKey, byte[] msgKey, boolean isOut) {
    int x = 0;// ww  w  .ja v a2  s . co m
    if (!isOut)
        x = 8;

    ByteBuffer tempA = ByteBuffer.allocate(msgKey.length + 32);
    tempA.put(msgKey);
    tempA.put(authKey, x, 32);
    byte[] a = getSHA1hash(tempA.array());

    ByteBuffer tempB = ByteBuffer.allocate(msgKey.length + 32);
    tempB.put(authKey, 32 + x, 16);
    tempB.put(msgKey);
    tempB.put(authKey, 48 + x, 16);
    byte[] b = getSHA1hash(tempB.array());

    ByteBuffer tempC = ByteBuffer.allocate(msgKey.length + 32);
    tempC.put(authKey, 64 + x, 32);
    tempC.put(msgKey);
    byte[] c = getSHA1hash(tempC.array());

    ByteBuffer key = ByteBuffer.allocate(8 + 12 + 12);
    key.put(a, 0, 8);
    key.put(b, 8, 12);
    key.put(c, 4, 12);

    return key.array();
}

From source file:com.tesora.dve.db.mysql.portal.protocol.MSPAuthenticateV10MessageMessage.java

public static byte[] computeSecurePassword(String password, String salt) {
    byte[] sha1password = DigestUtils.sha1(password);
    byte[] seedbytes = salt.getBytes();
    ByteBuffer bb = ByteBuffer.allocate(sha1password.length + seedbytes.length);
    bb.put(seedbytes);
    bb.put(DigestUtils.sha1(sha1password));
    byte[] sha1parttwo = DigestUtils.sha1(bb.array());
    byte[] securePassword = new byte[sha1password.length];
    for (int i = 0; i < securePassword.length; ++i)
        securePassword[i] = (byte) (sha1password[i] ^ sha1parttwo[i]);
    return securePassword;
}

From source file:Main.java

public static byte[] getAESiv(byte[] authKey, byte[] msgKey, boolean isOut) {
    int x = 0;//from   w w  w. j av  a2s  . com
    if (!isOut)
        x = 8;

    ByteBuffer tempA = ByteBuffer.allocate(msgKey.length + 32);
    tempA.put(msgKey);
    tempA.put(authKey, x, 32);
    byte[] a = getSHA1hash(tempA.array());

    ByteBuffer tempB = ByteBuffer.allocate(msgKey.length + 32);
    tempB.put(authKey, 32 + x, 16);
    tempB.put(msgKey);
    tempB.put(authKey, 48 + x, 16);
    byte[] b = getSHA1hash(tempB.array());

    ByteBuffer tempC = ByteBuffer.allocate(msgKey.length + 32);
    tempC.put(authKey, 64 + x, 32);
    tempC.put(msgKey);
    byte[] c = getSHA1hash(tempC.array());

    ByteBuffer tempD = ByteBuffer.allocate(msgKey.length + 32);
    tempD.put(msgKey);
    tempD.put(authKey, 96 + x, 32);
    byte[] d = getSHA1hash(tempD.array());

    ByteBuffer iv = ByteBuffer.allocate(12 + 8 + 4 + 8);
    iv.put(a, 8, 12);
    iv.put(b, 0, 8);
    iv.put(c, 16, 4);
    iv.put(d, 0, 8);

    return iv.array();
}

From source file:Main.java

public static ByteBuffer setupByteBuffer(ByteBuffer preBuffer, byte[] array) {

    if (preBuffer == null || preBuffer.capacity() < array.length) {
        preBuffer = createByteBuffer(array.length * 2);
    } else {//from   w ww . j  av a  2 s  . co m
        preBuffer.clear();
    }
    preBuffer.put(array);
    preBuffer.position(0);
    return preBuffer;
}

From source file:Main.java

public static byte[] padLeft(byte[] tgt, int len, byte padding) {
    if (tgt.length >= len) {
        return tgt;
    }//from w  w  w  . j a va2s  . co  m
    ByteBuffer buffer = ByteBuffer.allocate(len);
    byte[] paddings = new byte[len - tgt.length];
    Arrays.fill(paddings, padding);

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

    return buffer.array();
}