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:com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionAzureKeyVaultProvider.java

private short convertTwoBytesToShort(byte[] input, int index) throws SQLServerException {

    short shortVal = -1;
    if (index + 1 >= input.length) {
        throw new SQLServerException(null, SQLServerException.getErrString("R_ByteToShortConversion"), null, 0,
                false);/*from w  w w. j a v a 2 s  . c o  m*/
    }
    ByteBuffer byteBuffer = ByteBuffer.allocate(2);
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    byteBuffer.put(input[index]);
    byteBuffer.put(input[index + 1]);
    shortVal = byteBuffer.getShort(0);
    return shortVal;

}

From source file:org.basdroid.common.NetworkUtils.java

public static String getMacAddressFromNetworkInterface(final Context context) {

    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();

    // Convert little-endian to big-endianif needed
    if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
        ipAddress = Integer.reverseBytes(ipAddress);
    }/*  www . j  av a2s .c  o m*/

    byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray();

    String result;
    try {
        InetAddress addr = InetAddress.getByAddress(bytes);
        NetworkInterface netInterface = NetworkInterface.getByInetAddress(addr);
        Log.d(TAG, "Wifi netInterface.getName() = " + netInterface.getName());

        byte[] mac = netInterface.getHardwareAddress();
        if (mac == null || mac.length == 0)
            return "";
        StringBuilder buf = new StringBuilder();
        for (int idx = 0; idx < mac.length; idx++) {
            buf.append(String.format("%02X:", mac[idx]));
        }
        if (buf.length() > 0)
            buf.deleteCharAt(buf.length() - 1);
        return buf.toString();
    } catch (UnknownHostException ex) {
        Log.e(TAG, "getMacAddressFromNetworkInterface() Unknown host.", ex);
        result = null;
    } catch (SocketException ex) {
        Log.e(TAG, "getMacAddressFromNetworkInterface() Socket exception.", ex);
        result = null;
    } catch (Exception ex) {
        Log.e(TAG, "getMacAddressFromNetworkInterface() Exception.", ex);
        result = null;
    }

    return result;
}

From source file:org.xenei.bloomgraph.bloom.sql.DBIO.java

/**
 * Convert the byte buffer to a input stream.
 * //from w  ww .j  a  v  a2 s.co m
 * @param buffer
 *            the buffer to conver
 * @return the input stream.
 */
public static InputStream asInputStream(final ByteBuffer buffer) {
    final ByteBuffer buff = buffer.slice().order(ByteOrder.LITTLE_ENDIAN);
    if (buff.hasArray()) {
        // use heap buffer; no array is created; only the reference is used
        return new ByteArrayInputStream(buff.array());
    }
    return new ByteBufferInputStream(buff);
}

From source file:srebrinb.compress.sevenzip.SevenZOutputFile.java

/**
 * Finishes the addition of entries to this archive, without closing it.
 * /*from www  .  j a v  a 2 s  .c om*/
 * @throws IOException if archive is already closed.
 */
public void finish() throws IOException {
    if (finished) {
        throw new IOException("This archive has already been finished");
    }
    finished = true;

    final long headerPosition = channel.position();

    final ByteArrayOutputStream headerBaos = new ByteArrayOutputStream();
    final DataOutputStream header = new DataOutputStream(headerBaos);

    writeHeader(header);
    header.flush();
    final byte[] headerBytes = headerBaos.toByteArray();
    channel.write(ByteBuffer.wrap(headerBytes));

    final CRC32 crc32 = new CRC32();
    crc32.update(headerBytes);

    ByteBuffer bb = ByteBuffer.allocate(SevenZFile.sevenZSignature.length + 2 /* version */
            + 4 /* start header CRC */
            + 8 /* next header position */
            + 8 /* next header length */
            + 4 /* next header CRC */).order(ByteOrder.LITTLE_ENDIAN);
    // signature header
    channel.position(0);
    bb.put(SevenZFile.sevenZSignature);
    // version
    bb.put((byte) 0).put((byte) 2);

    // placeholder for start header CRC
    bb.putInt(0);

    // start header
    bb.putLong(headerPosition - SevenZFile.SIGNATURE_HEADER_SIZE).putLong(0xffffFFFFL & headerBytes.length)
            .putInt((int) crc32.getValue());
    crc32.reset();
    crc32.update(bb.array(), SevenZFile.sevenZSignature.length + 6, 20);
    bb.putInt(SevenZFile.sevenZSignature.length + 2, (int) crc32.getValue());
    bb.flip();
    channel.write(bb);
}

From source file:xbird.storage.io.RemoteVarSegments.java

private byte[] recvResponse(final ByteChannel channel, final ByteBuffer rcvBuf) throws IOException {
    ByteBuffer tmpBuf = ByteBuffer.allocate(4);
    NIOUtils.readFully(channel, tmpBuf, 4);
    tmpBuf.flip();/*from  w ww.j av  a2s .com*/
    int datalen = tmpBuf.getInt();

    final ByteBuffer buf = truncateBuffer(rcvBuf, datalen);
    NIOUtils.readFully(channel, buf, datalen);
    buf.flip();
    if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
        buf.order(ByteOrder.BIG_ENDIAN);
    }
    final byte[] b = new byte[datalen];
    buf.get(b);
    if (buf != rcvBuf) {
        _rbufPool.returnObject(buf);
    }
    return b;
}

From source file:au.org.ala.layers.grid.GridCacheBuilder.java

static public float getNextValue(RandomAccessFile raf, Grid g) {
    float f = Float.NaN;
    try {/*  ww w  .j av  a 2  s .  c o m*/
        int size;
        byte[] b;
        if (g.datatype.charAt(0) == 'B') {//"BYTE")) {
            f = raf.readByte();
        } else if (g.datatype.charAt(0) == 'U') {//equalsIgnoreCase("UBYTE")) {
            f = raf.readByte();
            if (f < 0) {
                f += 256;
            }
        } else if (g.datatype.charAt(0) == 'S') {//equalsIgnoreCase("SHORT")) {
            size = 2;
            b = new byte[size];
            raf.read(b);
            if (g.byteorderLSB) {
                f = (short) (((0xFF & b[1]) << 8) | (b[0] & 0xFF));
            } else {
                f = (short) (((0xFF & b[0]) << 8) | (b[1] & 0xFF));
            }
        } else if (g.datatype.charAt(0) == 'I') {//equalsIgnoreCase("INT")) {
            size = 4;
            b = new byte[size];
            raf.read(b);
            if (g.byteorderLSB) {
                f = ((0xFF & b[3]) << 24) | ((0xFF & b[2]) << 16) + ((0xFF & b[1]) << 8) + (b[0] & 0xFF);
            } else {
                f = ((0xFF & b[0]) << 24)
                        | ((0xFF & b[1]) << 16) + ((0xFF & b[2]) << 8) + ((0xFF & b[3]) & 0xFF);
            }
        } else if (g.datatype.charAt(0) == 'L') {//equalsIgnoreCase("LONG")) {
            size = 8;
            b = new byte[size];
            raf.read(b);
            if (g.byteorderLSB) {
                f = ((long) (0xFF & b[7]) << 56) + ((long) (0xFF & b[6]) << 48) + ((long) (0xFF & b[5]) << 40)
                        + ((long) (0xFF & b[4]) << 32) + ((long) (0xFF & b[3]) << 24)
                        + ((long) (0xFF & b[2]) << 16) + ((long) (0xFF & b[1]) << 8) + (0xFF & b[0]);
            } else {
                f = ((long) (0xFF & b[0]) << 56) + ((long) (0xFF & b[1]) << 48) + ((long) (0xFF & b[2]) << 40)
                        + ((long) (0xFF & b[3]) << 32) + ((long) (0xFF & b[4]) << 24)
                        + ((long) (0xFF & b[5]) << 16) + ((long) (0xFF & b[6]) << 8) + (0xFF & b[7]);
            }
        } else if (g.datatype.charAt(0) == 'F') {//.equalsIgnoreCase("FLOAT")) {
            size = 4;
            b = new byte[size];
            raf.read(b);
            ByteBuffer bb = ByteBuffer.wrap(b);
            if (g.byteorderLSB) {
                bb.order(ByteOrder.LITTLE_ENDIAN);
            }
            f = bb.getFloat();

        } else if (g.datatype.charAt(0) == 'D') {//.equalsIgnoreCase("DOUBLE")) {
            size = 8;
            b = new byte[8];
            raf.read(b);
            ByteBuffer bb = ByteBuffer.wrap(b);
            if (g.byteorderLSB) {
                bb.order(ByteOrder.LITTLE_ENDIAN);
            }
            f = (float) bb.getDouble();
        }
        //replace not a number            
        if ((float) f == (float) g.nodatavalue) {
            f = Float.NaN;
        }
    } catch (Exception e) {
    }
    return f;
}

From source file:io.github.dsheirer.source.tuner.hackrf.HackRFTunerController.java

@Override
public void setTunedFrequency(long frequency) throws SourceException {
    ByteBuffer buffer = ByteBuffer.allocateDirect(8);
    buffer.order(ByteOrder.LITTLE_ENDIAN);

    int mhz = (int) (frequency / 1E6);
    int hz = (int) (frequency - (mhz * 1E6));

    buffer.putInt(mhz);//from  ww  w .j  a  v  a  2s . c om
    buffer.putInt(hz);

    buffer.rewind();

    try {
        write(Request.SET_FREQUENCY, 0, 0, buffer);
    } catch (UsbException e) {
        mLog.error("error setting frequency [" + frequency + "]", e);

        throw new SourceException("error setting frequency [" + frequency + "]", e);
    }
}

From source file:org.apache.myriad.state.utils.ByteBufferSupport.java

private static ByteBuffer createBuffer(int size) {
    return ByteBuffer.allocate(size).order(ByteOrder.LITTLE_ENDIAN);
}

From source file:org.apache.spark.sql.execution.vectorized.OffHeapColumnVector.java

@Override
public void putDoubles(int rowId, int count, byte[] src, int srcIndex) {
    if (!bigEndianPlatform) {
        Platform.copyMemory(src, Platform.BYTE_ARRAY_OFFSET + srcIndex, null, data + rowId * 8, count * 8);
    } else {//from   w  w w .  j  av  a 2  s . c o  m
        ByteBuffer bb = ByteBuffer.wrap(src).order(ByteOrder.LITTLE_ENDIAN);
        long offset = data + 8 * rowId;
        for (int i = 0; i < count; ++i, offset += 8) {
            Platform.putDouble(null, offset, bb.getDouble(srcIndex + (8 * i)));
        }
    }
}

From source file:com.github.nlloyd.hornofmongo.util.BSONizer.java

@SuppressWarnings("deprecation")
public static Object convertBSONtoJS(MongoScope mongoScope, Object bsonObject) {
    Object jsObject = null;/*from   ww w . j av a  2s .c o m*/
    if (bsonObject instanceof List<?>) {
        List<?> bsonList = (List<?>) bsonObject;
        Scriptable jsArray = (Scriptable) MongoRuntime.call(new NewInstanceAction(mongoScope, bsonList.size()));

        int index = 0;
        for (Object bsonEntry : bsonList) {
            ScriptableObject.putProperty(jsArray, index, convertBSONtoJS(mongoScope, bsonEntry));
            index++;
        }

        jsObject = jsArray;
    } else if (bsonObject instanceof BSONObject) {
        Scriptable jsObj = (Scriptable) MongoRuntime.call(new NewInstanceAction(mongoScope));
        BSONObject bsonObj = (BSONObject) bsonObject;

        for (String key : bsonObj.keySet()) {
            Object value = convertBSONtoJS(mongoScope, bsonObj.get(key));
            MongoRuntime.call(new JSPopulatePropertyAction(jsObj, key, value));
        }
        jsObject = jsObj;
    } else if (bsonObject instanceof Symbol) {
        jsObject = ((Symbol) bsonObject).getSymbol();
    } else if (bsonObject instanceof Date) {
        jsObject = MongoRuntime.call(
                new NewInstanceAction(mongoScope, "Date", new Object[] { ((Date) bsonObject).getTime() }));
    } else if (bsonObject instanceof Pattern) {
        Pattern regex = (Pattern) bsonObject;
        String source = regex.pattern();
        String options = Bytes.regexFlags(regex.flags());
        jsObject = MongoRuntime
                .call(new NewInstanceAction(mongoScope, "RegExp", new Object[] { source, options }));
    } else if (bsonObject instanceof org.bson.types.ObjectId) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "ObjectId"));
        ((ObjectId) jsObject).setRealObjectId((org.bson.types.ObjectId) bsonObject);
    } else if (bsonObject instanceof org.bson.types.MinKey) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "MinKey"));
    } else if (bsonObject instanceof org.bson.types.MaxKey) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "MaxKey"));
    } else if (bsonObject instanceof com.mongodb.DBRef) {
        com.mongodb.DBRef dbRef = (com.mongodb.DBRef) bsonObject;
        Object id = convertBSONtoJS(mongoScope, dbRef.getId());
        jsObject = MongoRuntime.call(
                new NewInstanceAction(mongoScope, "DBRef", new Object[] { dbRef.getCollectionName(), id }));
    } else if (bsonObject instanceof BSONTimestamp) {
        BSONTimestamp bsonTstamp = (BSONTimestamp) bsonObject;
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "Timestamp",
                new Object[] { bsonTstamp.getTime(), bsonTstamp.getInc() }));
    } else if (bsonObject instanceof Long) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "NumberLong"));
        ((NumberLong) jsObject).setRealLong((Long) bsonObject);
    } else if (bsonObject instanceof Integer) {
        jsObject = Double.valueOf((Integer) bsonObject);
    } else if (bsonObject instanceof Code) {
        jsObject = ((Code) bsonObject).getCode();
    } else if (bsonObject instanceof byte[]) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "BinData"));
        ((BinData) jsObject).setValues(0, (byte[]) bsonObject);
    } else if (bsonObject instanceof Binary) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "BinData"));
        ((BinData) jsObject).setValues(((Binary) bsonObject).getType(), ((Binary) bsonObject).getData());
    } else if (bsonObject instanceof UUID) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "BinData"));
        UUID uuid = (UUID) bsonObject;
        ByteBuffer dataBuffer = ByteBuffer.allocate(16);
        // mongodb wire protocol is little endian
        dataBuffer.order(ByteOrder.LITTLE_ENDIAN);
        dataBuffer.putLong(uuid.getMostSignificantBits());
        dataBuffer.putLong(uuid.getLeastSignificantBits());
        ((BinData) jsObject).setValues(BSON.B_UUID, dataBuffer.array());
    } else {
        jsObject = bsonObject;
    }

    return jsObject;
}