Example usage for java.nio ByteOrder nativeOrder

List of usage examples for java.nio ByteOrder nativeOrder

Introduction

In this page you can find the example usage for java.nio ByteOrder nativeOrder.

Prototype

public static ByteOrder nativeOrder() 

Source Link

Document

Returns the current platform byte order.

Usage

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

/**
 *
 * @param digest byte array//from w  w  w.ja  va 2  s. com
 * @param enc String with final encoding
 * @return String the encoded base64 of digest
 * @throws UnsupportedEncodingException
 */
public static String tobase64(byte[] digest, String enc) throws UnsupportedEncodingException {

    ByteArrayOutputStream btstream = new ByteArrayOutputStream();
    //this make sure is written in big-endian

    DataOutputStream stream = new DataOutputStream(btstream);

    byte[] tobase64 = null;
    byte[] revdigest = new byte[digest.length];
    revdigest = changeByteOrder(digest, ByteOrder.nativeOrder());
    try {

        stream.write(revdigest);
        stream.flush();

        tobase64 = Base64.encodeBase64(btstream.toByteArray());

    } catch (IOException io) {
        tobase64 = Base64.encodeBase64(digest);
    }

    return new String(tobase64, enc);
}

From source file:com.google.fpl.liquidfunpaint.ParticleRenderer.java

public ParticleRenderer() {
    mParticlePositionBuffer = ByteBuffer.allocateDirect(2 * 4 * Renderer.MAX_PARTICLE_COUNT)
            .order(ByteOrder.nativeOrder());
    mParticleColorBuffer = ByteBuffer.allocateDirect(4 * Renderer.MAX_PARTICLE_COUNT)
            .order(ByteOrder.nativeOrder());
    mParticleWeightBuffer = ByteBuffer.allocateDirect(4 * Renderer.MAX_PARTICLE_COUNT)
            .order(ByteOrder.nativeOrder());
}

From source file:org.ballerinalang.stdlib.io.data.DataInputOutputTest.java

@Test(description = "Test signed var long", dataProvider = "SignedVarLongValues")
public void testSingedVarLong(long value, int byteCount) throws IOException {
    String filePath = currentDirectoryPath + "/sample.bin";
    ByteChannel byteChannel = TestUtil.openForReadingAndWriting(filePath);
    Channel channel = new MockByteChannel(byteChannel);
    DataChannel dataChannel = new DataChannel(channel, ByteOrder.nativeOrder());
    dataChannel.writeLong(value, Representation.VARIABLE);
    channel.close();/*from w  w  w.  java  2  s. c  o  m*/
    byteChannel = TestUtil.openForReadingAndWriting(filePath);
    channel = new MockByteChannel(byteChannel);
    dataChannel = new DataChannel(channel, ByteOrder.nativeOrder());
    LongResult longResult = dataChannel.readLong(Representation.VARIABLE);
    long readInt = longResult.getValue();
    int nBytes = longResult.getByteCount();
    Assert.assertEquals(readInt, value);
    Assert.assertEquals(nBytes, byteCount);
}

From source file:ffx.xray.CCP4MapFilter.java

/**
 * {@inheritDoc}/*from www  . j a v  a2  s . co  m*/
 */
@Override
public Crystal getCrystal(String filename, CompositeConfiguration properties) {
    int imapdata;
    int sg = -1;
    double cella = -1.0;
    double cellb = -1.0;
    double cellc = -1.0;
    double cellalpha = -1.0;
    double cellbeta = -1.0;
    double cellgamma = -1.0;

    ByteOrder b = ByteOrder.nativeOrder();

    FileInputStream fis;
    DataInputStream dis;

    // first determine byte order of file versus system
    try {
        fis = new FileInputStream(filename);
        dis = new DataInputStream(fis);

        dis.skipBytes(212);
        byte bytes[] = new byte[4];
        dis.read(bytes, 0, 4);
        ByteBuffer bb = ByteBuffer.wrap(bytes);

        imapdata = bb.order(ByteOrder.BIG_ENDIAN).getInt();
        String stampstr = Integer.toHexString(imapdata);
        // System.out.println("stamp: " + stampstr);
        switch (stampstr.charAt(0)) {
        case '1':
        case '3':
            if (b.equals(ByteOrder.LITTLE_ENDIAN)) {
                b = ByteOrder.BIG_ENDIAN;
            }
            break;
        case '4':
            if (b.equals(ByteOrder.BIG_ENDIAN)) {
                b = ByteOrder.LITTLE_ENDIAN;
            }
            break;
        }
        fis.close();
    } catch (Exception e) {
        String message = "Fatal exception reading CCP4 map.\n";
        logger.log(Level.SEVERE, message, e);
        System.exit(-1);
    }

    try {
        fis = new FileInputStream(filename);
        dis = new DataInputStream(fis);

        dis.skipBytes(40);
        byte bytes[] = new byte[80];
        dis.read(bytes, 0, 80);
        ByteBuffer bb = ByteBuffer.wrap(bytes);

        cella = bb.order(b).getFloat();
        cellb = bb.order(b).getFloat();
        cellc = bb.order(b).getFloat();
        cellalpha = bb.order(b).getFloat();
        cellbeta = bb.order(b).getFloat();
        cellgamma = bb.order(b).getFloat();

        for (int i = 0; i < 3; i++) {
            bb.order(b).getInt();
        }
        for (int i = 0; i < 3; i++) {
            bb.order(b).getFloat();
        }

        sg = bb.order(b).getInt();
        fis.close();
    } catch (Exception e) {
        String message = "Fatal exception reading CCP4 map.\n";
        logger.log(Level.SEVERE, message, e);
        System.exit(-1);
    }

    return new Crystal(cella, cellb, cellc, cellalpha, cellbeta, cellgamma, SpaceGroup.spaceGroupNames[sg - 1]);
}

From source file:ffx.realspace.CCP4MapFilter.java

/**
 * {@inheritDoc}//www  .j  a v  a2  s  . com
 */
@Override
public Crystal getCrystal(String fileName, CompositeConfiguration properties) {
    int imapData;
    int spaceGroup = -1;
    double cellA = -1.0;
    double cellB = -1.0;
    double cellC = -1.0;
    double cellAlpha = -1.0;
    double cellBeta = -1.0;
    double cellGamma = -1.0;

    ByteOrder byteOrder = ByteOrder.nativeOrder();
    FileInputStream fileInputStream;
    DataInputStream dataInputStream;

    // first determine byte order of file versus system
    try {
        fileInputStream = new FileInputStream(fileName);
        dataInputStream = new DataInputStream(fileInputStream);

        dataInputStream.skipBytes(212);
        byte bytes[] = new byte[4];
        dataInputStream.read(bytes, 0, 4);
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);

        imapData = byteBuffer.order(ByteOrder.BIG_ENDIAN).getInt();
        String stampString = Integer.toHexString(imapData);

        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;
        }
        fileInputStream.close();
    } catch (Exception e) {
        String message = " Fatal exception reading CCP4 map.\n";
        logger.log(Level.SEVERE, message, e);
    }

    try {
        fileInputStream = new FileInputStream(fileName);
        dataInputStream = new DataInputStream(fileInputStream);

        dataInputStream.skipBytes(40);
        byte bytes[] = new byte[80];
        dataInputStream.read(bytes, 0, 80);
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);

        cellA = byteBuffer.order(byteOrder).getFloat();
        cellB = byteBuffer.order(byteOrder).getFloat();
        cellC = byteBuffer.order(byteOrder).getFloat();
        cellAlpha = byteBuffer.order(byteOrder).getFloat();
        cellBeta = byteBuffer.order(byteOrder).getFloat();
        cellGamma = byteBuffer.order(byteOrder).getFloat();

        for (int i = 0; i < 3; i++) {
            byteBuffer.order(byteOrder).getInt();
        }
        for (int i = 0; i < 3; i++) {
            byteBuffer.order(byteOrder).getFloat();
        }

        spaceGroup = byteBuffer.order(byteOrder).getInt();
        fileInputStream.close();
    } catch (Exception e) {
        String message = " Fatal exception reading CCP4 map.\n";
        logger.log(Level.SEVERE, message, e);
    }

    return new Crystal(cellA, cellB, cellC, cellAlpha, cellBeta, cellGamma,
            SpaceGroup.spaceGroupNames[spaceGroup - 1]);
}

From source file:rb.app.GLObject.java

public void allocateBuffer() {
    int SHORT_MAX = 250000;
    int FLOAT_MAX = 1000000;

    Log.d("GLRenderer", "Allocate (short):" + SHORT_MAX * 2 + " bytes");
    ByteBuffer vbb = ByteBuffer.allocateDirect(SHORT_MAX * 2);
    vbb.order(ByteOrder.nativeOrder());
    _shortBuffer = vbb.asShortBuffer();/*from  w w w .  j ava2 s  . c om*/
    _shortBuffer.position(0);

    Log.d("GLRenderer", "Allocate (float):" + FLOAT_MAX * 4 + " bytes");
    ByteBuffer fbb = ByteBuffer.allocateDirect(FLOAT_MAX * 4);
    fbb.order(ByteOrder.nativeOrder());
    _floatBuffer = fbb.asFloatBuffer();
    _floatBuffer.position(0);
}

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

/**
 * Alternative tobase64 method.Encodes the String(byte[]) instead of the array
 *
 * @param digest byte array for encoding in base 64,
 * @param cset String with name of charset
 * @return String base 64 the encoded base64 of digest
 */// w  w  w .j  a  va  2s . com
public static String tobase641(byte[] digest, String cset) {
    byte[] revdigest = changeByteOrder(digest, ByteOrder.nativeOrder());
    String str = null;

    str = new String(revdigest);
    ByteArrayOutputStream btstream = new ByteArrayOutputStream();
    //this make sure is written in big-endian

    DataOutputStream stream = new DataOutputStream(btstream);
    String tobase64 = null;
    //use a charset for encoding
    if (cset == null) {
        cset = DEFAULT_CHAR_ENCODING;
    }
    BCodec bc = new BCodec(cset);
    try {

        tobase64 = (String) bc.encode(str);

    } catch (EncoderException err) {
        mLog.info("base64Encoding: exception" + err.getMessage());
    }
    return tobase64;
}

From source file:de.ailis.threedee.utils.BufferUtils.java

/**
 * Converts the specified integer buffer to native endian and returns this
 * new buffer. If buffer is already in correct endian format then it is
 * returned right away.//from  www .  ja v a 2  s.c  o  m
 *
 * @param buffer
 *            The integer buffer to convert
 * @return The converted integer buffer or the source buffer if no
 *         conversion is needed
 */

public static IntBuffer convertToNativeEndian(final IntBuffer buffer) {
    if (buffer.order() == ByteOrder.nativeOrder())
        return buffer;

    final ByteBuffer bytes = ByteBuffer.allocateDirect(buffer.capacity());
    bytes.order(ByteOrder.nativeOrder());
    final IntBuffer ints = bytes.asIntBuffer();
    ints.put(buffer).rewind();
    return ints;
}

From source file:com.alvermont.terraj.fracplanet.geom.TriangleBufferArray.java

/** Creates a new instance of TriangleBufferArray */
public TriangleBufferArray() {
    this.buffer = ByteBuffer.allocateDirect(DEFAULT_CAPACITY * INTS_PER_ENTRY * SIZEOF_INT)
            .order(ByteOrder.nativeOrder()).asIntBuffer();
    this.buffer.limit(0);
}

From source file:org.ballerinalang.stdlib.io.data.DataInputOutputTest.java

@Test(description = "Test floating point values", dataProvider = "DoubleValues")
public void testFloat(double value, Representation representation) throws IOException {
    String filePath = currentDirectoryPath + "/sample.bin";
    ByteChannel byteChannel = TestUtil.openForReadingAndWriting(filePath);
    Channel channel = new MockByteChannel(byteChannel);
    DataChannel dataChannel = new DataChannel(channel, ByteOrder.nativeOrder());
    dataChannel.writeDouble(value, representation);
    byteChannel = TestUtil.openForReadingAndWriting(filePath);
    channel = new MockByteChannel(byteChannel);
    dataChannel = new DataChannel(channel, ByteOrder.nativeOrder());
    double readFloat = dataChannel.readDouble(representation);
    Assert.assertEquals(readFloat, value);
}