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.linkedin.databus.core.DbusEventV1.java

private static void setAttributeBit(byte[] attribute, int mask, ByteOrder byteOrder) {
    if (mask > 0xffff) // constrained by AttributesLength, which is exactly 2 for V1 events
    {/*from   ww w . ja va  2  s  .  co  m*/
        throw new DatabusRuntimeException("attribute mask exceeds max size of attributes"); // or EventCreationException?  InvalidConfigException?
    }
    byte msByte = (byte) ((mask & 0xff00) >> 8);
    byte lsByte = (byte) (mask & 0xff);
    if (byteOrder == ByteOrder.BIG_ENDIAN) {
        attribute[0] |= msByte;
        attribute[1] |= lsByte;
    } else {
        attribute[0] |= lsByte;
        attribute[1] |= msByte;
    }
}

From source file:org.apache.hadoop.hbase.filter.SlicedRowFilter.java

/**
 * Serialize the filter/*ww w .  j  ava 2 s.  com*/
 */
@Override
public byte[] toByteArray() throws IOException {

    //
    // Allocate buffer for the following data:
    //
    // count: 8 bytes
    // slicesLength: 4 bytes
    // nbounds: 4 bytes (this.bounds.length)
    // bounds: 4 * this.bounds.length
    // Size of range keys: 4 bytes (this.rangekeys.length)
    // slices: this.rangekeys
    //

    ByteBuffer bb = ByteBuffer.wrap(new byte[8 + 4 + 4 * this.bounds.length + 4 + 4 + this.rangekeys.length])
            .order(ByteOrder.BIG_ENDIAN);

    bb.putLong(this.count);
    bb.putInt(this.slicesLength);
    bb.putInt(this.bounds.length);
    for (int i = 0; i < this.bounds.length; i++) {
        bb.putInt(this.bounds[i]);
    }
    bb.putInt(this.rangekeys.length);
    bb.put(this.rangekeys);

    return bb.array();
}

From source file:edu.hawaii.soest.kilonalu.adam.AdamParser.java

/**
 * A method that gets the channel six data mimimum as a converted decimal float
 *
 * @return channelSixMin - the 2 bytes of the channelSixMin data as a float
 *//*from www .j  a v a  2  s. co  m*/
public float getChannelSixMin() {
    this.channelSixMin.flip();
    int channelData = (int) this.channelSixMin.order(ByteOrder.BIG_ENDIAN).getShort();

    return getVoltage(channelData);
}

From source file:byps.BTransport.java

private BProtocol createNegotiatedProtocol(BNegotiate nego) throws BException {
    BProtocol protocol = null;/*from ww  w. j a  v a 2 s . co m*/

    if (nego.protocols.startsWith(BProtocolS.BINARY_MODEL.getProtocolId())) {
        int negotiatedBypsVersion = Math.min(BMessageHeader.BYPS_VERSION_CURRENT, nego.bversion);
        long negotiatedVersion = Math.min(apiDesc.version, nego.version);
        nego.protocols = BProtocolS.BINARY_MODEL.getProtocolId();
        if (nego.byteOrder == null)
            nego.byteOrder = ByteOrder.BIG_ENDIAN;
        nego.version = negotiatedVersion;
        nego.bversion = negotiatedBypsVersion;
        protocol = new BProtocolS(apiDesc, negotiatedBypsVersion, negotiatedVersion, nego.byteOrder);
    } else if (nego.protocols.startsWith(BProtocolJson.BINARY_MODEL.getProtocolId())) {
        nego.protocols = BProtocolJson.BINARY_MODEL.getProtocolId();
        protocol = new BProtocolJson(apiDesc);
    } else {
        throw new BException(BExceptionC.CORRUPT, "Protocol negotiation failed.");
    }

    return protocol;
}

From source file:edu.hawaii.soest.kilonalu.adam.AdamParser.java

/**
 * A method that gets the channel seven data mimimum as a converted decimal float
 *
 * @return channelSevenMin - the 2 bytes of the channelSevenMin data as a float
 *//*  w  w  w .j av  a  2s  .  c  om*/
public float getChannelSevenMin() {
    this.channelSevenMin.flip();
    int channelData = (int) this.channelSevenMin.order(ByteOrder.BIG_ENDIAN).getShort();

    return getVoltage(channelData);
}

From source file:ffx.xray.parsers.MTZFilter.java

/**
 * Read the structure factors.//from ww w. j  a  va2 s . co  m
 *
 * @param mtzFile
 * @param reflectionList
 * @param fcData
 * @param properties
 * @return
 */
public boolean readFcs(File mtzFile, ReflectionList reflectionList, DiffractionRefinementData fcData,
        CompositeConfiguration properties) {

    int nRead, nIgnore, nRes, nFriedel, nCut;
    ByteOrder byteOrder = ByteOrder.nativeOrder();
    FileInputStream fileInputStream;
    DataInputStream dataInputStream;

    StringBuilder sb = new StringBuilder();
    try {
        fileInputStream = new FileInputStream(mtzFile);
        dataInputStream = new DataInputStream(fileInputStream);

        byte headerOffset[] = new byte[4];
        byte bytes[] = new byte[80];
        int offset = 0;

        // Eat "MTZ" title.
        dataInputStream.read(bytes, offset, 4);
        String mtzString = null;

        // Header offset.
        dataInputStream.read(headerOffset, offset, 4);

        // Machine stamp.
        dataInputStream.read(bytes, offset, 4);
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
        int stamp = byteBuffer.order(ByteOrder.BIG_ENDIAN).getInt();
        String stampString = Integer.toHexString(stamp);
        switch (stampString.charAt(0)) {
        case '1':
        case '3':
            if (byteOrder.equals(ByteOrder.LITTLE_ENDIAN)) {
                byteOrder = ByteOrder.BIG_ENDIAN;
            }
            break;
        case '4':
            if (byteOrder.equals(ByteOrder.BIG_ENDIAN)) {
                byteOrder = ByteOrder.LITTLE_ENDIAN;
            }
            break;
        }

        byteBuffer = ByteBuffer.wrap(headerOffset);
        int headerOffsetI = byteBuffer.order(byteOrder).getInt();

        // Skip to header and parse.
        dataInputStream.skipBytes((headerOffsetI - 4) * 4);

        for (Boolean parsing = true; parsing; dataInputStream.read(bytes, offset, 80)) {
            mtzString = new String(bytes);
            parsing = parseHeader(mtzString);
        }

        // Column identifiers.
        fc = phiC = fs = phiS = -1;
        boolean print = true;
        parseFcColumns(print);

        if (h < 0 || k < 0 || l < 0) {
            String message = " Fatal error in MTZ file - no H K L indexes?\n";
            logger.log(Level.SEVERE, message);
            return false;
        }

        // Reopen to start at beginning.
        fileInputStream = new FileInputStream(mtzFile);
        dataInputStream = new DataInputStream(fileInputStream);

        // Skip initial header.
        dataInputStream.skipBytes(80);

        float data[] = new float[nColumns];
        HKL mate = new HKL();

        // Read in data.
        ComplexNumber complexNumber = new ComplexNumber();
        nRead = nIgnore = nRes = nFriedel = nCut = 0;
        for (int i = 0; i < nReflections; i++) {
            for (int j = 0; j < nColumns; j++) {
                dataInputStream.read(bytes, offset, 4);
                byteBuffer = ByteBuffer.wrap(bytes);
                data[j] = byteBuffer.order(byteOrder).getFloat();
            }
            int ih = (int) data[h];
            int ik = (int) data[k];
            int il = (int) data[l];
            boolean friedel = reflectionList.findSymHKL(ih, ik, il, mate, false);
            HKL hkl = reflectionList.getHKL(mate);

            if (hkl != null) {
                if (fc > 0 && phiC > 0) {
                    complexNumber.re(data[fc] * cos(toRadians(data[phiC])));
                    complexNumber.im(data[fc] * sin(toRadians(data[phiC])));
                    fcData.setFc(hkl.index(), complexNumber);
                }
                if (fs > 0 && phiS > 0) {
                    complexNumber.re(data[fs] * cos(toRadians(data[phiS])));
                    complexNumber.im(data[fs] * sin(toRadians(data[phiS])));
                    fcData.setFs(hkl.index(), complexNumber);
                }
                nRead++;
            } else {
                HKL tmp = new HKL(ih, ik, il);
                if (!reflectionList.resolution
                        .inInverseResSqRange(Crystal.invressq(reflectionList.crystal, tmp))) {
                    nRes++;
                } else {
                    nIgnore++;
                }
            }
        }

        if (logger.isLoggable(Level.INFO)) {
            sb.append(format(" MTZ file type (machine stamp): %s\n", stampString));
            sb.append(format(" Fc HKL read in:                             %d\n", nRead));
            sb.append(format(" Fc HKL read as friedel mates:               %d\n", nFriedel));
            sb.append(format(" Fc HKL NOT read in (too high resolution):   %d\n", nRes));
            sb.append(format(" Fc HKL NOT read in (not in internal list?): %d\n", nIgnore));
            sb.append(format(" Fc HKL NOT read in (F/sigF cutoff):         %d\n", nCut));
            sb.append(
                    format(" HKL in internal list:                       %d\n", reflectionList.hkllist.size()));
            logger.info(sb.toString());
        }
    } catch (EOFException e) {
        String message = " MTZ end of file reached.";
        logger.log(Level.WARNING, message, e);
        return false;
    } catch (IOException e) {
        String message = " MTZ IO Exception.";
        logger.log(Level.WARNING, message, e);
        return false;
    }

    return true;
}

From source file:edu.hawaii.soest.kilonalu.adam.AdamParser.java

/**
 * A method that gets the channel average mimimum data as a converted decimal float
 *
 * @return channelAverageMin - the 2 bytes of the channelAverageMin data as a float
 *///  www  . j  a v a  2  s. c om
public float getChannelAverageMin() {
    this.channelAverageMin.flip();
    int channelData = (int) this.channelAverageMin.order(ByteOrder.BIG_ENDIAN).getShort();

    return getVoltage(channelData);
}

From source file:io.warp10.continuum.gts.GTSDecoder.java

public static GTSDecoder fromBlock(byte[] block, byte[] key) throws IOException {

    if (block.length < 6) {
        throw new IOException("Invalid block.");
    }//from ww w . j ava2s  .  c om

    ByteBuffer buffer = ByteBuffer.wrap(block);

    //
    // Extract size
    //

    buffer.order(ByteOrder.BIG_ENDIAN);
    int size = buffer.getInt();

    // Check size

    if (block.length != size) {
        throw new IOException("Invalid block size, expected " + size + ", block is " + block.length);
    }

    // Extract compression

    byte comp = buffer.get();

    boolean compress = false;

    if (0 == comp) {
        compress = false;
    } else if (1 == comp) {
        compress = true;
    } else {
        throw new IOException("Invalid compression flag");
    }

    // Extract base timestamp

    long base = Varint.decodeSignedLong(buffer);

    InputStream in;

    ByteArrayInputStream bain = new ByteArrayInputStream(block, buffer.position(), buffer.remaining());

    if (compress) {
        in = new GZIPInputStream(bain);
    } else {
        in = bain;
    }

    byte[] buf = new byte[1024];

    ByteArrayOutputStream out = new ByteArrayOutputStream(buffer.remaining());

    while (true) {
        int len = in.read(buf);

        if (len <= 0) {
            break;
        }
        out.write(buf, 0, len);
    }

    GTSDecoder decoder = new GTSDecoder(base, key, ByteBuffer.wrap(out.toByteArray()));

    return decoder;
}

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

/**
 * Decodes a GUID value./*  w  w  w.j a  v  a 2  s.  c  om*/
 */
private static String readGUIDValue(ByteBuffer buffer, ByteOrder order) {
    if (order != ByteOrder.BIG_ENDIAN) {
        byte[] tmpArr = ByteUtil.getBytes(buffer, 16);

        // 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

@Override
public byte[] encodeSetTime() {
    final short LENGTH_SETTIME = 5;
    long ts = System.currentTimeMillis();
    long ts_offset = (SimpleTimeZone.getDefault().getOffset(ts));
    ByteBuffer buf;/*from   w w  w  .j ava  2 s .c om*/
    if (mFwMajor >= 3) {
        String timezone = SimpleTimeZone.getDefault().getID();
        short length = (short) (LENGTH_SETTIME + timezone.getBytes().length + 3);
        buf = ByteBuffer.allocate(LENGTH_PREFIX + length);
        buf.order(ByteOrder.BIG_ENDIAN);
        buf.putShort(length);
        buf.putShort(ENDPOINT_TIME);
        buf.put(TIME_SETTIME_UTC);
        buf.putInt((int) (ts / 1000));
        buf.putShort((short) (ts_offset / 60000));
        buf.put((byte) timezone.getBytes().length);
        buf.put(timezone.getBytes());
        LOG.info(timezone);
    } else {
        buf = ByteBuffer.allocate(LENGTH_PREFIX + LENGTH_SETTIME);
        buf.order(ByteOrder.BIG_ENDIAN);
        buf.putShort(LENGTH_SETTIME);
        buf.putShort(ENDPOINT_TIME);
        buf.put(TIME_SETTIME);
        buf.putInt((int) ((ts + ts_offset) / 1000));
    }
    return buf.array();
}