Example usage for java.nio ByteBuffer order

List of usage examples for java.nio ByteBuffer order

Introduction

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

Prototype

Endianness order

To view the source code for java.nio ByteBuffer order.

Click Source Link

Document

The byte order of this buffer, default is BIG_ENDIAN .

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);//ww  w  .j a va 2s.  co  m
    buf.putShort(ENDPOINT_PUTBYTES);
    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);/* w  w w.  ja va 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);//from   w w  w .jav a2 s  .  c  om
    buf.putShort(ENDPOINT_SYSTEMMESSAGE);
    buf.put((byte) 0);
    buf.put(systemMessage);
    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;
            }/*from  w  w w.jav a  2  s  .c o m*/
            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);/*ww w  .  ja v  a  2 s .c o  m*/
    buf.putShort(ENDPOINT_BLOBDB);

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

    return buf.array();
}

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

byte[] encodeActivateWeather(boolean activate) {
    if (activate) {
        ByteBuffer buf = ByteBuffer.allocate(0x61);
        buf.put((byte) 1);
        buf.order(ByteOrder.BIG_ENDIAN);
        buf.putLong(UUID_LOCATION.getMostSignificantBits());
        buf.putLong(UUID_LOCATION.getLeastSignificantBits());
        // disable remaining 5 possible location
        buf.put(new byte[60 - LENGTH_UUID]);
        return encodeBlobdb("weatherApp", BLOBDB_INSERT, BLOBDB_APPSETTINGS, buf.array());
    } else {/*from  w  w w .  java  2s . c om*/
        return encodeBlobdb("weatherApp", BLOBDB_DELETE, BLOBDB_APPSETTINGS, null);
    }
}

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 .  c  o  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

@Override
public byte[] encodeAppStart(UUID uuid, boolean start) {
    if (mFwMajor >= 3) {
        final short LENGTH_APPRUNSTATE = 17;
        ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + LENGTH_APPRUNSTATE);
        buf.order(ByteOrder.BIG_ENDIAN);
        buf.putShort(LENGTH_APPRUNSTATE);
        buf.putShort(ENDPOINT_APPRUNSTATE);
        buf.put(start ? APPRUNSTATE_START : APPRUNSTATE_STOP);
        buf.putLong(uuid.getMostSignificantBits());
        buf.putLong(uuid.getLeastSignificantBits());
        return buf.array();
    } else {// w ww .j  a  v  a2  s  .  com
        ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>();
        int param = start ? 1 : 0;
        pairs.add(new Pair<>(1, (Object) param));
        return encodeApplicationMessagePush(ENDPOINT_LAUNCHER, uuid, pairs);
    }
}

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;/*ww w.  jav a 2s . c  o 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

@Override
public byte[] encodeSetMusicInfo(String artist, String album, String track, int duration, int trackCount,
        int trackNr) {
    String[] parts = { artist, album, track };
    if (duration == 0 || mFwMajor < 3) {
        return encodeMessage(ENDPOINT_MUSICCONTROL, MUSICCONTROL_SETMUSICINFO, 0, parts);
    } else {//w w  w .ja v  a2s .  co m
        // Calculate length first
        int length = LENGTH_PREFIX + 9;
        if (parts != null) {
            for (String s : parts) {
                if (s == null || s.equals("")) {
                    length++; // encode null or empty strings as 0x00 later
                    continue;
                }
                length += (1 + s.getBytes().length);
            }
        }

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

        // Encode Pascal-Style Strings
        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);
        }

        buf.order(ByteOrder.LITTLE_ENDIAN);
        buf.putInt(duration * 1000);
        buf.putShort((short) (trackCount & 0xffff));
        buf.putShort((short) (trackNr & 0xffff));

        return buf.array();
    }
}