Example usage for java.nio ByteBuffer putShort

List of usage examples for java.nio ByteBuffer putShort

Introduction

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

Prototype

public abstract ByteBuffer putShort(short value);

Source Link

Document

Writes the given short to the current position and increases the position by 2.

Usage

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java

byte[] encodeUploadCommit(int token, int crc) {
    final short LENGTH_UPLOADCOMMIT = 9;
    ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + LENGTH_UPLOADCOMMIT);
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort(LENGTH_UPLOADCOMMIT);
    buf.putShort(ENDPOINT_PUTBYTES);/*from w w w. java2s . c  o m*/
    buf.put(PUTBYTES_COMMIT);
    buf.putInt(token);
    buf.putInt(crc);
    return buf.array();
}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java

byte[] encodeUploadComplete(int token) {
    final short LENGTH_UPLOADCOMPLETE = 5;
    ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + LENGTH_UPLOADCOMPLETE);
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort(LENGTH_UPLOADCOMPLETE);
    buf.putShort(ENDPOINT_PUTBYTES);/*from  ww  w. j  a v a  2  s.com*/
    buf.put(PUTBYTES_COMPLETE);
    buf.putInt(token);
    return buf.array();
}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java

private byte[] encodeSystemMessage(byte systemMessage) {
    final short LENGTH_SYSTEMMESSAGE = 2;
    ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + LENGTH_SYSTEMMESSAGE);
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort(LENGTH_SYSTEMMESSAGE);
    buf.putShort(ENDPOINT_SYSTEMMESSAGE);
    buf.put((byte) 0);
    buf.put(systemMessage);/*from   ww w.j  av a2 s.  c  o  m*/
    return buf.array();

}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java

private byte[] encodeMessage(short endpoint, byte type, int cookie, String[] parts) {
    // Calculate length first
    int length = LENGTH_PREFIX + 1;
    if (parts != null) {
        for (String s : parts) {
            if (s == null || s.equals("")) {
                length++; // encode null or empty strings as 0x00 later
                continue;
            }// www  .ja  v a 2s.c  om
            length += (1 + s.getBytes().length);
        }
    }
    if (endpoint == ENDPOINT_PHONECONTROL) {
        length += 4; //for cookie;
    }

    // Encode Prefix
    ByteBuffer buf = ByteBuffer.allocate(length);
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort((short) (length - LENGTH_PREFIX));
    buf.putShort(endpoint);
    buf.put(type);

    if (endpoint == ENDPOINT_PHONECONTROL) {
        buf.putInt(cookie);
    }
    // Encode Pascal-Style Strings
    if (parts != null) {
        for (String s : parts) {
            if (s == null || s.equals("")) {
                buf.put((byte) 0x00);
                continue;
            }

            int partlength = s.getBytes().length;
            if (partlength > 255)
                partlength = 255;
            buf.put((byte) partlength);
            buf.put(s.getBytes(), 0, partlength);
        }
    }
    return buf.array();
}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java

private byte[] encodeBlobDBClear(byte database) {
    final short LENGTH_BLOBDB_CLEAR = 4;
    ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + LENGTH_BLOBDB_CLEAR);

    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort(LENGTH_BLOBDB_CLEAR);
    buf.putShort(ENDPOINT_BLOBDB);/*w  w  w. j a  v  a 2  s . c o m*/

    buf.order(ByteOrder.LITTLE_ENDIAN);
    buf.put(BLOBDB_CLEAR);
    buf.putShort((short) mRandom.nextInt()); // token
    buf.put(database);

    return buf.array();
}

From source file:com.healthmarketscience.jackcess.Column.java

/**
 * Serialize an Object into a raw byte value for this column
 * @param obj Object to serialize/*from w ww.  java  2  s .  co m*/
 * @param order Order in which to serialize
 * @return A buffer containing the bytes
 * @usage _advanced_method_
 */
public ByteBuffer writeFixedLengthField(Object obj, ByteOrder order) throws IOException {
    int size = getType().getFixedSize(_columnLength);

    // create buffer for data
    ByteBuffer buffer = getPageChannel().createBuffer(size, order);

    // since booleans are not written by this method, it's safe to convert any
    // incoming boolean into an integer.
    obj = booleanToInteger(obj);

    switch (getType()) {
    case BOOLEAN:
        //Do nothing
        break;
    case BYTE:
        buffer.put(toNumber(obj).byteValue());
        break;
    case INT:
        buffer.putShort(toNumber(obj).shortValue());
        break;
    case LONG:
        buffer.putInt(toNumber(obj).intValue());
        break;
    case MONEY:
        writeCurrencyValue(buffer, obj);
        break;
    case FLOAT:
        buffer.putFloat(toNumber(obj).floatValue());
        break;
    case DOUBLE:
        buffer.putDouble(toNumber(obj).doubleValue());
        break;
    case SHORT_DATE_TIME:
        writeDateValue(buffer, obj);
        break;
    case TEXT:
        // apparently text numeric values are also occasionally written as fixed
        // length...
        int numChars = getLengthInUnits();
        // force uncompressed encoding for fixed length text
        buffer.put(encodeTextValue(obj, numChars, numChars, true));
        break;
    case GUID:
        writeGUIDValue(buffer, obj, order);
        break;
    case NUMERIC:
        // yes, that's right, occasionally numeric values are written as fixed
        // length...
        writeNumericValue(buffer, obj);
        break;
    case BINARY:
    case UNKNOWN_0D:
    case UNKNOWN_11:
    case COMPLEX_TYPE:
        buffer.putInt(toNumber(obj).intValue());
        break;
    case UNSUPPORTED_FIXEDLEN:
        byte[] bytes = toByteArray(obj);
        if (bytes.length != getLength()) {
            throw new IOException(
                    "Invalid fixed size binary data, size " + getLength() + ", got " + bytes.length);
        }
        buffer.put(bytes);
        break;
    default:
        throw new IOException("Unsupported data type: " + getType());
    }
    buffer.flip();
    return buffer;
}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java

byte[] encodeUploadChunk(int token, byte[] buffer, int size) {
    final short LENGTH_UPLOADCHUNK = 9;
    ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + LENGTH_UPLOADCHUNK + size);
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort((short) (LENGTH_UPLOADCHUNK + size));
    buf.putShort(ENDPOINT_PUTBYTES);//from   ww  w. j  a v  a 2s.  co m
    buf.put(PUTBYTES_SEND);
    buf.putInt(token);
    buf.putInt(size);
    buf.put(buffer, 0, size);
    return buf.array();
}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java

byte[] encodeUploadStart(byte type, int app_id, int size, String filename) {
    short length;
    if (mFwMajor >= 3 && (type != PUTBYTES_TYPE_FILE)) {
        length = (short) 10;
        type |= 0b10000000;//from   w w w .  j  a va 2  s  . co m
    } else {
        length = (short) 7;
    }

    if (type == PUTBYTES_TYPE_FILE && filename != null) {
        length += filename.getBytes().length + 1;
    }

    ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + length);
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort(length);
    buf.putShort(ENDPOINT_PUTBYTES);
    buf.put(PUTBYTES_INIT);
    buf.putInt(size);
    buf.put(type);

    if (mFwMajor >= 3 && (type != PUTBYTES_TYPE_FILE)) {
        buf.putInt(app_id);
    } else {
        // slot
        buf.put((byte) app_id);
    }

    if (type == PUTBYTES_TYPE_FILE && filename != null) {
        buf.put(filename.getBytes());
        buf.put((byte) 0);
    }

    return buf.array();
}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java

private byte[] encodePhoneVersion2x(byte os) {
    final short LENGTH_PHONEVERSION = 17;
    ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + LENGTH_PHONEVERSION);
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort(LENGTH_PHONEVERSION);
    buf.putShort(ENDPOINT_PHONEVERSION);
    buf.put((byte) 0x01);
    buf.putInt(-1); //0xffffffff

    if (os == PHONEVERSION_REMOTE_OS_ANDROID) {
        buf.putInt(PHONEVERSION_SESSION_CAPS_GAMMARAY);
    } else {/*  w w  w .j av a2 s.co  m*/
        buf.putInt(0);
    }
    buf.putInt(PHONEVERSION_REMOTE_CAPS_SMS | PHONEVERSION_REMOTE_CAPS_TELEPHONY | os);

    buf.put(PHONEVERSION_APPVERSION_MAGIC);
    buf.put(PHONEVERSION_APPVERSION_MAJOR);
    buf.put(PHONEVERSION_APPVERSION_MINOR);
    buf.put(PHONEVERSION_APPVERSION_PATCH);

    return buf.array();
}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java

private byte[] encodePhoneVersion3x(byte os) {
    final short LENGTH_PHONEVERSION3X = 25;
    ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + LENGTH_PHONEVERSION3X);
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort(LENGTH_PHONEVERSION3X);
    buf.putShort(ENDPOINT_PHONEVERSION);
    buf.put((byte) 0x01);
    buf.putInt(-1); //0xffffffff
    buf.putInt(0);// w  w w  . j a v  a  2 s  .  com

    buf.putInt(os);

    buf.put(PHONEVERSION_APPVERSION_MAGIC);
    buf.put((byte) 4); // major
    buf.put((byte) 1); // minor
    buf.put((byte) 1); // patch
    buf.order(ByteOrder.LITTLE_ENDIAN);
    buf.putLong(0x00000000000029af); //flags

    return buf.array();
}