Example usage for java.nio ByteOrder LITTLE_ENDIAN

List of usage examples for java.nio ByteOrder LITTLE_ENDIAN

Introduction

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

Prototype

ByteOrder LITTLE_ENDIAN

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

Click Source Link

Document

This constant represents little endian.

Usage

From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.dta.DTAFileReader.java

private void decodeExpansionFields(BufferedInputStream stream) throws IOException {

    dbgLog.fine("***** decodeExpansionFields(): start *****");

    if (stream == null) {
        throw new IllegalArgumentException("stream == null!");
    }/*from w  w w .ja  v a 2s  .  co  m*/

    // Added since release 105
    // [1-byte byte_field][short(2)/int(4)_field][variable_field whose
    // length is specified by the previous short/int field]

    int int_type_expansion_field = constantTable.get("EXPANSION");
    if (dbgLog.isLoggable(Level.FINE))
        dbgLog.fine("int_type_expansion_field=" + int_type_expansion_field);
    while (true) {
        byte[] firstByte = new byte[1];
        byte[] lengthBytes = new byte[int_type_expansion_field];

        int nbyte = stream.read(firstByte, 0, 1);
        dbgLog.fine("read 1st byte");
        int nbytes = stream.read(lengthBytes, 0, int_type_expansion_field);
        dbgLog.fine("read next integer");

        ByteBuffer bb_field_length = ByteBuffer.wrap(lengthBytes);

        if (isLittleEndian) {
            bb_field_length.order(ByteOrder.LITTLE_ENDIAN);
            dbgLog.fine("byte reversed");
        }

        int field_length;

        if (int_type_expansion_field == 2) {
            field_length = bb_field_length.getShort();
        } else {
            field_length = bb_field_length.getInt();
        }

        if (dbgLog.isLoggable(Level.FINE))
            dbgLog.fine("field_length=" + field_length);
        if (dbgLog.isLoggable(Level.FINE))
            dbgLog.fine("firstByte[0]=" + firstByte[0]);
        if ((field_length + (int) firstByte[0]) == 0) {
            // reached the end of this field
            break;
        } else {
            byte[] stringField = new byte[field_length];
            nbyte = stream.read(stringField, 0, field_length);

        }
    }

    dbgLog.fine("***** decodeExpansionFields(): end *****");

}

From source file:edu.hawaii.soest.kilonalu.dvp2.DavisWxParser.java

/**
 * get the value from the solarRadiation field
 *
 * @return solarRadiation - the solarRadiation as a float
 */// w  ww .j  a va  2 s. co m
public float getSolarRadiation() {
    this.solarRadiation.flip();
    return (float) (this.solarRadiation.order(ByteOrder.LITTLE_ENDIAN).getShort());
}

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

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

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

    if (notificationType == null) {
        notificationType = NotificationType.UNKNOWN;
    }//from   w ww . j  a v  a2 s  . com

    int icon_id = notificationType.icon;
    byte color_id = notificationType.color;

    // Calculate length first
    byte actions_count;
    short actions_length;
    String dismiss_string;
    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 = 2; // icon
    short attributes_length = (short) (11 + actions_length);
    if (parts != null) {
        for (String s : parts) {
            if (s == null || s.equals("")) {
                continue;
            }
            attributes_count++;
            attributes_length += (3 + s.getBytes().length);
        }
    }

    short pin_length = (short) (NOTIFICATION_PIN_LENGTH + attributes_length);

    ByteBuffer buf = ByteBuffer.allocate(pin_length);

    // pin - 46 bytes
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putLong(GB_UUID_MASK);
    buf.putLong(id);
    buf.putLong(UUID_NOTIFICATIONS.getMostSignificantBits());
    buf.putLong(UUID_NOTIFICATIONS.getLeastSignificantBits());
    buf.order(ByteOrder.LITTLE_ENDIAN);
    buf.putInt(timestamp); // 32-bit timestamp
    buf.putShort((short) 0); // duration
    buf.put((byte) 0x01); // type (0x01 = notification)
    buf.putShort((short) 0x0001); // flags 0x0001 = ?
    buf.put((byte) 0x04); // layout (0x04 = notification?)
    buf.putShort(attributes_length); // total length of all attributes and actions in bytes
    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 > 512)
                partlength = 512;
            buf.put(attribute_id);
            buf.putShort((short) partlength);
            buf.put(s.getBytes(), 0, partlength);
        }
    }

    buf.put((byte) 4); // icon
    buf.putShort((short) 4); // length of int
    buf.putInt(0x80000000 | icon_id);

    buf.put((byte) 28); // background_color
    buf.putShort((short) 1); // length of int
    buf.put(color_id);

    // dismiss action
    buf.put(dismiss_action_id);
    buf.put((byte) 0x02); // generic action, dismiss did not do anything
    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 action
        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 action
        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 encodeBlobdb(UUID.randomUUID(), BLOBDB_INSERT, BLOBDB_NOTIFICATION, buf.array());
}

From source file:edu.hawaii.soest.kilonalu.dvp2.DavisWxParser.java

/**
 * get the value from the stormRain field
 *
 * @return stormRain - the stormRain as a float
 *///from  w ww  .j a v a 2  s  . co  m
public float getStormRain() {
    this.stormRain.flip();
    return (float) (this.stormRain.order(ByteOrder.LITTLE_ENDIAN).getShort() / 100);
}

From source file:edu.hawaii.soest.kilonalu.dvp2.DavisWxParser.java

/**
 * get the value from the currentStormStartDate field
 *
 * @return currentStormStartDate - the currentStormStartDate as a String
 *//*from  w  ww. j a va2s . com*/
public String getCurrentStormStartDate() {
    this.currentStormStartDate.flip();
    int currentStormStartDate = (int) this.currentStormStartDate.order(ByteOrder.LITTLE_ENDIAN).getShort();

    int month = currentStormStartDate;
    month = (month >> 11); // clear bits 0-11
    String monthString = (new Integer(month)).toString();

    int day = currentStormStartDate;
    day = (day << 21) >> 27; // clear bits 16-32, then 0-11
    String dayString = (new Integer(day)).toString();

    int year = currentStormStartDate;
    year = (year << 25) >> 25; // clear bits 7-32
    int centuryYear = 2000 + year;
    String centuryYearString = (new Integer(centuryYear)).toString();
    if (monthString.equals("-1") || dayString.equals("-1")) {
        return "01-01-1999";
    } else {
        return (monthString + "-" + dayString + "-" + centuryYearString);
    }
}

From source file:edu.hawaii.soest.kilonalu.dvp2.DavisWxParser.java

/**
 * get the value from the dailyRain field
 *
 * @return dailyRain - the dailyRain as a float
 *///from  w  ww.  ja  va2  s  . c  o m
public float getDailyRain() {
    this.dailyRain.flip();
    return (float) (this.dailyRain.order(ByteOrder.LITTLE_ENDIAN).getShort() / 100);
}

From source file:edu.hawaii.soest.kilonalu.dvp2.DavisWxParser.java

/**
 * get the value from the monthlyRain field
 *
 * @return monthlyRain - the monthlyRain as a float
 *//*from w  w  w  . j a  v a 2  s  .com*/
public float getMonthlyRain() {
    this.monthlyRain.flip();
    return (float) (this.monthlyRain.order(ByteOrder.LITTLE_ENDIAN).getShort() / 100);
}

From source file:edu.hawaii.soest.kilonalu.dvp2.DavisWxParser.java

/**
 * get the value from the yearlyRain field
 *
 * @return yearlyRain - the yearlyRain as a float
 *//*from   w w  w .  j a  v  a  2s  .c  om*/
public float getYearlyRain() {
    this.yearlyRain.flip();
    return (float) (this.yearlyRain.order(ByteOrder.LITTLE_ENDIAN).getShort() / 100);
}

From source file:org.bimserver.geometry.StreamingGeometryGenerator.java

private ByteBuffer quantizeVertices(DoubleBuffer vertices, double[] quantizationMatrix, double multiplierToMm) {
    ByteBuffer quantizedBuffer = ByteBuffer.wrap(new byte[vertices.capacity() * 2]);
    quantizedBuffer.order(ByteOrder.LITTLE_ENDIAN);

    double[] vertex = new double[4];
    double[] result = new double[4];
    vertex[3] = 1;/*from w w w  .j  a v a  2s  .  c om*/
    int nrVertices = vertices.capacity();
    for (int i = 0; i < nrVertices; i += 3) {
        vertex[0] = vertices.get(i);
        vertex[1] = vertices.get(i + 1);
        vertex[2] = vertices.get(i + 2);

        if (multiplierToMm != 1f) {
            vertex[0] = vertex[0] * multiplierToMm;
            vertex[1] = vertex[1] * multiplierToMm;
            vertex[2] = vertex[2] * multiplierToMm;
        }

        Matrix.multiplyMV(result, 0, quantizationMatrix, 0, vertex, 0);

        quantizedBuffer.putShort((short) result[0]);
        quantizedBuffer.putShort((short) result[1]);
        quantizedBuffer.putShort((short) result[2]);
    }

    return quantizedBuffer;
}

From source file:edu.hawaii.soest.kilonalu.dvp2.DavisWxParser.java

/**
 * get the value from the dailyEvapoTranspiration field
 *
 * @return dailyEvapoTranspiration - the dailyEvapoTranspiration as a float
 *///w  ww . j  a  v  a  2s .c  o  m
public float getDailyEvapoTranspiration() {
    this.dailyEvapoTranspiration.flip();
    return (float) (this.dailyEvapoTranspiration.order(ByteOrder.LITTLE_ENDIAN).getShort() / 100);
}