Example usage for java.nio FloatBuffer get

List of usage examples for java.nio FloatBuffer get

Introduction

In this page you can find the example usage for java.nio FloatBuffer get.

Prototype

public abstract float get(int index);

Source Link

Document

Returns a float at the specified index; the position is not changed.

Usage

From source file:Main.java

public static void main(String[] args) {
    FloatBuffer floatBuffer = FloatBuffer.allocate(10);

    floatBuffer.put(0, 1.23F);//from  www  .  j  a va 2 s.co  m

    floatBuffer.rewind();

    System.out.println(floatBuffer.get(0));

}

From source file:Main.java

public static void main(String[] args) {
    FloatBuffer floatBuffer = FloatBuffer.allocate(10);

    floatBuffer.put(0, 1.23F);/*from w  ww .  j ava 2 s  .  c  o m*/

    floatBuffer.rewind();

    float[] floatArray = new float[10];

    floatBuffer.get(floatArray);

    System.out.println(Arrays.toString(floatArray));

}

From source file:Main.java

public static void copy(final float[] data, final FloatBuffer buf, final int fromPos, final int toPos) {
    buf.position(fromPos);// ww  w .  jav  a 2  s . c om
    buf.get(data);
    buf.position(toPos);
    buf.put(data);
}

From source file:Main.java

/**
 * Copies floats from one position in the buffer to another.
 * /*www .  j a  v  a2s.c  o  m*/
 * @param buf
 *            the buffer to copy from/to
 * @param fromPos
 *            the starting point to copy from
 * @param toPos
 *            the starting point to copy to
 * @param length
 *            the number of floats to copy
 */
public static void copyInternal(FloatBuffer buf, int fromPos, int toPos, int length) {
    float[] data = new float[length];
    buf.position(fromPos);
    buf.get(data);
    buf.position(toPos);
    buf.put(data);
}

From source file:Main.java

public static void copyInternal(final FloatBuffer buf, final int fromPos, final int toPos, final int length) {
    final float[] data = new float[length];
    buf.position(fromPos);/*  w  ww  . j  a va2  s  . co  m*/
    buf.get(data);
    buf.position(toPos);
    buf.put(data);
}

From source file:Main.java

/**
 * Creates a float array from the provided {@link FloatBuffer}.
 * /*  w  ww .  j  a  v a  2 s.co  m*/
 * @param buffer {@link FloatBuffer} the data source.
 * @return float array containing the data of the buffer.
 */
public static float[] getFloatArrayFromBuffer(FloatBuffer buffer) {
    float[] array = null;
    if (buffer.hasArray()) {
        array = buffer.array();
    } else {
        buffer.rewind();
        array = new float[buffer.capacity()];
        buffer.get(array);
    }
    return array;
}

From source file:org.apache.sysml.runtime.matrix.data.LibMatrixNative.java

public static void fromFloatBuffer(FloatBuffer buff, double[] output) {
    Arrays.parallelSetAll(output, i -> (double) buff.get(i));
}

From source file:ubic.gemma.core.loader.expression.arrayDesign.AffyChipTypeExtractor.java

private static String parseGenericCCHeader(DataInputStream str) throws IOException {

    /*/* w ww .j  a  v a  2s  . c o m*/
     * acquisition data, intensity data etc. Usually "intensity data" for the first header.
     */
    String datatypeIdentifier = readString(str);

    AffyChipTypeExtractor.log.debug(datatypeIdentifier);

    String guid = readString(str);

    AffyChipTypeExtractor.log.debug(guid);

    // we just need to read thsee off, even if we aren't using it.
    @SuppressWarnings("unused")
    String createDate = readUnicodeString(str); // blank?
    @SuppressWarnings("unused")
    String locale = readUnicodeString(str); // e.g. en-US
    int numKeyValuePairs = readIntBigEndian(str); // e.g. 55
    String result = null;
    for (int i = 0; i < numKeyValuePairs; i++) {
        String name = readUnicodeString(str);
        byte[] value = readBytes(str);
        String type = readUnicodeString(str);
        Object v;

        switch (type) {
        case "text/x-calvin-float": {
            FloatBuffer intBuf = ByteBuffer.wrap(value).order(ByteOrder.BIG_ENDIAN).asFloatBuffer();
            float[] array = new float[intBuf.remaining()];
            intBuf.get(array);
            break;
        }
        case "text/plain":
        case "text/ascii":
            // text/ascii is undocumented, but needed.
            v = new String(value, "US-ASCII");
            String vv = new String(((String) v).getBytes(), "UTF-16").trim();

            if (name.equals("affymetrix-array-type")) {
                return vv;
            }

            break;
        case "text-x-calvin-unsigned-integer-8": {
            ShortBuffer intBuf = ByteBuffer.wrap(value).asShortBuffer();
            short[] array = new short[intBuf.remaining()];
            intBuf.get(array);

            break;
        }
        case "text/x-calvin-integer-16": {
            IntBuffer intBuf = ByteBuffer.wrap(value).order(ByteOrder.BIG_ENDIAN).asIntBuffer(); // wrong?

            int[] array = new int[intBuf.remaining()];
            intBuf.get(array);
            break;
        }
        case "text/x-calvin-integer-32": {
            IntBuffer intBuf = ByteBuffer.wrap(value).order(ByteOrder.BIG_ENDIAN).asIntBuffer();
            int[] array = new int[intBuf.remaining()];
            intBuf.get(array);

            break;
        }
        case "text/x-calvin-unsigned-integer-8": {
            ShortBuffer intBuf = ByteBuffer.wrap(value).asShortBuffer();
            short[] array = new short[intBuf.remaining()];
            intBuf.get(array);

            break;
        }
        case "text/x-calvin-unsigned-integer-16": {
            IntBuffer intBuf = ByteBuffer.wrap(value).order(ByteOrder.BIG_ENDIAN).asIntBuffer();// wrong?

            int[] array = new int[intBuf.remaining()];
            intBuf.get(array);

            break;
        }
        case "text/x-calvin-unsigned-integer-32": {
            IntBuffer intBuf = ByteBuffer.wrap(value).order(ByteOrder.BIG_ENDIAN).asIntBuffer();
            int[] array = new int[intBuf.remaining()];
            intBuf.get(array);

            break;
        }
        default:
            throw new IOException("Unknown mime type:" + type);
        }

    }

    @SuppressWarnings("unused")
    int numParentHeaders = readIntBigEndian(str);
    return result;
}

From source file:org.mrgeo.data.raster.RasterWritable.java

private static Raster read(final byte[] rasterBytes, Writable payload) throws IOException {
    WritableRaster raster;/*from  w ww  .  j  a  va 2  s.  c  o m*/

    final ByteBuffer rasterBuffer = ByteBuffer.wrap(rasterBytes);

    @SuppressWarnings("unused")
    final int headersize = rasterBuffer.getInt(); // this isn't really used anymore...
    final int height = rasterBuffer.getInt();
    final int width = rasterBuffer.getInt();
    final int bands = rasterBuffer.getInt();
    final int datatype = rasterBuffer.getInt();
    final SampleModelType sampleModelType = SampleModelType.values()[rasterBuffer.getInt()];

    SampleModel model;
    switch (sampleModelType) {
    case BANDED:
        model = new BandedSampleModel(datatype, width, height, bands);
        break;
    case MULTIPIXELPACKED:
        throw new NotImplementedException("MultiPixelPackedSampleModel not implemented yet");
        // model = new MultiPixelPackedSampleModel(dataType, w, h, numberOfBits)
    case PIXELINTERLEAVED: {
        final int pixelStride = rasterBuffer.getInt();
        final int scanlineStride = rasterBuffer.getInt();
        final int bandcnt = rasterBuffer.getInt();
        final int[] bandOffsets = new int[bandcnt];
        for (int i = 0; i < bandcnt; i++) {
            bandOffsets[i] = rasterBuffer.getInt();
        }
        model = new PixelInterleavedSampleModel(datatype, width, height, pixelStride, scanlineStride,
                bandOffsets);
        break;
    }
    case SINGLEPIXELPACKED:
        throw new NotImplementedException("SinglePixelPackedSampleModel not implemented yet");
        // model = new SinglePixelPackedSampleModel(dataType, w, h, bitMasks);
    case COMPONENT: {
        final int pixelStride = rasterBuffer.getInt();
        final int scanlineStride = rasterBuffer.getInt();
        final int bandcnt = rasterBuffer.getInt();
        final int[] bandOffsets = new int[bandcnt];
        for (int i = 0; i < bandcnt; i++) {
            bandOffsets[i] = rasterBuffer.getInt();
        }
        model = new ComponentSampleModel(datatype, width, height, pixelStride, scanlineStride, bandOffsets);
        break;
    }
    default:
        throw new RasterWritableException("Unknown RasterSampleModel type");
    }

    // include the header size param in the count
    int startdata = rasterBuffer.position();

    // calculate the data size
    int[] samplesize = model.getSampleSize();
    int samplebytes = 0;
    for (int ss : samplesize) {
        // bits to bytes
        samplebytes += (ss / 8);
    }
    int databytes = model.getHeight() * model.getWidth() * samplebytes;

    // final ByteBuffer rasterBuffer = ByteBuffer.wrap(rasterBytes, headerbytes, databytes);
    // the corner of the raster is always 0,0
    raster = Raster.createWritableRaster(model, null);

    switch (datatype) {
    case DataBuffer.TYPE_BYTE: {
        // we can't use the byte buffer explicitly because the header info is
        // still in it...
        final byte[] bytedata = new byte[databytes];
        rasterBuffer.get(bytedata);

        raster.setDataElements(0, 0, width, height, bytedata);
        break;
    }
    case DataBuffer.TYPE_FLOAT: {
        final FloatBuffer floatbuff = rasterBuffer.asFloatBuffer();
        final float[] floatdata = new float[databytes / RasterUtils.FLOAT_BYTES];

        floatbuff.get(floatdata);

        raster.setDataElements(0, 0, width, height, floatdata);
        break;
    }
    case DataBuffer.TYPE_DOUBLE: {
        final DoubleBuffer doublebuff = rasterBuffer.asDoubleBuffer();
        final double[] doubledata = new double[databytes / RasterUtils.DOUBLE_BYTES];

        doublebuff.get(doubledata);

        raster.setDataElements(0, 0, width, height, doubledata);

        break;
    }
    case DataBuffer.TYPE_INT: {
        final IntBuffer intbuff = rasterBuffer.asIntBuffer();
        final int[] intdata = new int[databytes / RasterUtils.INT_BYTES];

        intbuff.get(intdata);

        raster.setDataElements(0, 0, width, height, intdata);

        break;
    }
    case DataBuffer.TYPE_SHORT:
    case DataBuffer.TYPE_USHORT: {
        final ShortBuffer shortbuff = rasterBuffer.asShortBuffer();
        final short[] shortdata = new short[databytes / RasterUtils.SHORT_BYTES];
        shortbuff.get(shortdata);
        raster.setDataElements(0, 0, width, height, shortdata);
        break;
    }
    default:
        throw new RasterWritableException("Error trying to read raster.  Bad raster data type");
    }

    // should we even try to extract the payload?
    if (payload != null) {
        // test to see if this is a raster with a possible payload
        final int payloadStart = startdata + databytes;
        if (rasterBytes.length > payloadStart) {
            // extract the payload
            final ByteArrayInputStream bais = new ByteArrayInputStream(rasterBytes, payloadStart,
                    rasterBytes.length - payloadStart);
            final DataInputStream dis = new DataInputStream(bais);
            payload.readFields(dis);
        }
    }
    return raster;
}

From source file:io.anserini.embeddings.WordEmbeddingDictionary.java

public float[] getEmbeddingVector(String term) throws IOException {
    Query query = AnalyzerUtils.buildBagOfWordsQuery(FIELD_ID, analyzer, term);
    TopDocs rs = searcher.search(query, 1);
    ScoredDocuments docs = ScoredDocuments.fromTopDocs(rs, searcher);

    byte[] val = docs.documents[0].getField(FIELD_BODY).binaryValue().bytes;
    FloatBuffer floatBuffer = ByteBuffer.wrap(val).asFloatBuffer();
    float[] floatArray = new float[floatBuffer.limit()];
    floatBuffer.get(floatArray);
    return floatArray;
}