Example usage for java.nio ByteBuffer array

List of usage examples for java.nio ByteBuffer array

Introduction

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

Prototype

public final byte[] array() 

Source Link

Document

Returns the byte array which this buffer is based on, if there is one.

Usage

From source file:com.hengyi.japp.tools.UuidUtils.java

protected static String base64Uuid(UUID uuid) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());
    return Base64.encodeBase64URLSafeString(bb.array());
}

From source file:com.microsoft.applicationinsights.extensibility.initializer.SequencePropertyInitializer.java

private static String uuidToBase64() {
    Base64 base64 = new Base64();
    UUID uuid = UUID.randomUUID();
    ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());
    return base64.encodeBase64URLSafeString(bb.array());
}

From source file:Main.java

public static ByteBuffer copyBinary(final ByteBuffer orig) {
    if (orig == null) {
        return null;
    }//w  w w  . j a va  2s.com
    ByteBuffer copy = ByteBuffer.wrap(new byte[orig.remaining()]);
    if (orig.hasArray()) {
        System.arraycopy(orig.array(), orig.arrayOffset() + orig.position(), copy.array(), 0, orig.remaining());
    } else {
        orig.slice().get(copy.array());
    }

    return copy;
}

From source file:com.cloudera.branchreduce.impl.thrift.Writables.java

public static <T extends Writable> T fromByteBuffer(ByteBuffer bb, Class<T> clazz) {
    T instance = ReflectionUtils.newInstance(clazz, DUMMY);
    try {//from   w w  w  .  ja  va  2  s  . c o  m
        instance.readFields(
                new DataInputStream(new ByteArrayInputStream(bb.array(), bb.arrayOffset(), bb.limit())));
    } catch (IOException e) {
        LOG.error("Deserialization error for class: " + clazz, e);
    }
    return instance;
}

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/*w w w. j  av a2s  . com*/
 * @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:Main.java

public static byte[] getDHdataWithHash(byte[] hash, byte[] data) {
    int ost = (data.length + 20) % 16 > 0 ? 16 : 0;
    int size = ((20 + data.length) / 16) * 16 + ost;

    ByteBuffer buffer = ByteBuffer.allocate(size);
    buffer.put(hash);//from ww  w .  j  a  va  2  s .c o m
    buffer.put(data);

    byte[] rand = new byte[size - hash.length - data.length];
    new Random().nextBytes(rand);
    buffer.put(rand);
    return buffer.array();
}

From source file:Main.java

/**
 * Stores substring in bb/*  w  w  w .  j  a va  2s .c o m*/
 * 
 * @param bb
 * @param startIdx
 * @return new bb limit
 */
public static final int substring(final ByteBuffer bb, final int startIdx) {
    final int limit = bb.limit();
    if (startIdx >= limit) {
        bb.limit(0);
        return 0;
    } else {
        final byte[] array = bb.array();
        int i = 0;
        int j = startIdx;
        while (j < limit) {
            array[i] = array[j];
            j++;
            i++;
        }
        bb.limit(limit - startIdx);
        return bb.limit();
    }
}

From source file:Main.java

public static String completeJweFromSIM(String jweSIM) {
    // android.os.Debug.waitForDebugger();

    try {/*  www.  j  av a2 s  .  com*/
        if (jweSIM != null && jweSIM.length() > 0) {
            String parts[] = jweSIM.split("\\.");
            ;
            if (parts != null && parts.length == 5) {
                // retrieve hmac key
                byte hmac_key[] = Base64.decode(parts[4], Base64.URL_SAFE);
                if (hmac_key != null && hmac_key.length == 16) {
                    // init hash instance
                    Mac hmac = Mac.getInstance("HmacSHA256", "SC");
                    hmac.init(new SecretKeySpec(hmac_key, "HmacSHA256"));

                    byte[] aad = parts[0].getBytes();
                    long al = aad.length * 8;
                    byte[] iv_key = decodeB64(parts[2]);
                    byte[] cryptedBytes = decodeB64(parts[3]);

                    // build data to hash
                    byte[] hmacData = new byte[aad.length + iv_key.length + cryptedBytes.length + 8];
                    int offset = 0;
                    System.arraycopy(aad, offset, hmacData, 0, aad.length);
                    offset += aad.length;
                    System.arraycopy(iv_key, 0, hmacData, offset, iv_key.length);
                    offset += iv_key.length;
                    System.arraycopy(cryptedBytes, 0, hmacData, offset, cryptedBytes.length);
                    offset += cryptedBytes.length;
                    ByteBuffer buffer = ByteBuffer.allocate(8);
                    buffer.putLong(al);
                    System.arraycopy(buffer.array(), 0, hmacData, offset, 8);

                    // compute hac value
                    byte[] hmacValue = hmac.doFinal(hmacData);
                    // authentication tag
                    byte[] auth_tag = Arrays.copyOf(hmacValue, 16);
                    String auth_tag64 = encodeB64(auth_tag);

                    // A.2.7. Complete Representation
                    String finalString = parts[0] + "." + parts[1] + "." + parts[2] + "." + parts[3] + "."
                            + auth_tag64;

                    //                  // just for verification
                    //                  byte jwt64 [] = decryptJWE(finalString, RsaKeyTim.privRsaKey);
                    //                  if(jwt64!=null) {
                    //                     String jws = new String(jwt64);
                    //                     Log.d("completeJweFromSIM", "jws verify Key TIM :"+verifyJWS(jws,RsaKeyTim.pubRsaKey));
                    //                  }

                    return finalString;
                }
            }
            // 
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.liveramp.commons.util.BytesUtils.java

public static byte[] deepCopyByteBufferToByteArray(ByteBuffer byteBuffer) {
    byte[] result = new byte[byteBuffer.remaining()];
    System.arraycopy(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(), result, 0,
            byteBuffer.remaining());//from   w  w w  . ja  v a 2s  .  com
    return result;
}

From source file:com.willetinc.hadoop.mapreduce.dynamodb.AttributeValueIOUtils.java

/**
 * <p>/*w ww  .j a  va2s.  c o  m*/
 * Creates a string representation of the specified AttributeValue.
 * </p>
 * 
 * <p>
 * Supported AttributeValue types are Number, String, and Binary. Throws an
 * IllegalArgumentException if any other types are specified.
 * </p>
 * 
 * @param type
 *            AttributeValue type
 * @param value
 *            value to serialize.
 * @return String representation of value.
 */
public static String toString(Types type, AttributeValue value) {
    if (null == value)
        return null;

    switch (type) {
    case STRING:
        return value.getS();
    case NUMBER:
        return value.getN();
    case BINARY:
        ByteBuffer buf = value.getB();
        return Base64.encodeBase64String(buf.array());

    default:
        throw new IllegalArgumentException("Only String, Number and Binary types are supported");
    }
}