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:jsave.Utils.java

public static int read_uint16(final RandomAccessFile raf) throws IOException {
    byte[] data = new byte[2];
    raf.read(data);/*from   w w w.  j  a  v a 2s . c  o  m*/
    ByteBuffer bb = ByteBuffer.allocate(data.length);
    bb.put(data);
    return getUnsignedShort(bb);
}

From source file:jsave.Utils.java

public static long read_uint32(final RandomAccessFile raf) throws IOException {
    byte[] data = new byte[4];
    raf.read(data);//from  www  .  j a  v  a 2  s. c o  m
    ByteBuffer bb = ByteBuffer.allocate(data.length);
    bb.put(data);
    return getUnsignedInt(bb);
}

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 ww. j a  v a  2 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.//from   www  . ja  va2s . c  om
 * 
 * @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.sitewhere.hbase.device.HBaseDeviceEvent.java

/**
 * Get row key for a given event type and time.
 * /*from   w  ww.j a v  a2 s  .co 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:Main.java

/**
 * Creates the Uri string with embedded expansion codes.
 *
 * @param uri to be encoded//  w w  w . j  a v a 2 s.co m
 * @return the Uri string with expansion codes.
 */
public static byte[] encodeUri(String uri) {
    if (uri == null || uri.length() == 0) {
        Log.i(TAG, "null or empty uri");
        return new byte[0];
    }
    ByteBuffer bb = ByteBuffer.allocate(uri.length());
    // UUIDs are ordered as byte array, which means most significant first
    bb.order(ByteOrder.BIG_ENDIAN);
    int position = 0;

    // Add the byte code for the scheme or return null if none
    Byte schemeCode = encodeUriScheme(uri);
    if (schemeCode == null) {
        Log.i(TAG, "null scheme code");
        return null;
    }
    String scheme = URI_SCHEMES.get(schemeCode);
    bb.put(schemeCode);
    position += scheme.length();

    if (URLUtil.isNetworkUrl(scheme)) {
        Log.i(TAG, "is network URL");
        return encodeUrl(uri, position, bb);
    } else if ("urn:uuid:".equals(scheme)) {
        Log.i(TAG, "is UUID");
        return encodeUrnUuid(uri, position, bb);
    }
    return null;
}

From source file:jsave.Utils.java

/**
 * Reads a Byte (-128 to 127).//from  ww w .j a va 2s  .  c o m
 *
 * @param raf the file where the 4 Bytes are read
 * @return a Byte
 */
private static short read_UnsignedByte(RandomAccessFile raf) {
    byte[] data = new byte[1];
    byte byteData = -1;
    try {
        raf.read(data);
        ByteBuffer bb = ByteBuffer.allocate(data.length);
        bb.put(data);
        return getUnsignedByte(bb);
    } catch (IOException e) {
    }

    return byteData;
}

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

/**
 * Creates a base 64 encoded String for unique event key.
 * //from   w  ww.j a  v a  2 s . co m
 * @param rowkey
 * @param qualifier
 * @return
 */
public static String getEncodedEventId(byte[] rowkey, byte[] qualifier) {
    ByteBuffer buffer = ByteBuffer.allocate(rowkey.length + qualifier.length);
    buffer.put(rowkey);
    buffer.put(qualifier);
    return DatatypeConverter.printBase64Binary(buffer.array());
}

From source file:io.mycat.util.ByteBufferUtil.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 w  w. j  a va 2  s .c o m
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:com.wandrell.example.swss.test.util.factory.SecureSoapMessages.java

/**
 * Generates the digest value for the SOAP secure header.
 * <p>//ww  w  .  j av a 2s.com
 * This is a codified password, with the help of the date and nonce values.
 * Both of these values should be found on the SOAP secure header.
 *
 * @param password
 *            password to digest
 * @param date
 *            date used on the SOAP header
 * @param nonce
 *            nonce used on the SOAP header
 * @return the digested password
 * @throws UnsupportedEncodingException
 *             if the UTF-8 encoding is not supported
 */
private static final String generateDigest(final String password, final String date, final String nonce)
        throws UnsupportedEncodingException {
    final ByteBuffer buf; // Buffers storing the data to digest
    byte[] toHash; // Bytes to generate the hash

    // Fills buffer with data to digest
    buf = ByteBuffer.allocate(1000);
    buf.put(Base64.decodeBase64(nonce));
    buf.put(date.getBytes("UTF-8"));
    buf.put(password.getBytes("UTF-8"));

    // Initializes hash bytes to the correct size
    toHash = new byte[buf.position()];
    buf.rewind();

    // Copies bytes from the buffer to the hash bytes
    buf.get(toHash);

    return Base64.encodeBase64String(DigestUtils.sha1(toHash));
}