Example usage for java.nio ByteBuffer putFloat

List of usage examples for java.nio ByteBuffer putFloat

Introduction

In this page you can find the example usage for java.nio ByteBuffer putFloat.

Prototype

public abstract ByteBuffer putFloat(float value);

Source Link

Document

Writes the given float to the current position and increases the position by 4.

Usage

From source file:au.org.ala.layers.grid.GridCacheBuilder.java

private static void writeGroupGRI(File file, ArrayList<Grid> group) {
    Grid g = group.get(0);//  www  .j  av  a  2  s . c  o m
    RandomAccessFile[] raf = new RandomAccessFile[group.size()];
    RandomAccessFile output = null;

    try {
        output = new RandomAccessFile(file, "rw");

        for (int i = 0; i < group.size(); i++) {
            raf[i] = new RandomAccessFile(group.get(i).filename + ".gri", "r");
        }

        int length = g.ncols * g.nrows;
        int size = 4;
        byte[] b = new byte[size * group.size() * g.ncols];
        float noDataValue = Float.MAX_VALUE * -1;

        byte[] bi = new byte[g.ncols * 8];
        float[][] rows = new float[group.size()][g.ncols];

        for (int i = 0; i < g.nrows; i++) {
            //read
            for (int j = 0; j < raf.length; j++) {
                nextRowOfFloats(rows[j], group.get(j).datatype, group.get(j).byteorderLSB, g.ncols, raf[j], bi,
                        (float) g.nodatavalue);
            }

            //write
            ByteBuffer bb = ByteBuffer.wrap(b);
            bb.order(ByteOrder.LITTLE_ENDIAN);

            for (int k = 0; k < g.ncols; k++) {
                for (int j = 0; j < raf.length; j++) {
                    //float f = getNextValue(raf[j], group.get(j));
                    float f = rows[j][k];
                    if (Float.isNaN(f)) {
                        bb.putFloat(noDataValue);
                    } else {
                        bb.putFloat(f);
                    }
                }
            }
            output.write(b);
        }
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
        for (int i = 0; i < raf.length; i++) {
            if (raf[i] != null) {
                try {
                    raf[i].close();
                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                }
            }
        }
    }
}

From source file:org.getspout.spoutapi.packet.PacketBlockData.java

public PacketBlockData(Set<Block> modifiedData) {
    if (modifiedData.size() > 0) {
        ByteBuffer rawData = ByteBuffer.allocate(modifiedData.size() * (15));

        Iterator<Block> i = modifiedData.iterator();
        while (i.hasNext()) {
            Block next = i.next();// w ww .  j a v a 2 s. c  om

            rawData.put((byte) next.getRawId());
            rawData.put((byte) next.getRawData());
            rawData.putFloat(next.getHardness());
            rawData.putInt(next.getLightLevel());
            rawData.putFloat(next.getFriction());
            rawData.put((byte) (next.isOpaque() ? 1 : 0));
        }

        data = rawData.array();
    }
}

From source file:indexer.DocVector.java

byte[] getVecBytes(float[] x) {
    ByteBuffer buff = ByteBuffer.allocate(Float.SIZE * x.length);
    for (float xval : x)
        buff.putFloat(xval);

    byte[] bytes = buff.array();
    return bytes;
}

From source file:au.org.ala.delta.io.BinaryKeyFile.java

public int writeFloatsToRecord(int recordNumber, List<Float> values) {
    ByteBuffer bytes = ByteBuffer.allocate(values.size() * SIZE_INT_IN_BYTES);
    bytes.order(ByteOrder.LITTLE_ENDIAN);
    for (float value : values) {
        bytes.putFloat(value);
    }//from  w w w.  j av  a2  s  .c  o m
    return writeToRecord(recordNumber, 0, bytes.array());
}

From source file:edu.umn.cs.spatialHadoop.visualization.FrequencyMap.java

@Override
public void write(DataOutput out) throws IOException {
    super.write(out);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = new GZIPOutputStream(baos);
    ByteBuffer bbuffer = ByteBuffer.allocate(getHeight() * 4 + 8);
    bbuffer.putInt(getWidth());/*from  w w  w.j a  va2  s. c o m*/
    bbuffer.putInt(getHeight());
    gzos.write(bbuffer.array(), 0, bbuffer.position());
    for (int x = 0; x < getWidth(); x++) {
        bbuffer.clear();
        for (int y = 0; y < getHeight(); y++) {
            bbuffer.putFloat(frequencies[x][y]);
        }
        gzos.write(bbuffer.array(), 0, bbuffer.position());
    }
    gzos.close();

    byte[] serializedData = baos.toByteArray();
    out.writeInt(serializedData.length);
    out.write(serializedData);
}

From source file:com.abs.mdu.bluetoothchat.BluetoothChatFragment.java

private void sendMsg(float progress, byte cmd) throws IOException {

    String s = "";

    byte[] temperature_msg = new byte[11];
    Arrays.fill(temperature_msg, (byte) 0);
    ByteBuffer buffer = ByteBuffer.allocate(4);
    buffer.putFloat(progress);
    byte[] byte_progress = new byte[4];
    ;/*from   ww  w  . j  av  a2s .  co m*/
    Arrays.fill(byte_progress, (byte) 0);
    byte_progress = buffer.array();

    temperature_msg[0] = start_byte;
    //length in the end
    temperature_msg[2] = opc;
    temperature_msg[3] = (byte) id;
    temperature_msg[4] = cmd;
    temperature_msg[5] = status;

    temperature_msg[6] = (byte) byte_progress[3];
    temperature_msg[7] = (byte) byte_progress[2];
    temperature_msg[8] = (byte) byte_progress[1];
    temperature_msg[9] = (byte) byte_progress[0];
    temperature_msg[1] = (byte) (temperature_msg.length - 2);

    byte crc, i;
    crc = 0;
    for (i = 0; i < temperature_msg.length; i++) {
        crc ^= temperature_msg[i];
    }

    temperature_msg[10] = ((byte) crc);

    /*        Charset mycharset = defaultCharset();
    //length must be in the end, when msg is completely build;
            
    //best working, only missing negative charaters thar are trasnlated into more the one bytes
    String temperature_msg_string = new String(temperature_msg, mycharset); //must put the correct encoding for the negatives
    if(temperature_msg_string.length() != 11 ) {
        temperature_msg_string = "Error size = " + temperature_msg_string.length();
    }
    //mOutEditText.setText(temperature_msg_string);
    */

    mChatService.write(temperature_msg);

    // Reset out string buffer to zero and clear the edit text field
    mOutStringBuffer.setLength(0);
    mOutEditText.setText(mOutStringBuffer);

    id++;

    //mSendButton.callOnClick();
}

From source file:com.linkedin.pinot.core.startree.MmapLinkedListStarTreeTable.java

@Override
protected ByteBuffer toByteBuffer(StarTreeTableRow row) {
    ByteBuffer buffer = getNextBuffer();

    for (int i = 0; i < dimensionTypes.size(); i++) {
        buffer.putInt(row.getDimensions().get(i));
    }//from   w  ww. ja  v  a  2 s. c o  m

    for (int i = 0; i < metricTypes.size(); i++) {
        switch (metricTypes.get(i)) {
        case SHORT:
            buffer.putShort(row.getMetrics().get(i).shortValue());
            break;
        case INT:
            buffer.putInt(row.getMetrics().get(i).intValue());
            break;
        case LONG:
            buffer.putLong(row.getMetrics().get(i).longValue());
            break;
        case FLOAT:
            buffer.putFloat(row.getMetrics().get(i).floatValue());
            break;
        case DOUBLE:
            buffer.putDouble(row.getMetrics().get(i).doubleValue());
            break;
        default:
            throw new IllegalArgumentException("Unsupported metric type " + metricTypes.get(i));
        }
    }

    return buffer;
}

From source file:org.deegree.tools.rendering.dem.builder.DEMDatasetGenerator.java

private void put(ByteBuffer buffer, float value, int bytesPerValue) {
    if (bytesPerValue == 4) {
        buffer.putFloat(value);
    } else if (bytesPerValue == 2) {
        short s = 0;
        if (value < 1) {
            s = (short) (32768.0f * value);
        } else {/*w w w. j a v a 2 s .c  o m*/
            s = (short) (32767.0f * value);
        }
        buffer.putShort(s);
    } else if (bytesPerValue == 1) {
        byte b = 0;
        if (value < 1) {
            b = (byte) (128.0f * value);
        } else {
            b = (byte) (127.0f * value);
        }
        buffer.put(b);
    }
}

From source file:org.wso2.carbon.analytics.datasource.core.util.GenericUtils.java

public static byte[] encodeElement(String name, Object value) throws AnalyticsException {
    ByteBuffer buffer = ByteBuffer.allocate(calculateBufferSizePerElement(name, value));
    String strVal;//from   w  ww. java2  s  .  c  om
    boolean boolVal;
    byte[] binData;

    buffer.putInt(name.getBytes(StandardCharsets.UTF_8).length);
    buffer.put(name.getBytes(StandardCharsets.UTF_8));
    if (value instanceof String) {
        buffer.put(DATA_TYPE_STRING);
        strVal = (String) value;
        buffer.putInt(strVal.getBytes(StandardCharsets.UTF_8).length);
        buffer.put(strVal.getBytes(StandardCharsets.UTF_8));
    } else if (value instanceof Long) {
        buffer.put(DATA_TYPE_LONG);
        buffer.putLong((Long) value);
    } else if (value instanceof Double) {
        buffer.put(DATA_TYPE_DOUBLE);
        buffer.putDouble((Double) value);
    } else if (value instanceof Boolean) {
        buffer.put(DATA_TYPE_BOOLEAN);
        boolVal = (Boolean) value;
        if (boolVal) {
            buffer.put(BOOLEAN_TRUE);
        } else {
            buffer.put(BOOLEAN_FALSE);
        }
    } else if (value instanceof Integer) {
        buffer.put(DATA_TYPE_INTEGER);
        buffer.putInt((Integer) value);
    } else if (value instanceof Float) {
        buffer.put(DATA_TYPE_FLOAT);
        buffer.putFloat((Float) value);
    } else if (value instanceof byte[]) {
        buffer.put(DATA_TYPE_BINARY);
        binData = (byte[]) value;
        buffer.putInt(binData.length);
        buffer.put(binData);
    } else if (value == null) {
        buffer.put(DATA_TYPE_NULL);
    } else {
        buffer.put(DATA_TYPE_OBJECT);
        binData = GenericUtils.serializeObject(value);
        buffer.putInt(binData.length);
        buffer.put(binData);
    }

    return buffer.array();
}

From source file:hivemall.recommend.SlimUDTF.java

private void recordTrainingInput(final int itemI, @Nonnull final Int2ObjectMap<Int2FloatMap> knnItems,
        final int numKNNItems) throws HiveException {
    ByteBuffer buf = this._inputBuf;
    NioStatefulSegment dst = this._fileIO;

    if (buf == null) {
        // invoke only at task node (initialize is also invoked in compilation)
        final File file;
        try {// ww  w . java  2s .c o  m
            file = File.createTempFile("hivemall_slim", ".sgmt"); // to save KNN data
            file.deleteOnExit();
            if (!file.canWrite()) {
                throw new UDFArgumentException("Cannot write a temporary file: " + file.getAbsolutePath());
            }
        } catch (IOException ioe) {
            throw new UDFArgumentException(ioe);
        }

        this._inputBuf = buf = ByteBuffer.allocateDirect(8 * 1024 * 1024); // 8MB
        this._fileIO = dst = new NioStatefulSegment(file, false);
    }

    int recordBytes = SizeOf.INT + SizeOf.INT + SizeOf.INT * 2 * knnItems.size()
            + (SizeOf.INT + SizeOf.FLOAT) * numKNNItems;
    int requiredBytes = SizeOf.INT + recordBytes; // need to allocate space for "recordBytes" itself

    int remain = buf.remaining();
    if (remain < requiredBytes) {
        writeBuffer(buf, dst);
    }

    buf.putInt(recordBytes);
    buf.putInt(itemI);
    buf.putInt(knnItems.size());

    for (Int2ObjectMap.Entry<Int2FloatMap> e1 : Fastutil.fastIterable(knnItems)) {
        int user = e1.getIntKey();
        buf.putInt(user);

        Int2FloatMap ru = e1.getValue();
        buf.putInt(ru.size());
        for (Int2FloatMap.Entry e2 : Fastutil.fastIterable(ru)) {
            buf.putInt(e2.getIntKey());
            buf.putFloat(e2.getFloatValue());
        }
    }
}