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:ffx.xray.parsers.MTZWriter.java

/**
 * <p>//from   w w w. j  a  va  2  s .  co  m
 * write</p>
 */
public void write() {
    ByteOrder byteOrder = ByteOrder.nativeOrder();
    FileOutputStream fileOutputStream;
    DataOutputStream dataOutputStream;

    try {
        if (logger.isLoggable(Level.INFO)) {
            StringBuilder sb = new StringBuilder();
            sb.append(format("\n Writing MTZ HKL file: \"%s\"\n", fileName));
            logger.info(sb.toString());
        }

        fileOutputStream = new FileOutputStream(fileName);
        dataOutputStream = new DataOutputStream(fileOutputStream);

        byte bytes[] = new byte[80];
        int offset = 0;
        int writeLen = 0;

        int iMapData;
        float fMapData;

        // Header.
        StringBuilder sb = new StringBuilder();
        sb.append("MTZ ");
        dataOutputStream.writeBytes(sb.toString());

        // Header offset.
        int headerOffset = n * nCol + 21;
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
        byteBuffer.order(byteOrder).putInt(headerOffset);

        // Machine code: double, float, int, uchar
        // 0x4441 for LE, 0x1111 for BE
        if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
            iMapData = 0x4441;
        } else {
            iMapData = 0x1111;
        }
        byteBuffer.order(byteOrder).putInt(iMapData);
        dataOutputStream.write(bytes, offset, 8);

        sb = new StringBuilder();
        sb.append(" ");
        sb.setLength(68);
        dataOutputStream.writeBytes(sb.toString());

        // Data.
        Vector<String> colname = new Vector<>(nCol);
        char colType[] = new char[nCol];
        double res[] = new double[2];
        res[0] = Double.POSITIVE_INFINITY;
        res[1] = Double.NEGATIVE_INFINITY;
        float colMinMax[][] = new float[nCol][2];
        for (int i = 0; i < nCol; i++) {
            colMinMax[i][0] = Float.POSITIVE_INFINITY;
            colMinMax[i][1] = Float.NEGATIVE_INFINITY;
        }
        ReflectionSpline sigmaASpline = new ReflectionSpline(reflectionList, refinementData.sigmaa.length);
        int col = 0;

        colname.add("H");
        colType[col++] = 'H';
        colname.add("K");
        colType[col++] = 'H';
        colname.add("L");
        colType[col++] = 'H';
        writeLen += 12;

        if (mtzType != MTZType.FCONLY) {
            colname.add("FO");
            colType[col++] = 'F';
            colname.add("SIGFO");
            colType[col++] = 'Q';
            colname.add("FreeR");
            colType[col++] = 'I';
            writeLen += 12;
        }

        if (mtzType != MTZType.DATAONLY) {
            colname.add("Fs");
            colType[col++] = 'F';
            colname.add("PHIFs");
            colType[col++] = 'P';
            colname.add("Fc");
            colType[col++] = 'F';
            colname.add("PHIFc");
            colType[col++] = 'P';
            writeLen += 16;
        }

        if (mtzType == MTZType.ALL) {
            colname.add("FOM");
            colType[col++] = 'W';
            colname.add("PHIW");
            colType[col++] = 'P';
            colname.add("SigmaAs");
            colType[col++] = 'F';
            colname.add("SigmaAw");
            colType[col++] = 'Q';
            colname.add("FWT");
            colType[col++] = 'F';
            colname.add("PHWT");
            colType[col++] = 'P';
            colname.add("DELFWT");
            colType[col++] = 'F';
            colname.add("PHDELWT");
            colType[col++] = 'P';
            writeLen += 32;
        }

        for (HKL ih : reflectionList.hkllist) {
            col = 0;
            int i = ih.index();

            // Skip the 0 0 0 reflection.
            if (ih.h() == 0 && ih.k() == 0 && ih.l() == 0) {
                continue;
            }

            double ss = Crystal.invressq(crystal, ih);
            res[0] = min(ss, res[0]);
            res[1] = max(ss, res[1]);

            // HKL first (3)
            fMapData = ih.h();
            colMinMax[col][0] = min(fMapData, colMinMax[0][0]);
            colMinMax[col][1] = max(fMapData, colMinMax[0][1]);
            byteBuffer.rewind();
            byteBuffer.order(byteOrder).putFloat(fMapData);
            col++;

            fMapData = ih.k();
            colMinMax[col][0] = min(fMapData, colMinMax[1][0]);
            colMinMax[col][1] = max(fMapData, colMinMax[1][1]);
            byteBuffer.order(byteOrder).putFloat(fMapData);
            col++;

            fMapData = ih.l();
            colMinMax[col][0] = min(fMapData, colMinMax[2][0]);
            colMinMax[col][1] = max(fMapData, colMinMax[2][1]);
            byteBuffer.order(byteOrder).putFloat(fMapData);
            col++;

            if (mtzType != MTZType.FCONLY) {
                // F/sigF (2)
                fMapData = (float) refinementData.getF(i);
                if (!isNaN(fMapData)) {
                    colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
                    colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
                }
                byteBuffer.order(byteOrder).putFloat(fMapData);
                col++;
                fMapData = (float) refinementData.getSigF(i);
                if (!isNaN(fMapData)) {
                    colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
                    colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
                }
                byteBuffer.order(byteOrder).putFloat(fMapData);
                col++;

                // Free R (1)
                fMapData = (float) refinementData.getFreeR(i);
                if (!isNaN(fMapData)) {
                    colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
                    colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
                }
                byteBuffer.order(byteOrder).putFloat(fMapData);
                col++;
            }

            if (mtzType == MTZType.FCONLY) {
                // Fs (2)
                fMapData = (float) refinementData.fsF(i);
                colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
                colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
                byteBuffer.order(byteOrder).putFloat(fMapData);
                col++;
                fMapData = (float) toDegrees(refinementData.fsPhi(i));
                colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
                colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
                byteBuffer.order(byteOrder).putFloat(fMapData);
                col++;

                // Fc (unscaled!) (2)
                fMapData = (float) refinementData.fcF(i);
                if (!isNaN(fMapData)) {
                    colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
                    colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
                }
                byteBuffer.order(byteOrder).putFloat(fMapData);
                col++;
                fMapData = (float) Math.toDegrees(refinementData.fcPhi(i));
                if (!isNaN(fMapData)) {
                    colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
                    colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
                }
                byteBuffer.order(byteOrder).putFloat(fMapData);
                col++;
            }

            if (mtzType == MTZType.ALL) {
                // Fs (2)
                fMapData = (float) refinementData.fsF(i);
                colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
                colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
                byteBuffer.order(byteOrder).putFloat(fMapData);
                col++;
                fMapData = (float) toDegrees(refinementData.fsPhi(i));
                colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
                colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
                byteBuffer.order(byteOrder).putFloat(fMapData);
                col++;

                // Fctot (2)
                fMapData = (float) refinementData.fcTotF(i);
                if (!isNaN(fMapData)) {
                    colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
                    colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
                }
                byteBuffer.order(byteOrder).putFloat(fMapData);
                col++;
                fMapData = (float) toDegrees(refinementData.fcTotPhi(i));
                if (!isNaN(fMapData)) {
                    colMinMax[col][0] = Math.min(fMapData, colMinMax[col][0]);
                    colMinMax[col][1] = Math.max(fMapData, colMinMax[col][1]);
                }
                byteBuffer.order(byteOrder).putFloat(fMapData);
                col++;

                // FOM/phase (2)
                fMapData = (float) refinementData.fomphi[i][0];
                if (!isNaN(fMapData)) {
                    colMinMax[col][0] = Math.min(fMapData, colMinMax[col][0]);
                    colMinMax[col][1] = Math.max(fMapData, colMinMax[col][1]);
                }
                byteBuffer.order(byteOrder).putFloat(fMapData);
                col++;
                fMapData = (float) toDegrees(refinementData.fomphi[i][1]);
                colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
                colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
                byteBuffer.order(byteOrder).putFloat(fMapData);
                col++;

                // Spline setup.
                double fh = spline.f(ss, refinementData.spline);
                double sa = sigmaASpline.f(ss, refinementData.sigmaa);
                double wa = sigmaASpline.f(ss, refinementData.sigmaw);

                // sigmaA/w (2)
                fMapData = (float) sa;
                if (!isNaN(fMapData)) {
                    colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
                    colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
                }
                byteBuffer.order(byteOrder).putFloat(fMapData);
                col++;
                fMapData = (float) wa;
                if (!isNaN(fMapData)) {
                    colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
                    colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
                }
                byteBuffer.order(byteOrder).putFloat(fMapData);
                col++;

                // Map coeffs (4).
                fMapData = (float) refinementData.FoFc2F(i);
                colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
                colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
                byteBuffer.order(byteOrder).putFloat(fMapData);
                col++;
                fMapData = (float) toDegrees(refinementData.FoFc2Phi(i));
                colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
                colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
                byteBuffer.order(byteOrder).putFloat(fMapData);
                col++;
                fMapData = (float) refinementData.foFc1F(i);
                colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
                colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
                byteBuffer.order(byteOrder).putFloat(fMapData);
                col++;
                fMapData = (float) toDegrees(refinementData.foFc1Phi(i));
                colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
                colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
                byteBuffer.order(byteOrder).putFloat(fMapData);
                col++;
            }

            dataOutputStream.write(bytes, offset, writeLen);
        }

        // Header.
        sb = new StringBuilder();
        sb.append("VERS MTZ:V1.1 ");
        while (sb.length() < 80) {
            sb.append(" ");
        }
        dataOutputStream.writeBytes(sb.toString());

        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss ");
        sb = new StringBuilder();
        sb.append("TITLE FFX output: " + sdf.format(now));
        while (sb.length() < 80) {
            sb.append(" ");
        }
        dataOutputStream.writeBytes(sb.toString());

        sb = new StringBuilder();
        sb.append(String.format("NCOL %8d %12d %8d", nCol, n, 0));
        while (sb.length() < 80) {
            sb.append(" ");
        }
        dataOutputStream.writeBytes(sb.toString());

        sb = new StringBuilder();
        sb.append("SORT    0    0    0    0    0 ");
        while (sb.length() < 80) {
            sb.append(" ");
        }
        dataOutputStream.writeBytes(sb.toString());

        sb = new StringBuilder();
        char cdata = spaceGroup.shortName.charAt(0);
        if (cdata == 'H') {
            cdata = 'R';
        }
        sb.append(String.format("SYMINF %3d %2d %c %5d %22s %5s", spaceGroup.getNumberOfSymOps(),
                spaceGroup.numPrimitiveSymEquiv, cdata, spaceGroup.number, "'" + spaceGroup.shortName + "'",
                spaceGroup.pointGroupName));
        while (sb.length() < 80) {
            sb.append(" ");
        }
        dataOutputStream.writeBytes(sb.toString());

        for (int i = 0; i < spaceGroup.symOps.size(); i++) {
            sb = new StringBuilder();
            sb.append("SYMM ");
            SymOp symop = spaceGroup.symOps.get(i);
            sb.append(symop.toXYZString());
            while (sb.length() < 80) {
                sb.append(" ");
            }
            dataOutputStream.writeBytes(sb.toString());
        }

        sb = new StringBuilder();
        sb.append(String.format("RESO %8.6f%13s%8.6f", res[0], " ", res[1]));
        while (sb.length() < 80) {
            sb.append(" ");
        }
        dataOutputStream.writeBytes(sb.toString());

        sb = new StringBuilder();
        sb.append("VALM NAN ");
        while (sb.length() < 80) {
            sb.append(" ");
        }
        dataOutputStream.writeBytes(sb.toString());

        sb = new StringBuilder();
        sb.append("NDIF        1 ");
        while (sb.length() < 80) {
            sb.append(" ");
        }
        dataOutputStream.writeBytes(sb.toString());

        sb = new StringBuilder();
        sb.append("PROJECT       1 project ");
        while (sb.length() < 80) {
            sb.append(" ");
        }
        dataOutputStream.writeBytes(sb.toString());

        sb = new StringBuilder();
        sb.append("CRYSTAL       1 crystal ");
        while (sb.length() < 80) {
            sb.append(" ");
        }
        dataOutputStream.writeBytes(sb.toString());

        sb = new StringBuilder();
        sb.append("DATASET       1 dataset ");
        while (sb.length() < 80) {
            sb.append(" ");
        }
        dataOutputStream.writeBytes(sb.toString());

        for (int j = 0; j < nCol; j++) {
            sb = new StringBuilder();
            sb.append(String.format("COLUMN %-30s %c %17.4f %17.4f    1", colname.get(j), colType[j],
                    colMinMax[j][0], colMinMax[j][1]));
            dataOutputStream.writeBytes(sb.toString());
        }

        sb = new StringBuilder();
        sb.append(String.format("CELL %10.4f %9.4f %9.4f %9.4f %9.4f %9.4f ", crystal.a, crystal.b, crystal.c,
                crystal.alpha, crystal.beta, crystal.gamma));
        while (sb.length() < 80) {
            sb.append(" ");
        }
        dataOutputStream.writeBytes(sb.toString());

        sb = new StringBuilder();
        sb.append(String.format("DCELL %9d %10.4f %9.4f %9.4f %9.4f %9.4f %9.4f ", 1, crystal.a, crystal.b,
                crystal.c, crystal.alpha, crystal.beta, crystal.gamma));
        while (sb.length() < 80) {
            sb.append(" ");
        }
        dataOutputStream.writeBytes(sb.toString());

        sb = new StringBuilder();
        sb.append("DWAVEL        1    1.00000 ");
        while (sb.length() < 80) {
            sb.append(" ");
        }
        dataOutputStream.writeBytes(sb.toString());

        sb = new StringBuilder();
        sb.append("END ");
        while (sb.length() < 80) {
            sb.append(" ");
        }
        dataOutputStream.writeBytes(sb.toString());

        sb = new StringBuilder();
        sb.append("MTZENDOFHEADERS ");
        while (sb.length() < 80) {
            sb.append(" ");
        }
        dataOutputStream.writeBytes(sb.toString());

        dataOutputStream.close();
    } catch (Exception e) {
        String message = "Fatal exception evaluating structure factors.\n";
        logger.log(Level.SEVERE, message, e);
    }
}

From source file:GeometryByReferenceNIOBuffer.java

void createJ3DBuffers() {
    int i;/*from   w w  w  .  j a  v a  2  s .  c  o m*/
    ByteOrder order = ByteOrder.nativeOrder();

    FloatBuffer coord = ByteBuffer.allocateDirect(36 * 4).order(order).asFloatBuffer();
    coord.put(floatVerts, 0, 36);
    floatBufferCoord = new J3DBuffer(coord);

    FloatBuffer color = ByteBuffer.allocateDirect(36 * 4).order(order).asFloatBuffer();
    color.put(floatClrs, 0, 36);
    floatBufferColor = new J3DBuffer(color);

    FloatBuffer indexedCoord = ByteBuffer.allocateDirect(12 * 4).order(order).asFloatBuffer();
    indexedCoord.put(indexedFloatVerts, 0, 12);
    indexedFloatBufferCoord = new J3DBuffer(indexedCoord);

    FloatBuffer indexedColor = ByteBuffer.allocateDirect(12 * 4).order(order).asFloatBuffer();
    indexedColor.put(indexedFloatClrs, 0, 12);
    indexedFloatBufferColor = new J3DBuffer(indexedColor);
}

From source file:org.apache.tez.engine.common.sort.impl.dflt.DefaultSorter.java

@Override
public void initialize(Configuration conf, Master master) throws IOException, InterruptedException {
    if (task == null) {
        LOG.info("Bailing!", new IOException());
        return;/*  w  ww . j a v  a  2s  .  co m*/
    }

    super.initialize(conf, master);

    // sanity checks
    final float spillper = job.getFloat(TezJobConfig.TEZ_ENGINE_SORT_SPILL_PERCENT,
            TezJobConfig.DEFAULT_TEZ_ENGINE_SORT_SPILL_PERCENT);
    final int sortmb = job.getInt(TezJobConfig.TEZ_ENGINE_IO_SORT_MB,
            TezJobConfig.DEFAULT_TEZ_ENGINE_IO_SORT_MB);
    if (spillper > (float) 1.0 || spillper <= (float) 0.0) {
        throw new IOException("Invalid \"" + TezJobConfig.TEZ_ENGINE_SORT_SPILL_PERCENT + "\": " + spillper);
    }
    if ((sortmb & 0x7FF) != sortmb) {
        throw new IOException("Invalid \"" + TezJobConfig.TEZ_ENGINE_IO_SORT_MB + "\": " + sortmb);
    }

    indexCacheMemoryLimit = job.getInt(TezJobConfig.TEZ_ENGINE_INDEX_CACHE_MEMORY_LIMIT_BYTES,
            TezJobConfig.DEFAULT_TEZ_ENGINE_INDEX_CACHE_MEMORY_LIMIT_BYTES);

    // buffers and accounting
    int maxMemUsage = sortmb << 20;
    maxMemUsage -= maxMemUsage % METASIZE;
    kvbuffer = new byte[maxMemUsage];
    bufvoid = kvbuffer.length;
    kvmeta = ByteBuffer.wrap(kvbuffer).order(ByteOrder.nativeOrder()).asIntBuffer();
    setEquator(0);
    bufstart = bufend = bufindex = equator;
    kvstart = kvend = kvindex;

    maxRec = kvmeta.capacity() / NMETA;
    softLimit = (int) (kvbuffer.length * spillper);
    bufferRemaining = softLimit;
    if (LOG.isInfoEnabled()) {
        LOG.info(TezJobConfig.TEZ_ENGINE_IO_SORT_MB + ": " + sortmb);
        LOG.info("soft limit at " + softLimit);
        LOG.info("bufstart = " + bufstart + "; bufvoid = " + bufvoid);
        LOG.info("kvstart = " + kvstart + "; length = " + maxRec);
    }

    // k/v serialization
    valSerializer.open(bb);
    keySerializer.open(bb);

    spillInProgress = false;
    minSpillsForCombine = job.getInt(TezJobConfig.TEZ_ENGINE_COMBINE_MIN_SPILLS, 3);
    spillThread.setDaemon(true);
    spillThread.setName("SpillThread");
    spillLock.lock();
    try {
        spillThread.start();
        while (!spillThreadRunning) {
            spillDone.await();
        }
    } catch (InterruptedException e) {
        throw new IOException("Spill thread failed to initialize", e);
    } finally {
        spillLock.unlock();
    }
    if (sortSpillException != null) {
        throw new IOException("Spill thread failed to initialize", sortSpillException);
    }
}

From source file:com.example.ex_templete.TouchRotateFragment.java

public Cube() {
    int one = 0x10000;
    int vertices[] = { -one, -one, -one, one, -one, -one, one, one, -one, -one, one, -one, -one, -one, one, one,
            -one, one, one, one, one, -one, one, one, };

    int colors[] = { 0, 0, 0, one, one, 0, 0, one, one, one, 0, one, 0, one, 0, one, 0, 0, one, one, one, 0,
            one, one, one, one, one, one, 0, one, one, one, };

    byte indices[] = { 0, 4, 5, 0, 5, 1, 1, 5, 6, 1, 6, 2, 2, 6, 7, 2, 7, 3, 3, 7, 4, 3, 4, 0, 4, 7, 6, 4, 6, 5,
            3, 0, 1, 3, 1, 2 };/*from  w w w.  j  a  v a2  s. c  o m*/

    // Buffers to be passed to gl*Pointer() functions
    // must be direct, i.e., they must be placed on the
    // native heap where the garbage collector cannot
    // move them.
    //
    // Buffers with multi-byte datatypes (e.g., short, int, float)
    // must have their byte order set to native order

    ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    mVertexBuffer = vbb.asIntBuffer();
    mVertexBuffer.put(vertices);
    mVertexBuffer.position(0);

    ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
    cbb.order(ByteOrder.nativeOrder());
    mColorBuffer = cbb.asIntBuffer();
    mColorBuffer.put(colors);
    mColorBuffer.position(0);

    mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
    mIndexBuffer.put(indices);
    mIndexBuffer.position(0);
}

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

/**
 * Chop up the main buffer into smaller buffers that are used to hold
 * each piece of data.//from   w  w w.  j  a v  a  2 s .co m
 *
 * @param capacity The capacity (number of elements it can store) of the buffer
 */
protected void sliceAndDice(int capacity) {
    final int sizeofPositions = capacity * FLOATSIZE * 3;
    final int sizeofNormals = capacity * FLOATSIZE * 3;
    final int sizeofColours = capacity * SIZEOF_COLOUR_ENTRY;
    final int sizeofEmissive = capacity * SIZEOF_EMISSIVE_ENTRY;

    buffer.position(0);
    buffer.limit(sizeofPositions);

    positionBuffer = buffer.slice().order(ByteOrder.nativeOrder()).asFloatBuffer();

    buffer.limit(sizeofPositions + sizeofNormals);
    buffer.position(sizeofPositions);

    normalBuffer = buffer.slice().order(ByteOrder.nativeOrder()).asFloatBuffer();

    buffer.limit(sizeofPositions + sizeofNormals + sizeofColours);
    buffer.position(sizeofPositions + sizeofNormals);

    colourBuffer = buffer.slice().order(ByteOrder.nativeOrder());

    buffer.limit(buffer.capacity());
    buffer.position(sizeofPositions + sizeofNormals + sizeofColours);

    emissiveBuffer = buffer.slice().order(ByteOrder.nativeOrder());

    positionBuffer.limit(0);
    normalBuffer.limit(0);
    colourBuffer.limit(0);
    emissiveBuffer.limit(0);

    buffer.limit(buffer.capacity());
    buffer.rewind();
}

From source file:InterleavedNIOBuffer.java

void createInterleavedBuffers() {
    int size;//from   w  w  w .  j a v a2 s .  c om
    ByteOrder order = ByteOrder.nativeOrder();

    size = (2 + 2 + 3 + 3) * 3 * 4;
    FloatBuffer vertex = ByteBuffer.allocateDirect(size * 4).order(order).asFloatBuffer();
    vertex.put(interleaved, 0, size);
    interleavedBuffer = new J3DBuffer(vertex);

    size = (2 + 2 + 3 + 3) * 4;
    FloatBuffer indexedVertex = ByteBuffer.allocateDirect(size * 4).order(order).asFloatBuffer();
    indexedVertex.put(indexedInterleaved, 0, size);
    indexedInterleavedBuffer = new J3DBuffer(indexedVertex);
}

From source file:org.jtrfp.trcl.core.ResourceManager.java

public ResourceManager(final TR tr) {
    this.tr = tr;
    try {/* w w w . j  a  va2  s.c  o  m*/
        Class.forName("de.quippy.javamod.multimedia.mod.loader.tracker.ProTrackerMod");
        Class.forName("de.quippy.javamod.multimedia.mod.ModContainer"); // ModContainer uses the ModFactory!!
    } catch (Exception e) {
        tr.showStopper(e);
    }
    gpuResidentMODs = new CachedObjectFactory<String, GPUResidentMOD>() {
        @Override
        protected GPUResidentMOD generate(String key) {
            return new GPUResidentMOD(tr, getMOD(key));
        }//end generate(...)
    };
    soundTextures = new CachedObjectFactory<String, SoundTexture>() {
        @Override
        protected SoundTexture generate(String key) {
            try {
                final AudioInputStream ais = AudioSystem
                        .getAudioInputStream(getInputStreamFromResource("SOUND\\" + key));
                final FloatBuffer fb = ByteBuffer.allocateDirect((int) ais.getFrameLength() * 4)
                        .order(ByteOrder.nativeOrder()).asFloatBuffer();
                int value;
                while ((value = ais.read()) != -1) {
                    fb.put(((float) (value - 128)) / 128f);
                }
                fb.clear();
                return tr.soundSystem.get().newSoundTexture(fb, (int) ais.getFormat().getFrameRate());
            } catch (Exception e) {
                tr.showStopper(e);
                return null;
            }
        }
    };

    setupPODListeners();
}

From source file:Main.java

public static ShortBuffer createShortBufferOnHeap(final int size) {
    final ShortBuffer buf = ByteBuffer.allocate(SIZEOF_SHORT * size).order(ByteOrder.nativeOrder())
            .asShortBuffer();/*from ww  w .  ja v a2s.com*/
    buf.clear();
    return buf;
}

From source file:cpcc.ros.services.RosImageConverterTest.java

@Test
public void shouldConvertRGB8Images() throws IOException {
    int height = 240;
    int width = 320;
    int step = 960;
    String encoding = "rgb8";
    String imageName = "data/test-image-rgb8.rgb8";
    String convertedImageName = "data/test-image-rgb8.png";

    InputStream stream = RosImageConverterTest.class.getResourceAsStream(imageName);
    byte[] imageData = IOUtils.toByteArray(stream);

    stream = RosImageConverterTest.class.getResourceAsStream(convertedImageName);
    byte[] convertedImageData = IOUtils.toByteArray(stream);

    when(buffer.array()).thenReturn(imageData);

    when(message.getEncoding()).thenReturn(encoding);
    when(message.getHeight()).thenReturn(height);
    when(message.getWidth()).thenReturn(width);
    when(message.getStep()).thenReturn(step);
    when(message.getData()).thenReturn(ChannelBuffers.copiedBuffer(ByteOrder.nativeOrder(), imageData));

    BufferedImage result = conv.messageToBufferedImage(message);
    assertThat(result).isNotNull();/*from   ww w.  j  av a2s .co m*/
    assertThat(result.getHeight()).isEqualTo(height);
    assertThat(result.getWidth()).isEqualTo(width);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ImageIO.write(result, "PNG", bos);

    assertThat(bos.toByteArray()).isEqualTo(convertedImageData);
}

From source file:com.kentdisplays.synccardboarddemo.MainActivity.java

/**
 * Creates the buffers we use to store information about the 3D world. OpenGL doesn't use Java
 * arrays, but rather needs data in a format it can understand. Hence we use ByteBuffers.
 * @param config The EGL configuration used when creating the surface.
 *//*from  w  ww .  j  a v  a  2  s .c o  m*/
@Override
public void onSurfaceCreated(EGLConfig config) {
    Log.i(TAG, "onSurfaceCreated");

    // make a floor
    ByteBuffer bbFloorVertices = ByteBuffer.allocateDirect(DATA.FLOOR_COORDS.length * 4);
    bbFloorVertices.order(ByteOrder.nativeOrder());
    mFloorVertices = bbFloorVertices.asFloatBuffer();
    mFloorVertices.put(DATA.FLOOR_COORDS);
    mFloorVertices.position(0);

    ByteBuffer bbFloorNormals = ByteBuffer.allocateDirect(DATA.FLOOR_NORMALS.length * 4);
    bbFloorNormals.order(ByteOrder.nativeOrder());
    mFloorNormals = bbFloorNormals.asFloatBuffer();
    mFloorNormals.put(DATA.FLOOR_NORMALS);
    mFloorNormals.position(0);

    ByteBuffer bbFloorColors = ByteBuffer.allocateDirect(DATA.FLOOR_COLORS.length * 4);
    bbFloorColors.order(ByteOrder.nativeOrder());
    mFloorColors = bbFloorColors.asFloatBuffer();
    mFloorColors.put(DATA.FLOOR_COLORS);
    mFloorColors.position(0);

    int vertexShader = loadGLShader(GLES20.GL_VERTEX_SHADER, R.raw.light_vertex);
    int gridShader = loadGLShader(GLES20.GL_FRAGMENT_SHADER, R.raw.grid_fragment);

    mGlProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(mGlProgram, vertexShader);
    GLES20.glAttachShader(mGlProgram, gridShader);
    GLES20.glLinkProgram(mGlProgram);

    GLES20.glEnable(GLES20.GL_DEPTH_TEST);

    Matrix.setIdentityM(mModelFloor, 0);
    Matrix.translateM(mModelFloor, 0, 0, -mFloorDepth, 0); // Floor appears below user

    // Create the placeholder pages.
    mPages = new Page[4];
    mPages[0] = new Page(getResources().openRawResource(R.raw.boogie_board), mGlProgram, 0);
    mPages[1] = new Page(getResources().openRawResource(R.raw.house), mGlProgram, 1);
    mPages[2] = new Page(getResources().openRawResource(R.raw.placeholder), mGlProgram, 2);
    mPages[3] = new Page(getResources().openRawResource(R.raw.cylinder), mGlProgram, 3);

    checkGLError("onSurfaceCreated");
}