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:org.cablelabs.playready.cryptfile.PlayReadyPSSH.java

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

    switch (cpType) {

    case CENC:/*from   w w  w  .  ja  va  2 s.c  om*/
        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;
}

From source file:nl.salp.warcraft4j.util.ChecksumTest.java

@Test
public void shouldTrimToSizeWithSmallEndianess() {
    Checksum trimmed = checksum.trim(TRIMMED_LENGTH, ByteOrder.LITTLE_ENDIAN);

    assertEquals("Invalid sized checksum for little endian trimmed data", TRIMMED_LENGTH, trimmed.length());
    assertNotSame("Same instance returned for little endian trimmed data", checksum, trimmed);
    assertArrayEquals("Little endian data not trimmed to size", TRIMMED_LITTLE_ENDIAN, trimmed.getChecksum());
}

From source file:edu.harvard.iq.dvn.unf.Base64Encoding.java

/**
 * Helper function to change the endianess of the byte array
 *
 * @param digest byte array/*from   w  w  w.j a  v a 2s .  c  o m*/
 * @param local ByteOrder
 * @return byte array with endianness according to getBorder()
 */
public static byte[] changeByteOrder(byte[] digest, ByteOrder local) {
    byte[] revdigest = new byte[digest.length];

    if ((local.equals(ByteOrder.LITTLE_ENDIAN) && getBorder().equals(ByteOrder.BIG_ENDIAN))
            || (local.equals(ByteOrder.BIG_ENDIAN) && getBorder().equals(ByteOrder.LITTLE_ENDIAN))) {
        int ln = digest.length;
        for (int n = 0; n < ln; ++n) {
            revdigest[n] = digest[ln - 1 - n];
        }
    } else {
        revdigest = digest;
    }
    return revdigest;
}

From source file:com.newventuresoftware.waveformdemo.MainActivity.java

private short[] getAudioSample() throws IOException {
    InputStream is = getResources().openRawResource(R.raw.jinglebells);
    byte[] data;/*from  ww  w.ja v  a2  s. c o m*/
    try {
        data = IOUtils.toByteArray(is);
    } finally {
        if (is != null) {
            is.close();
        }
    }

    ShortBuffer sb = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
    short[] samples = new short[sb.limit()];
    sb.get(samples);
    return samples;
}

From source file:com.act.lcms.MzMLParser.java

protected static List<Double> base64ToDoubleList(String b64) {
    byte[] decodedBytes = Base64.getDecoder().decode(b64);
    ByteBuffer buf = ByteBuffer.wrap(decodedBytes).order(ByteOrder.LITTLE_ENDIAN);
    List<Double> values = new ArrayList<>(decodedBytes.length / 8);
    while (buf.hasRemaining()) {
        values.add(buf.getDouble());/*from   w w  w . j  a  v  a 2 s .  co m*/
    }
    return values;
}

From source file:main.java.utils.Utility.java

public static long convertByteToUnsignedLong(byte[] b) {
    return (ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN).getLong() & 0xFFFFFFFFL);
}

From source file:uk.ac.cam.cl.dtg.picky.parser.pcap2.PcapParser.java

private byte[] toPCAPFormat(Packet packet) {
    ByteArrayBuffer buffer = new ByteArrayBuffer(50);

    ByteBuffer b = ByteBuffer.allocate(16);
    b.order(ByteOrder.LITTLE_ENDIAN);

    // guint32 ts_sec; /* timestamp seconds */
    putUnsignedInt(b, pcap.getTimestamp().getTime() / 1000);

    // guint32 ts_usec; /* timestamp microseconds */
    putUnsignedInt(b, (pcap.getTimestamp().getTime() - pcap.getTimestamp().getTime() / 1000 * 1000));

    // guint32 incl_len; /* number of octets of packet saved in file */
    putUnsignedInt(b, packet.getRawData().length);

    // guint32 orig_len; /* actual length of packet */
    putUnsignedInt(b, packet.length());//  ww  w  .ja  va 2  s .co  m
    // FIXME: putUnsignedInt(b, packet.getOriginalLength());

    buffer.append(b.array(), 0, 16);
    buffer.append(packet.getRawData(), 0, packet.getRawData().length);

    return buffer.toByteArray();
}

From source file:org.mcisb.util.math.MathUtils.java

/**
 * //from  w  w  w.j a  v a2 s  .  co m
 * @param encoded
 * @param bigEndian
 * @param doublePrecision
 * @return double[]
 */
public static double[] decode(final byte[] encoded, final boolean bigEndian, final boolean doublePrecision) {
    final byte[] bytes = Base64.decode(encoded);
    ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
    byteBuffer = byteBuffer.order(bigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);

    final int limit = byteBuffer.limit();
    double[] decoded = new double[((doublePrecision) ? limit / DOUBLE_LENGTH : limit / FLOAT_LENGTH)];
    int i = 0;

    while (byteBuffer.hasRemaining()) {
        if (doublePrecision) {
            decoded[i++] = byteBuffer.getDouble();
        } else {
            decoded[i++] = byteBuffer.getFloat();
        }
    }

    return decoded;
}

From source file:de.undercouch.bson4jackson.BsonGeneratorTest.java

private void assertRaw(byte[] r) throws Exception {
    ByteArrayInputStream bais = new ByteArrayInputStream(r);
    BSONDecoder decoder = new BasicBSONDecoder();
    BSONObject obj = decoder.readObject(bais);
    byte[] o = (byte[]) obj.get("Test");
    CharBuffer buf = ByteBuffer.wrap(o).order(ByteOrder.LITTLE_ENDIAN).asCharBuffer();
    assertEquals(2, buf.remaining());//from  w w  w. j  a  v a 2 s.co m
    char a = buf.get();
    char b = buf.get();
    assertEquals('a', a);
    assertEquals('b', b);
}

From source file:org.zuinnote.hadoop.bitcoin.format.BitcoinUtil.java

/**
* Reads a size from a reversed byte order, such as block size in the block header
*
* @param byteSize byte array with a length of exactly 4 
* 
* @return size, returns 0 in case of invalid block size
*
*///from  w  ww . ja  v a2  s .  c o m

public static long getSize(byte[] byteSize) {
    if (byteSize.length != 4)
        return 0;
    ByteBuffer converterBuffer = ByteBuffer.wrap(byteSize);
    converterBuffer.order(ByteOrder.LITTLE_ENDIAN);
    return convertSignedIntToUnsigned(converterBuffer.getInt());
}