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:org.energy_home.jemma.osgi.ah.io.flexgateway.FlexGatewayButtons.java

public void run() {
    long tv_sec;//from  www .  j  a  va 2s.c  o  m
    long tv_usec;
    short type;
    short code;
    int value;
    while (true) {
        buttonFile = new File(flexGatewayButtonsDevice);
        ByteBuffer buffer = ByteBuffer.allocate(100);
        buffer.order(ByteOrder.nativeOrder());
        try {
            fis = new FileInputStream(buttonFile);
            channel = fis.getChannel();
            while (true) {
                buffer.clear();
                int size = channel.read(buffer);
                buffer.rewind();

                while (size > 0) {
                    tv_sec = buffer.getInt();
                    tv_usec = buffer.getInt();
                    long tv = tv_sec * 1000000 + tv_usec;
                    type = buffer.getShort();
                    code = buffer.getShort();
                    value = buffer.getInt();
                    size -= 16;

                    if (type == 0 && code == 0 && value == 0)
                        continue;

                    // Code 3 -> front button
                    // Code 2 -> back button
                    // Value > 0 -> button pressed
                    // Value > 0 -> button released

                    // send event
                    log.debug("Button: ms " + tv + " type " + (type & 0xffff) + " code " + (code & 0xffff)
                            + " value " + (value & 0xffffffff));

                    Hashtable props = new Hashtable();
                    props.put("timestamp", new Long(tv));
                    if (value > 0)
                        this.postEvent("button/" + code + "/UP", props);
                    else
                        this.postEvent("button/" + code + "/DOWN", props);
                }
            }
        } catch (ClosedByInterruptException e) {
            break;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            log.error("exception", e);
            break;
        } finally {
            try {
                if (channel != null)
                    channel.close();
            } catch (IOException e) {
                log.error("exception", e);
                break;
            }
        }
    }
    log.debug("exiting");
}

From source file:edu.udo.scaffoldhunter.model.db.StringProperty.java

/**
 * Set the length in the BitFingerprint (first two bytes)
 * //from   ww  w  .j  a  v  a 2 s  . c o m
 * @param length
 *            the length
 * @param bitFingerprint
 *            the fingerprint
 */
private void lenghtToBitFingerprint(short length, byte[] bitFingerprint) {
    ByteBuffer bb = ByteBuffer.allocate(2);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.putShort(length);

    bitFingerprint[0] = bb.get(0);
    bitFingerprint[1] = bb.get(1);
}

From source file:edu.udo.scaffoldhunter.model.db.StringProperty.java

/**
 * Extracts the BitFingerprint length from the first two bytes
 * //  w  ww .j a v a 2s  . co  m
 * @param bitFingerprint
 *            the bytes
 * @return the length
 */
private short bitFingerprintToLength(byte[] bitFingerprint) {
    Preconditions.checkArgument(bitFingerprint.length >= 3);

    ByteBuffer bb = ByteBuffer.allocate(2);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.put(bitFingerprint[0]);
    bb.put(bitFingerprint[1]);
    short length = bb.getShort(0);

    return length;
}

From source file:com.woodcomputing.bobbin.format.PESFormat.java

@Override
public Design load(File file) {

    byte[] bytes = null;
    try (InputStream is = new FileInputStream(file)) {
        bytes = IOUtils.toByteArray(is);
    } catch (IOException ex) {
        log.catching(ex);/*  w  w  w .  ja  va2 s .c  o m*/
    }
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    Design design = new Design();
    log.debug("Magic: {}{}{}{}", (char) bb.get(), (char) bb.get(), (char) bb.get(), (char) bb.get());
    int pecStart = bb.getInt(8);
    log.debug("PEC Start: {}", pecStart);
    byte colorCount = bb.get(pecStart + 48);
    log.debug("Color Count: {}", colorCount);
    int colors[] = new int[colorCount];
    for (int i = 0; i < colorCount; i++) {
        colors[i] = bb.get() & 0xFF;
        log.debug("Color[{}] = {}", i, colors[i]);
    }

    bb.position(pecStart + 532);
    int x;
    int y;
    int colorChanges = 0;
    PESColor color = pesColorMap.get(colors[colorChanges++]);
    StitchGroup stitchGroup = new StitchGroup();
    stitchGroup.setColor(color.getColor());

    while (true) {
        x = bb.get() & 0xFF;
        y = bb.get() & 0xFF;
        if (x == 0xFF && y == 0x00) {
            log.debug("End of stitches");
            break;
        }
        if (x == 0xFE && y == 0xB0) {
            int colorIndex = bb.get() & 0xFF;
            log.debug("Color change: {}", colorIndex);
            color = pesColorMap.get(colors[colorChanges++]);
            stitchGroup = new StitchGroup();
            stitchGroup.setColor(color.getColor());
            continue;
        }
        if ((x & 0x80) > 0) {
            log.debug("Testing X: {} -  X & 0x80: {}", x, x & 0x80);
            if ((x & 0x20) > 0) {
                log.debug("Stich type TRIM");
            }
            if ((x & 0x10) > 0) {
                log.debug("Stich type JUMP");
            }
            x = ((x & 0x0F) << 8) + y;

            if ((x & 0x800) > 0) {
                x -= 0x1000;
            }
            y = bb.get() & 0xFF;

        } else if (x >= 0x40) {
            x -= 0x80;
        }
        if ((y & 0x80) > 0) {
            log.debug("Testing Y: {} -  Y & 0x80: {}", y, y & 0x80);
            if ((y & 0x20) > 0) {
                log.debug("Stich type TRIM");
            }
            if ((y & 0x10) > 0) {
                log.debug("Stich type JUMP");
            }
            y = ((y & 0x0F) << 8) + bb.get() & 0xFF;

            if ((y & 0x800) > 0) {
                y -= 0x1000;
            }
        } else if (y >= 0x40) {
            y -= 0x80;
        }
        //            Stitch designStitch = new Stitch(cx, -cy, nx, -ny);
        //            stitchGroup.getStitches().add(designStitch);
        log.debug("X: {} Y: {}", x, y);
    }
    log.debug("Color Changes: {}", colorChanges);
    return design;
}

From source file:com.woodcomputing.bobbin.format.JEFFormat.java

@Override
public Design load(File file) {

    byte[] bytes = null;
    try (InputStream is = new FileInputStream(file)) {
        bytes = IOUtils.toByteArray(is);
    } catch (IOException ex) {
        log.catching(ex);/*from  w ww. jav  a2  s.c  o  m*/
    }
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    JEF jef = new JEF();
    jef.setFirstStitchLocation(bb.getInt(0));
    log.debug("First Stitch Location: {}", jef.getFirstStitchLocation());
    jef.setThreadChangeCount(bb.getInt(24));
    log.debug("Threads Changes: {}", jef.getThreadChangeCount());
    jef.setHoop(Hoop.id2Hoop(bb.getInt(32)));
    log.debug("Hoop: {}", jef.getHoop());
    jef.setStitchCount(bb.getInt(28));
    log.debug("Stitch Count: {}", jef.getStitchCount());

    bb.position(116);
    JEFColor[] colors = new JEFColor[jef.getThreadChangeCount()];
    for (int i = 0; i < jef.getThreadChangeCount(); i++) {
        colors[i] = jefColorMap.get(bb.getInt());
    }
    jef.setThreadColors(colors);
    for (int i = 0; i < jef.getThreadChangeCount(); i++) {
        log.debug("ThreadType{}: {}", i, bb.getInt());
    }

    int dx = 0;
    int dy = 0;
    int cx = 0;
    int cy = 0;
    int nx = 0;
    int ny = 0;

    int change = 1;
    int stitches = 0;
    boolean isMove = false;
    bb.position(jef.getFirstStitchLocation());
    JEFColor color = jef.getThreadColors()[change - 1];
    Design design = new Design();
    StitchGroup stitchGroup = new StitchGroup();
    stitchGroup.setColor(color.getRgb());

    for (int stitch = 1; stitch < jef.getStitchCount(); stitch++) {
        dx = bb.get();
        dy = bb.get();
        if (dx == -128) {
            switch (dy) {
            case 1:
                log.debug("change: {}", bb.position());
                change++;
                color = jef.getThreadColors()[change - 1];
                design.getStitchGroups().add(stitchGroup);
                stitchGroup = new StitchGroup();
                stitchGroup.setColor(color.getRgb());
                //                        bb.get();
                //                        bb.get();
                continue;
            case 2:
                //                        log.debug("move");
                isMove = true;
                break;
            case 16:
                log.debug("last");
                isMove = true;
                break;
            }
        } else {
            nx = cx + dx;
            ny = cy + dy;
            if (isMove) {
                isMove = false;
            }
            //                } else {
            //                    log.debug("stitch");
            stitches++;
            Stitch designStitch = new Stitch(cx, -cy, nx, -ny);
            stitchGroup.getStitches().add(designStitch);
            //                }
            cx = nx;
            cy = ny;
        }
    }
    log.debug("Changes: {} Stitches {} End: {}", change, stitches, bb.position());
    return design;
}

From source file:io.warp10.quasar.encoder.QuasarTokenEncoder.java

private ByteBuffer toByteBuffer(String strUUID) {
    ByteBuffer buffer = ByteBuffer.allocate(16);
    buffer.order(ByteOrder.BIG_ENDIAN);

    UUID uuid = UUID.fromString(strUUID);

    buffer.putLong(uuid.getMostSignificantBits());
    buffer.putLong(uuid.getLeastSignificantBits());

    buffer.position(0);/*www. j  a  va 2s .co m*/
    return buffer;
}

From source file:io.warp10.quasar.encoder.QuasarTokenEncoder.java

public String getTokenIdent(String token, byte[] tokenSipHashkey) {
    long ident = SipHashInline.hash24_palindromic(tokenSipHashkey, token.getBytes());

    ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
    buffer.order(ByteOrder.BIG_ENDIAN);
    buffer.putLong(ident);/*w  w w .  ja  va2s .c  o m*/
    return Hex.encodeHexString(buffer.array());
}

From source file:com.github.cambierr.lorawanpacket.semtech.PushData.java

public PushData(byte[] _randoms, ByteBuffer _raw) throws MalformedPacketException {
    super(_randoms, PacketType.PUSH_DATA);
    _raw.order(ByteOrder.LITTLE_ENDIAN);

    if (_raw.remaining() < 8) {
        throw new MalformedPacketException("too short");
    }/*from  w w w  .ja  v  a2 s .  c o m*/

    gatewayEui = new byte[8];
    _raw.get(gatewayEui);

    byte[] json = new byte[_raw.remaining()];
    _raw.get(json);
    JSONObject jo;

    try {
        jo = new JSONObject(new String(json));
    } catch (JSONException ex) {
        throw new MalformedPacketException("malformed json");
    }

    stats = new ArrayList<>();
    rxpks = new ArrayList<>();

    if (jo.has("rxpk")) {
        if (!jo.get("rxpk").getClass().equals(JSONArray.class)) {
            throw new MalformedPacketException("malformed json (rxpk)");
        }
        JSONArray rxpk = jo.getJSONArray("rxpk");

        for (int i = 0; i < rxpk.length(); i++) {
            rxpks.add(new Rxpk(rxpk.getJSONObject(i)));
        }
    }

    if (jo.has("stat")) {
        if (!jo.get("stat").getClass().equals(JSONArray.class)) {
            throw new MalformedPacketException("malformed json (stat)");
        }
        JSONArray stat = jo.getJSONArray("stat");

        for (int i = 0; i < stat.length(); i++) {
            stats.add(new Stat(stat.getJSONObject(i)));
        }
    }

}

From source file:nl.dobots.bluenet.ble.base.structs.CrownstoneServiceData.java

public CrownstoneServiceData(byte[] bytes) {
    super();/*  w ww .  ja v a  2  s.  c o  m*/

    ByteBuffer bb = ByteBuffer.wrap(bytes);
    bb.order(ByteOrder.LITTLE_ENDIAN);

    bb.getShort(); // skip first two bytes (service UUID)

    bb.mark();
    int val = bb.get();

    if (val == 1) {
        setCrownstoneId(bb.getShort());
        setCrownstoneStateId(bb.getShort());
        setSwitchState((bb.get() & 0xff));
        setEventBitmask(bb.get());
        setTemperature(bb.get());
        setPowerUsage(bb.getInt());
        setAccumulatedEnergy(bb.getInt());

        setRelayState(BleUtils.isBitSet(getSwitchState(), 7));
        setPwm(getSwitchState() & ~(1 << 7));
    } else {
        bb.reset();
        setCrownstoneId(bb.getShort());
        setCrownstoneStateId(bb.getShort());
        setSwitchState((bb.get() & 0xff));
        setEventBitmask(bb.get());
        setTemperature(bb.get());
        bb.get(); // skip reserved
        //         bb.getShort(); // skip reserved
        setPowerUsage(bb.getInt());
        setAccumulatedEnergy(bb.getInt());

        setRelayState(BleUtils.isBitSet(getSwitchState(), 7));
        setPwm(getSwitchState() & ~(1 << 7));
    }
}

From source file:org.cablelabs.playready.cryptfile.PlayReadyPSSH.java

@Override
public Element generateContentProtection(Document d) throws IOException {
    Element e = super.generateContentProtection(d);

    switch (cpType) {

    case CENC:/*ww  w  .j  a v a2 s  .c  o  m*/
        e.appendChild(generateCENCContentProtectionData(d));
        break;

    case MSPRO:
        Element pro = d.createElement(MSPRO_ELEMENT);

        // Generate base64-encoded PRO
        ByteBuffer ba = ByteBuffer.allocate(4);
        ba.order(ByteOrder.LITTLE_ENDIAN);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);

        // PlayReady Header Object Size field
        ba.putInt(0, proSize);
        dos.write(ba.array());

        // Number of Records field
        ba.putShort(0, (short) wrmHeaders.size());
        dos.write(ba.array(), 0, 2);

        for (WRMHeader header : wrmHeaders) {

            byte[] wrmData = header.getWRMHeaderData();

            // Record Type (always 1 for WRM Headers)
            ba.putShort(0, (short) 1);
            dos.write(ba.array(), 0, 2);

            // Record Length
            ba.putShort(0, (short) wrmData.length);
            dos.write(ba.array(), 0, 2);

            // Data
            dos.write(wrmData);
        }

        pro.setTextContent(Base64.encodeBase64String(baos.toByteArray()));

        e.appendChild(pro);
        break;
    }

    return e;
}