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:de.ailis.threedee.utils.BufferUtils.java

/**
 * Creates a direct float buffer with native byte order.
 *
 * @param size/*from  w  w w.  ja  v  a 2 s.  co m*/
 *            The data
 * @return The created direct float buffer
 */

public static FloatBuffer createDirectFloatBuffer(final int size) {
    final ByteBuffer tmp = ByteBuffer.allocateDirect(size * FLOAT_BYTES);
    tmp.order(ByteOrder.nativeOrder());
    return tmp.asFloatBuffer();
}

From source file:Main.java

private static ByteBuffer convertImageData(BufferedImage bufferedImage) {
    ByteBuffer imageBuffer;// www. jav  a2  s . c  o  m
    WritableRaster raster;
    BufferedImage texImage;

    // for a texture
    if (bufferedImage.getColorModel().hasAlpha()) {
        raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bufferedImage.getWidth(),
                bufferedImage.getHeight(), 4, null);
        texImage = new BufferedImage(glAlphaColorModel, raster, false, new Hashtable<Object, Object>());
    } else {
        raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bufferedImage.getWidth(),
                bufferedImage.getHeight(), 3, null);
        texImage = new BufferedImage(glColorModel, raster, false, new Hashtable<Object, Object>());
    }

    // copy the source image into the produced image
    Graphics g = texImage.getGraphics();
    g.setColor(new Color(0f, 0f, 0f, 0f));
    g.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
    g.drawImage(bufferedImage, 0, 0, null);

    // build a byte buffer from the temporary image
    // that be used by OpenGL to produce a texture.
    byte[] data = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();

    imageBuffer = ByteBuffer.allocateDirect(data.length);
    imageBuffer.order(ByteOrder.nativeOrder());
    imageBuffer.put(data, 0, data.length);
    imageBuffer.flip();

    return imageBuffer;
}

From source file:Main.java

public static boolean AV_HAVE_BIGENDIAN() {
    return ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
}

From source file:Main.java

public static FloatBuffer createFloatBuffer(final int size) {
    final FloatBuffer buf = ByteBuffer.allocateDirect(4 * size).order(ByteOrder.nativeOrder()).asFloatBuffer();
    buf.clear();// w w  w . j a v  a  2s  .  c  o  m
    return buf;
}

From source file:Main.java

public static FloatBuffer createFloatBuffer(final int size) {
    final FloatBuffer buf = ByteBuffer.allocateDirect(SIZEOF_FLOAT * size).order(ByteOrder.nativeOrder())
            .asFloatBuffer();//from  www.ja v  a2 s  . c  om
    buf.clear();
    return buf;
}

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

/**
 * Creates a direct short buffer with native byte order.
 *
 * @param size//w  ww . j  a  va 2s .  co m
 *            The data
 * @return The created direct short buffer
 */

public static ShortBuffer createDirectShortBuffer(final int size) {
    final ByteBuffer tmp = ByteBuffer.allocateDirect(size * SHORT_BYTES);
    tmp.order(ByteOrder.nativeOrder());
    return tmp.asShortBuffer();
}

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

/**
 * Creates a direct integer buffer with native byte order.
 *
 * @param size//  w w w  . ja va  2 s .  c  o  m
 *            The data
 * @return The created direct integer buffer
 */

public static IntBuffer createDirectIntegerBuffer(final int size) {
    final ByteBuffer tmp = ByteBuffer.allocateDirect(size * INTEGER_BYTES);
    tmp.order(ByteOrder.nativeOrder());
    return tmp.asIntBuffer();
}

From source file:Triangle.java

public Triangle() {
    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
    mProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(mProgram, vertexShader);
    GLES20.glAttachShader(mProgram, fragmentShader);
    GLES20.glLinkProgram(mProgram);//from   www.  j a  v  a  2  s . c  om

    ByteBuffer bb = ByteBuffer.allocateDirect(triangleCoords.length * 4);
    bb.order(ByteOrder.nativeOrder());

    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(triangleCoords);
    vertexBuffer.position(0);
}

From source file:org.ros.android.rviz_for_android.drawable.loader.StlLoader.java

public void load(InputStream stream) {
    try {//from   w  w  w .j  a va 2  s  . c  o m
        byte[] data = IOUtils.toByteArray(stream);
        in = new ByteArrayInputStream(data);
    } catch (IOException e) {
        e.printStackTrace();
    }
    bb.order(ByteOrder.nativeOrder());

    // Skip 80 byte header
    in.skip(80);

    // Get number of triangles to load
    int nTriangles = getInt();

    vertex = new float[nTriangles * 9];
    normal = new float[nTriangles * 9];

    int vidx = 0;
    int nidx = 0;

    for (int i = 0; i < nTriangles; i++) {
        // Load the normal, check that it's properly formed
        normalVec = new Vector3(getFloat(), getFloat(), getFloat()).normalize();

        // Store the normalized normal
        for (int j = 0; j < 3; j++) {
            normal[nidx++] = (float) normalVec.getX();
            normal[nidx++] = (float) normalVec.getY();
            normal[nidx++] = (float) normalVec.getZ();
        }

        // Load and store the triangle vertices
        // Swap the order if necessary
        for (int b = 0; b < 3; b++) {
            vertexVec[b] = new Vector3(getFloat(), getFloat(), getFloat());
        }
        if (Utility.crossProduct(vertexVec[1].subtract(vertexVec[0]), vertexVec[2].subtract(vertexVec[0]))
                .dotProduct(normalVec) < 0) {
            vertexVec[3] = vertexVec[2];
            vertexVec[2] = vertexVec[1];
            vertexVec[1] = vertexVec[3];
        }
        for (int b = 0; b < 3; b++) {
            vertex[vidx++] = (float) vertexVec[b].getX();
            vertex[vidx++] = (float) vertexVec[b].getY();
            vertex[vidx++] = (float) vertexVec[b].getZ();
        }

        // Skip the footer
        in.skip(2);
    }
}

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

/**
 *
 * @param digest byte array for encoding in base 64,
 * @param  chngByteOrd boolean indicating if to change byte order
 * @return String the encoded base64 of digest
 *//*from   www.ja  va 2 s.  c om*/
public static String tobase64(byte[] digest, boolean chngByteOrd) {

    byte[] tobase64 = null;
    ByteOrder local = ByteOrder.nativeOrder();
    String ordbyte = local.toString();
    mLog.finer("Native byte order is: " + ordbyte);
    ByteBuffer btstream = ByteBuffer.wrap(digest);
    btstream.order(ByteOrder.BIG_ENDIAN);
    byte[] revdigest = null;
    if (chngByteOrd) {
        revdigest = changeByteOrder(digest, local);
    }
    if (revdigest != null) {
        btstream.put(revdigest);
    } else {
        btstream.put(digest);
    }

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

    return new String(tobase64);

}