Example usage for java.nio ByteOrder BIG_ENDIAN

List of usage examples for java.nio ByteOrder BIG_ENDIAN

Introduction

In this page you can find the example usage for java.nio ByteOrder BIG_ENDIAN.

Prototype

ByteOrder BIG_ENDIAN

To view the source code for java.nio ByteOrder BIG_ENDIAN.

Click Source Link

Document

This constant represents big endian.

Usage

From source file:com.healthmarketscience.jackcess.impl.ColumnImpl.java

/**
 * Writes a GUID value.//from w w w  . j a va 2 s.c  o m
 */
private static void writeGUIDValue(ByteBuffer buffer, Object value) throws IOException {
    Matcher m = GUID_PATTERN.matcher(toCharSequence(value));
    if (!m.matches()) {
        throw new IOException("Invalid GUID: " + value);
    }

    ByteBuffer origBuffer = null;
    byte[] tmpBuf = null;
    if (buffer.order() != ByteOrder.BIG_ENDIAN) {
        // write to a temp buf so we can do some swapping below
        origBuffer = buffer;
        tmpBuf = new byte[16];
        buffer = ByteBuffer.wrap(tmpBuf);
    }

    ByteUtil.writeHexString(buffer, m.group(1));
    ByteUtil.writeHexString(buffer, m.group(2));
    ByteUtil.writeHexString(buffer, m.group(3));
    ByteUtil.writeHexString(buffer, m.group(4));
    ByteUtil.writeHexString(buffer, m.group(5));

    if (tmpBuf != null) {
        // the first 3 guid components are integer components which need to
        // respect endianness, so swap 4-byte int, 2-byte int, 2-byte int
        ByteUtil.swap4Bytes(tmpBuf, 0);
        ByteUtil.swap2Bytes(tmpBuf, 4);
        ByteUtil.swap2Bytes(tmpBuf, 6);
        origBuffer.put(tmpBuf);
    }
}

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

private byte[] encodeExtensibleNotification(int id, int timestamp, String title, String subtitle, String body,
        String sourceName, boolean hasHandle, String[] cannedReplies) {
    final short ACTION_LENGTH_MIN = 10;

    String[] parts = { title, subtitle, body };

    // Calculate length first
    byte actions_count;
    short actions_length;
    String dismiss_string;//  w w  w .ja v a  2s  .c  o m
    String open_string = "Open on phone";
    String mute_string = "Mute";
    String reply_string = "Reply";
    if (sourceName != null) {
        mute_string += " " + sourceName;
    }

    byte dismiss_action_id;

    if (hasHandle && !"ALARMCLOCKRECEIVER".equals(sourceName)) {
        actions_count = 3;
        dismiss_string = "Dismiss";
        dismiss_action_id = 0x02;
        actions_length = (short) (ACTION_LENGTH_MIN * actions_count + dismiss_string.getBytes().length
                + open_string.getBytes().length + mute_string.getBytes().length);
    } else {
        actions_count = 1;
        dismiss_string = "Dismiss all";
        dismiss_action_id = 0x03;
        actions_length = (short) (ACTION_LENGTH_MIN * actions_count + dismiss_string.getBytes().length);
    }

    int replies_length = -1;
    if (cannedReplies != null && cannedReplies.length > 0) {
        actions_count++;
        for (String reply : cannedReplies) {
            replies_length += reply.getBytes().length + 1;
        }
        actions_length += ACTION_LENGTH_MIN + reply_string.getBytes().length + replies_length + 3; // 3 = attribute id (byte) + length(short)
    }

    byte attributes_count = 0;

    int length = 21 + 10 + actions_length;
    if (parts != null) {
        for (String s : parts) {
            if (s == null || s.equals("")) {
                continue;
            }
            attributes_count++;
            length += (3 + s.getBytes().length);
        }
    }

    // Encode Prefix
    ByteBuffer buf = ByteBuffer.allocate(length + LENGTH_PREFIX);

    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort((short) (length));
    buf.putShort(ENDPOINT_EXTENSIBLENOTIFS);

    buf.order(ByteOrder.LITTLE_ENDIAN); // !

    buf.put((byte) 0x00); // ?
    buf.put((byte) 0x01); // add notifications
    buf.putInt(0x00000000); // flags - ?
    buf.putInt(id);
    buf.putInt(0x00000000); // ANCS id
    buf.putInt(timestamp);
    buf.put((byte) 0x01); // layout - ?
    buf.put(attributes_count);
    buf.put(actions_count);

    byte attribute_id = 0;
    // Encode Pascal-Style Strings
    if (parts != null) {
        for (String s : parts) {
            attribute_id++;
            if (s == null || s.equals("")) {
                continue;
            }

            int partlength = s.getBytes().length;
            if (partlength > 255)
                partlength = 255;
            buf.put(attribute_id);
            buf.putShort((short) partlength);
            buf.put(s.getBytes(), 0, partlength);
        }
    }

    // dismiss action
    buf.put(dismiss_action_id);
    buf.put((byte) 0x04); // dismiss
    buf.put((byte) 0x01); // number attributes
    buf.put((byte) 0x01); // attribute id (title)
    buf.putShort((short) dismiss_string.getBytes().length);
    buf.put(dismiss_string.getBytes());

    // open and mute actions
    if (hasHandle && !"ALARMCLOCKRECEIVER".equals(sourceName)) {
        buf.put((byte) 0x01);
        buf.put((byte) 0x02); // generic
        buf.put((byte) 0x01); // number attributes
        buf.put((byte) 0x01); // attribute id (title)
        buf.putShort((short) open_string.getBytes().length);
        buf.put(open_string.getBytes());

        buf.put((byte) 0x04);
        buf.put((byte) 0x02); // generic
        buf.put((byte) 0x01); // number attributes
        buf.put((byte) 0x01); // attribute id (title)
        buf.putShort((short) mute_string.getBytes().length);
        buf.put(mute_string.getBytes());

    }

    if (cannedReplies != null && replies_length > 0) {
        buf.put((byte) 0x05);
        buf.put((byte) 0x03); // reply action
        buf.put((byte) 0x02); // number attributes
        buf.put((byte) 0x01); // title
        buf.putShort((short) reply_string.getBytes().length);
        buf.put(reply_string.getBytes());
        buf.put((byte) 0x08); // canned replies
        buf.putShort((short) replies_length);
        for (int i = 0; i < cannedReplies.length - 1; i++) {
            buf.put(cannedReplies[i].getBytes());
            buf.put((byte) 0x00);
        }
        // last one must not be zero terminated, else we get an additional emply reply
        buf.put(cannedReplies[cannedReplies.length - 1].getBytes());
    }

    return buf.array();
}

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

/**
 * Decodes a NUMERIC field.//  w  ww.j av  a  2  s  .  c  o  m
 */
private BigDecimal readNumericValue(ByteBuffer buffer) {
    boolean negate = (buffer.get() != 0);

    byte[] tmpArr = new byte[16];
    buffer.get(tmpArr);

    if (buffer.order() != ByteOrder.BIG_ENDIAN) {
        fixNumericByteOrder(tmpArr);
    }

    BigInteger intVal = new BigInteger(tmpArr);
    if (negate) {
        intVal = intVal.negate();
    }
    return new BigDecimal(intVal, getScale());
}

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

/**
 * Writes a numeric value.//from  w ww  . j  av a  2 s  .c  o  m
 */
private void writeNumericValue(ByteBuffer buffer, Object value) throws IOException {
    Object inValue = value;
    try {
        BigDecimal decVal = toBigDecimal(value);
        inValue = decVal;

        boolean negative = (decVal.compareTo(BigDecimal.ZERO) < 0);
        if (negative) {
            decVal = decVal.negate();
        }

        // write sign byte
        buffer.put(negative ? (byte) 0x80 : (byte) 0);

        // adjust scale according to this column type (will cause the an
        // ArithmeticException if number has too many decimal places)
        decVal = decVal.setScale(getScale());

        // check precision
        if (decVal.precision() > getPrecision()) {
            throw new IOException(
                    "Numeric value is too big for specified precision " + getPrecision() + ": " + decVal);
        }

        // convert to unscaled BigInteger, big-endian bytes
        byte[] intValBytes = decVal.unscaledValue().toByteArray();
        int maxByteLen = getType().getFixedSize() - 1;
        if (intValBytes.length > maxByteLen) {
            throw new IOException("Too many bytes for valid BigInteger?");
        }
        if (intValBytes.length < maxByteLen) {
            byte[] tmpBytes = new byte[maxByteLen];
            System.arraycopy(intValBytes, 0, tmpBytes, (maxByteLen - intValBytes.length), intValBytes.length);
            intValBytes = tmpBytes;
        }
        if (buffer.order() != ByteOrder.BIG_ENDIAN) {
            fixNumericByteOrder(intValBytes);
        }
        buffer.put(intValBytes);
    } catch (ArithmeticException e) {
        throw (IOException) new IOException("Numeric value '" + inValue + "' out of range").initCause(e);
    }
}

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

private byte[] encodeBlobdb(Object key, byte command, byte db, byte[] blob) {

    int length = 5;

    int key_length;
    if (key instanceof UUID) {
        key_length = LENGTH_UUID;
    } else if (key instanceof String) {
        key_length = ((String) key).getBytes().length;
    } else {/*from   www  .  j  a  va  2  s  . c o m*/
        LOG.warn("unknown key type");
        return null;
    }
    if (key_length > 255) {
        LOG.warn("key is too long");
        return null;
    }
    length += key_length;

    if (blob != null) {
        length += blob.length + 2;
    }

    ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + length);

    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort((short) length);
    buf.putShort(ENDPOINT_BLOBDB);

    buf.order(ByteOrder.LITTLE_ENDIAN);
    buf.put(command);
    buf.putShort((short) mRandom.nextInt()); // token
    buf.put(db);

    buf.put((byte) key_length);
    if (key instanceof UUID) {
        UUID uuid = (UUID) key;
        buf.order(ByteOrder.BIG_ENDIAN);
        buf.putLong(uuid.getMostSignificantBits());
        buf.putLong(uuid.getLeastSignificantBits());
        buf.order(ByteOrder.LITTLE_ENDIAN);
    } else {
        buf.put(((String) key).getBytes());
    }

    if (blob != null) {
        buf.putShort((short) blob.length);
        buf.put(blob);
    }

    return buf.array();
}

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

/**
 * Decodes a GUID value.//from  ww w  . ja v a2  s . co  m
 */
private static String readGUIDValue(ByteBuffer buffer, ByteOrder order) {
    if (order != ByteOrder.BIG_ENDIAN) {
        byte[] tmpArr = new byte[16];
        buffer.get(tmpArr);

        // the first 3 guid components are integer components which need to
        // respect endianness, so swap 4-byte int, 2-byte int, 2-byte int
        ByteUtil.swap4Bytes(tmpArr, 0);
        ByteUtil.swap2Bytes(tmpArr, 4);
        ByteUtil.swap2Bytes(tmpArr, 6);
        buffer = ByteBuffer.wrap(tmpArr);
    }

    StringBuilder sb = new StringBuilder(22);
    sb.append("{");
    sb.append(ByteUtil.toHexString(buffer, 0, 4, false));
    sb.append("-");
    sb.append(ByteUtil.toHexString(buffer, 4, 2, false));
    sb.append("-");
    sb.append(ByteUtil.toHexString(buffer, 6, 2, false));
    sb.append("-");
    sb.append(ByteUtil.toHexString(buffer, 8, 2, false));
    sb.append("-");
    sb.append(ByteUtil.toHexString(buffer, 10, 6, false));
    sb.append("}");
    return (sb.toString());
}

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  .  jav  a  2s. c o m
        return encodeBlobdb("weatherApp", BLOBDB_DELETE, BLOBDB_APPSETTINGS, null);
    }
}

From source file:au.org.ala.layers.intersect.Grid.java

/**
 * for DomainGenerator//  w w  w  . j a  v a  2  s  .  c o  m
 * <p/>
 * writes out a list of double (same as getGrid() returns) to a file
 * <p/>
 * byteorderlsb
 * data type, FLOAT
 *
 * @param newfilename
 * @param dfiltered
 */
public void writeGrid(String newfilename, int[] dfiltered, double xmin, double ymin, double xmax, double ymax,
        double xres, double yres, int nrows, int ncols) {
    int size, i, length = dfiltered.length;
    double maxvalue = Integer.MAX_VALUE * -1;
    double minvalue = Integer.MAX_VALUE;

    //write data as whole file
    RandomAccessFile afile = null;
    try { //read of random access file can throw an exception
        afile = new RandomAccessFile(newfilename + ".gri", "rw");

        size = 4;
        byte[] b = new byte[size * length];
        ByteBuffer bb = ByteBuffer.wrap(b);

        if (byteorderLSB) {
            bb.order(ByteOrder.LITTLE_ENDIAN);
        } else {
            bb.order(ByteOrder.BIG_ENDIAN);
        }
        for (i = 0; i < length; i++) {
            bb.putInt(dfiltered[i]);
        }

        afile.write(b);
    } catch (Exception e) {
        logger.error("error writing grid file", e);
    } finally {
        if (afile != null) {
            try {
                afile.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    writeHeader(newfilename, xmin, ymin, xmin + xres * ncols, ymin + yres * nrows, xres, yres, nrows, ncols,
            minvalue, maxvalue, "INT4BYTES", "-9999");

}

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

/**
 * Writes a GUID value.//from   w w w.  ja  v  a2 s . c  o m
 */
private static void writeGUIDValue(ByteBuffer buffer, Object value, ByteOrder order) throws IOException {
    Matcher m = GUID_PATTERN.matcher(toCharSequence(value));
    if (m.matches()) {
        ByteBuffer origBuffer = null;
        byte[] tmpBuf = null;
        if (order != ByteOrder.BIG_ENDIAN) {
            // write to a temp buf so we can do some swapping below
            origBuffer = buffer;
            tmpBuf = new byte[16];
            buffer = ByteBuffer.wrap(tmpBuf);
        }

        ByteUtil.writeHexString(buffer, m.group(1));
        ByteUtil.writeHexString(buffer, m.group(2));
        ByteUtil.writeHexString(buffer, m.group(3));
        ByteUtil.writeHexString(buffer, m.group(4));
        ByteUtil.writeHexString(buffer, m.group(5));

        if (tmpBuf != null) {
            // the first 3 guid components are integer components which need to
            // respect endianness, so swap 4-byte int, 2-byte int, 2-byte int
            ByteUtil.swap4Bytes(tmpBuf, 0);
            ByteUtil.swap2Bytes(tmpBuf, 4);
            ByteUtil.swap2Bytes(tmpBuf, 6);
            origBuffer.put(tmpBuf);
        }

    } else {
        throw new IOException("Invalid GUID: " + value);
    }
}

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);//from   www.ja  va2s  . 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();
}