Example usage for java.nio ByteBuffer duplicate

List of usage examples for java.nio ByteBuffer duplicate

Introduction

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

Prototype

public abstract ByteBuffer duplicate();

Source Link

Document

Returns a duplicated buffer that shares its content with this buffer.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer bbuf = ByteBuffer.allocate(10);
    int capacity = bbuf.capacity(); // 10
    System.out.println(capacity);
    bbuf.putShort(2, (short) 123);

    ByteBuffer bb = bbuf.duplicate();

    System.out.println(bb.equals(bb));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer bbuf = ByteBuffer.allocate(10);
    int capacity = bbuf.capacity(); // 10
    System.out.println(capacity);
    bbuf.putShort(2, (short) 123);

    ByteBuffer bb = bbuf.duplicate();

    System.out.println(bb.compareTo(bb));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer bbuf = ByteBuffer.allocate(10);
    int capacity = bbuf.capacity(); // 10
    System.out.println(capacity);
    bbuf.putShort(2, (short) 123);

    ByteBuffer bb = bbuf.duplicate();

    System.out.println(Arrays.toString(bb.array()));
}

From source file:Main.java

public static byte[] array(ByteBuffer byteBuffer) {
    ByteBuffer byteBuffer2 = byteBuffer.duplicate();
    byte[] arrby = new byte[byteBuffer2.limit()];
    byteBuffer2.rewind();/* w  w  w.  ja  va  2  s.co  m*/
    byteBuffer2.get(arrby);
    return arrby;
}

From source file:Main.java

/**
 * @param buffer/*w ww.j a v  a2s  .  c o m*/
 * @return
 */
public static ByteBuffer duplicate(ByteBuffer buffer) {
    final ByteBuffer r = buffer.duplicate();
    r.order(buffer.order());

    return r;
}

From source file:Main.java

static void memcpy(ByteBuffer dstBuffer, int dstByteOffset, ByteBuffer srcBuffer, int srcByteOffset,
        int length) {
    ByteBuffer dstDup = dstBuffer.duplicate();
    dstDup.position(dstByteOffset);// w  w  w  .  j  a v a 2  s.  com
    dstDup.limit(dstByteOffset + length);
    ByteBuffer srcDup = srcBuffer.duplicate();
    srcDup.position(srcByteOffset);
    srcDup.limit(srcByteOffset + length);
    dstDup.put(srcDup);
}

From source file:net.geertvos.theater.core.util.UUIDGen.java

public static byte[] hash(ByteBuffer... data) {
    for (ByteBuffer block : data) {
        digester.update(block.duplicate());
    }/*from   w ww .  j a va2s.c  o  m*/

    return digester.digest();
}

From source file:com.linkedin.databus.core.util.StringUtils.java

/**
 * Dumps as a hex string the contents of a buffer around a position
 * @param buf     the ByteBuffer to dump
 * @param bufOfs  starting offset in the buffer
 * @param length  the number of bytes to print
 * @return the hexstring//  w w w  .j  a  v a  2s .co  m
 */
public static String hexdumpByteBufferContents(ByteBuffer buf, int bufOfs, int length) {
    if (length < 0) {
        return "";
    }

    final int endOfs = Math.min(buf.limit(), bufOfs + length + 1);
    final byte[] bytes = new byte[endOfs - bufOfs];

    buf = buf.duplicate();
    buf.position(bufOfs);
    buf.get(bytes);
    return new String(Hex.encodeHex(bytes));
}

From source file:com.datatorrent.stram.LaunchContainerRunnable.java

public static ByteBuffer getTokens(UserGroupInformation ugi,
        Token<StramDelegationTokenIdentifier> delegationToken) {
    try {/*from w ww.  j  a v a 2s . c o  m*/
        Collection<Token<? extends TokenIdentifier>> tokens = ugi.getCredentials().getAllTokens();
        Credentials credentials = new Credentials();
        for (Token<? extends TokenIdentifier> token : tokens) {
            if (!token.getKind().equals(AMRMTokenIdentifier.KIND_NAME)) {
                credentials.addToken(token.getService(), token);
                LOG.debug("Passing container token {}", token);
            }
        }
        credentials.addToken(delegationToken.getService(), delegationToken);
        DataOutputBuffer dataOutput = new DataOutputBuffer();
        credentials.writeTokenStorageToStream(dataOutput);
        byte[] tokenBytes = dataOutput.getData();
        ByteBuffer cTokenBuf = ByteBuffer.wrap(tokenBytes);
        return cTokenBuf.duplicate();
    } catch (IOException e) {
        throw new RuntimeException("Error generating delegation token", e);
    }
}

From source file:com.icloud.framework.core.util.FBUtilities.java

public static String decodeToUTF8(ByteBuffer bytes) throws CharacterCodingException {
    bytes = bytes.duplicate();
    String decoded = Charsets.UTF_8.newDecoder().decode(bytes).toString();
    return decoded;
}