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.sitewhere.hbase.device.HBaseDeviceEvent.java

/**
 * Gets the absolute first possible event key for cases where a start timestamp is not
 * specified.// ww  w . j  a  va2 s . c o  m
 * 
 * @param assnKey
 * @return
 */
protected static byte[] getAbsoluteStartKey(byte[] assnKey) {
    ByteBuffer buffer = ByteBuffer.allocate(assnKey.length + 4);
    buffer.put(assnKey);
    buffer.put((byte) 0x00);
    buffer.put((byte) 0x00);
    buffer.put((byte) 0x00);
    buffer.put((byte) 0x00);
    return buffer.array();
}

From source file:com.sitewhere.hbase.device.HBaseDeviceEvent.java

/**
 * Gets the absolute first possible event key for cases where a start timestamp is not
 * specified./*  w  w w  . j a v  a2 s . c o m*/
 * 
 * @param assnKey
 * @return
 */
protected static byte[] getAbsoluteEndKey(byte[] assnKey) {
    ByteBuffer buffer = ByteBuffer.allocate(assnKey.length + 4);
    buffer.put(assnKey);
    buffer.put((byte) 0xff);
    buffer.put((byte) 0xff);
    buffer.put((byte) 0xff);
    buffer.put((byte) 0xff);
    return buffer.array();
}

From source file:com.glaf.core.util.ByteBufferUtils.java

/**
 * @return a new copy of the data in @param buffer USUALLY YOU SHOULD USE
 *         ByteBuffer.duplicate() INSTEAD, which creates a new Buffer (so
 *         you can mutate its position without affecting the original)
 *         without copying the underlying array.
 *///w  ww  .  j a v  a  2  s .c  om
public static ByteBuffer clone(ByteBuffer buffer) {
    assert buffer != null;

    if (buffer.remaining() == 0)
        return EMPTY_BYTE_BUFFER;

    ByteBuffer clone = ByteBuffer.allocate(buffer.remaining());

    if (buffer.hasArray()) {
        System.arraycopy(buffer.array(), buffer.arrayOffset() + buffer.position(), clone.array(), 0,
                buffer.remaining());
    } else {
        clone.put(buffer.duplicate());
        clone.flip();
    }

    return clone;
}

From source file:gdv.xport.feld.Feld.java

private static String toBezeichnung(final String name) {
    String converted = name.replaceAll("_", " ");
    ByteBuffer outputBuffer = Config.DEFAULT_ENCODING.encode(converted);
    String convertedISO = new String(outputBuffer.array(), Config.DEFAULT_ENCODING);
    return WordUtils.capitalize(convertedISO.toLowerCase());
}

From source file:com.alibaba.napoli.metamorphosis.client.producer.SimpleMessageProducer.java

/**
 * ??payload</br></br> 01attribute + payload
 * /*from   ww w .  ja va2  s.  co  m*/
 * @param message
 * @return
 */
public static byte[] encodeData(final Message message) {
    final byte[] payload = message.getData();
    final String attribute = message.getAttribute();
    byte[] attrData = null;
    if (attribute != null) {
        attrData = ByteUtils.getBytes(attribute);
    } else {
        return payload;
    }
    // attributenull
    final int attrLen = attrData == null ? 0 : attrData.length;
    final ByteBuffer buffer = ByteBuffer.allocate(4 + attrLen + payload.length);
    if (attribute != null) {
        buffer.putInt(attrLen);
        if (attrData != null) {
            buffer.put(attrData);
        }
    }

    buffer.put(payload);
    return buffer.array();
}

From source file:com.sitewhere.hbase.device.HBaseDeviceEvent.java

/**
 * Get row key for a given event type and time.
 * /* w w w  .  ja v a 2s.c  o m*/
 * @param assnToken
 * @param eventType
 * @param time
 * @return
 * @throws SiteWhereException
 */
public static byte[] getRowKey(byte[] assnKey, long time) throws SiteWhereException {
    time = time / 1000;
    long bucket = time - (time % BUCKET_INTERVAL);
    byte[] bucketBytes = Bytes.toBytes(bucket);
    ByteBuffer buffer = ByteBuffer.allocate(assnKey.length + 4);
    buffer.put(assnKey);
    buffer.put((byte) ~bucketBytes[4]);
    buffer.put((byte) ~bucketBytes[5]);
    buffer.put((byte) ~bucketBytes[6]);
    buffer.put((byte) ~bucketBytes[7]);
    return buffer.array();
}

From source file:byps.test.TestUtils.java

public static String bufferToString(ByteBuffer buf) {
    try {/*  w  w  w . j a v a2  s . c o m*/
        boolean isString = true;
        byte[] arr = buf.array();
        for (int i = 0; i < buf.remaining(); i++) {
            byte c = arr[i];
            if (c <= 127 || ((c & 0xC0) == 0x80) || ((c & 0xE0) == 0xC0))
                continue;
            isString = false;
            break;
        }
        if (isString) {
            return new String(buf.array(), 0, buf.remaining(), "UTF-8");
        } else {
            StringBuilder sbuf = new StringBuilder();
            sbuf.append("byte[] bytes = new byte[] {");
            for (int i = 0; i < buf.remaining(); i++) {
                if (i != 0)
                    sbuf.append(", ");
                if ((i % 10) == 0)
                    sbuf.append("\r\n");
                sbuf.append("(byte)0x");
                int c = arr[i] & 0xFF;
                String s = Integer.toHexString(c);
                if (s.length() < 2)
                    sbuf.append("0");
                sbuf.append(s);
            }
            sbuf.append("};");
            return sbuf.toString();
        }
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException(e);
    }
}

From source file:net.servicestack.client.Utils.java

public static byte[] toGuidBytes(UUID theUuid) {
    ByteBuffer first8 = ByteBuffer.allocate(8);
    first8.putLong(theUuid.getMostSignificantBits());
    first8.rewind();//  ww w .j a v  a2 s.  c  o  m

    byte[] first4 = new byte[4];
    first8.get(first4);
    reverse(first4);

    byte[] second2 = new byte[2];
    first8.get(second2);
    reverse(second2);

    byte[] third2 = new byte[2];
    first8.get(third2);
    reverse(third2);

    ByteBuffer converted16 = ByteBuffer.allocate(16).put(first4).put(second2).put(third2);

    ByteBuffer last8 = ByteBuffer.allocate(8);
    last8.putLong(theUuid.getLeastSignificantBits());
    last8.rewind();

    converted16.put(last8);

    return converted16.array();
}

From source file:com.orange.oidc.secproxy_service.KryptoUtils.java

static String encryptJWE(byte[] bytes, Key pubRsaKey, byte[] cek) {
    // Log.d("","encryptJWE");
    try {/*  w  w w  .  jav  a2  s .  com*/
        // A.2.1
        // jwe header already computed as static
        // jweProtectedHeader;

        // A.2.2 Content Encryption Key (CEK)
        if (cek == null) {
            cek = generateRandomKey(256);
        }

        // Log.d("","cek: "+bytesToHex(cek));

        // A.2.3 Key Encryption
        String jweEncrypted64 = encryptRsaB64(cek, pubRsaKey);
        // Log.d("","jweEncrypted "+jweEncrypted64 );

        // A.2.4 Initialization Vector
        byte[] iv_key = generateRandomKey(128);

        // Log.d("","jweInitVector: "+bytesToHex(iv_key));
        String jweInitVector64 = encodeB64(iv_key);
        // Log.d("","jweInitVector64 "+jweInitVector64 );

        // A.2.5 Additional Authenticated Data
        byte[] aad = jweProtectedHeader.getBytes();

        // A.2.6. Content Encryption
        Cipher encrypt = Cipher.getInstance("AES/CBC/PKCS5Padding");

        // check cek result byte array
        if (cek == null || cek.length == 0 || (cek.length % 2) != 0)
            return null;
        int keySize = cek.length / 2;
        Log.d("", "Encryption AES: " + keySize * 8);

        byte aes_key[] = new byte[keySize];
        byte hmac_key[] = new byte[keySize];

        System.arraycopy(cek, 0, hmac_key, 0, keySize);
        System.arraycopy(cek, keySize, aes_key, 0, keySize);

        // Log.d("","hmac_key: "+bytesToHex(hmac_key));
        // Log.d("","aes_key: "+bytesToHex(aes_key));

        encrypt.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(aes_key, "AES"), new IvParameterSpec(iv_key));
        byte[] cryptedBytes = encrypt.doFinal(bytes);
        String cryptedBytes64 = encodeB64(cryptedBytes);

        // compute hmac
        long al = aad.length * 8;

        // concatenate aad, iv_key, cryptedBytes and al 
        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);

        // hmac
        Mac hmac = Mac.getInstance("HmacSHA256", "SC");
        hmac.init(new SecretKeySpec(hmac_key, "HmacSHA256"));
        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 = jweProtectedHeader + "." + jweEncrypted64 + "." + jweInitVector64 + "."
                + cryptedBytes64 + "." + auth_tag64;

        return finalString;

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:io.blobkeeper.file.util.FileUtils.java

public static long getCrc(@NotNull File file) {
    CRC32 crc = new CRC32();

    while (true) {
        ByteBuffer buffer = ByteBuffer.allocate(CHUNK_SIZE);
        while (buffer.hasRemaining()) {
            int bytes = 0;
            try {
                bytes = file.getFileChannel().read(buffer);
            } catch (IOException e) {
                log.error("Can't read blob file " + file, e);
                throw new IllegalArgumentException(e);
            }/*from  w w  w. j av  a  2s .  c o m*/
            if (bytes < 0) {
                break;
            }
        }
        buffer.flip();
        if (buffer.remaining() == 0) {
            break;
        } else {
            crc.update(buffer.array());
        }
    }

    return crc.getValue();
}