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:Main.java

private static String formatShorts(byte[] data, boolean unsigned) {
    ShortBuffer bb = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();

    StringBuilder sb = new StringBuilder(bb.capacity() * 3);

    while (bb.remaining() > 0) {
        if (unsigned) {
            sb.append(bb.get() & 0xffff);
        } else {// ww  w. j a v a 2  s  .co  m
            sb.append(bb.get());
        }
        sb.append(',');
        sb.append('\n');
    }

    return sb.toString();
}

From source file:Main.java

public static String smallFileSha1(File file) {
    InputStream inputStream = null;
    try {/*from w  w  w . j a v  a  2 s.  co m*/
        MessageDigest md = MessageDigest.getInstance("SHA1");
        inputStream = new BufferedInputStream(new FileInputStream(file));
        byte[] buffer = new byte[1024];
        int nRead = 0;
        while ((nRead = inputStream.read(buffer)) != -1) {
            md.update(buffer, 0, nRead);
        }
        byte[] digest = md.digest();
        byte[] blockBytes = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(1).array();
        byte[] result = ByteBuffer.allocate(4 + digest.length).put(blockBytes, 0, 4)
                .put(digest, 0, digest.length).array();
        return Base64.encodeToString(result, Base64.URL_SAFE | Base64.NO_WRAP);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:Main.java

public static boolean isLittleEndian() {
    ByteOrder b = ByteOrder.nativeOrder();
    return b.equals(ByteOrder.LITTLE_ENDIAN);
}

From source file:Main.java

public static Buffer fillBuffer(double[] array) {
    // Convert to floats because OpenGL doesnt work on doubles, and manually
    // casting each input value would take too much time.
    ByteBuffer bb = ByteBuffer.allocateDirect(4 * array.length); // each
    // float/*from  ww w .j a  v  a  2  s  .  co  m*/
    // takes 4
    // bytes
    bb.order(ByteOrder.LITTLE_ENDIAN);
    for (double d : array)
        bb.putFloat((float) d);
    bb.rewind();

    return bb;

}

From source file:Main.java

public static int byteArrayToLeInt(byte[] b) {
    final ByteBuffer bb = ByteBuffer.wrap(b);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    return bb.getInt();
}

From source file:Main.java

public static byte[] leIntToByteArray(int i) {
    final ByteBuffer bb = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.putInt(i);//from w  w  w  .j a va  2 s. c o  m
    return bb.array();
}

From source file:info.gehrels.flockDBClient.ByteHelper.java

static ByteBuffer asByteBufferOrNull(long... destinationIds) {
    ByteBuffer buf = null;/* w ww.  ja v  a  2s . c  o  m*/
    if (isNotEmpty(destinationIds)) {
        buf = ByteBuffer.wrap(new byte[destinationIds.length * (Long.SIZE / 8)]);
        buf.order(ByteOrder.LITTLE_ENDIAN);
        for (long destinationId : destinationIds) {
            buf.putLong(destinationId);
        }
        buf.rewind();
    }
    return buf;
}

From source file:Main.java

public static byte[] int2bytes(int a) {
    int arraySize = Integer.SIZE / Byte.SIZE;
    ByteBuffer buffer = ByteBuffer.allocate(arraySize);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    return buffer.putInt(a).array();
}

From source file:Main.java

public static float readFloat32(byte[] value, int start, int end) {
    byte[] bytes = Arrays.copyOfRange(value, start, end);
    float result = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getFloat();
    return result;
}

From source file:Main.java

public static double readFloat64(byte[] value, int start, int end) {
    byte[] bytes = Arrays.copyOfRange(value, start, end);
    double result = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getDouble();
    return result;
}