Example usage for java.nio ByteBuffer putDouble

List of usage examples for java.nio ByteBuffer putDouble

Introduction

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

Prototype

public abstract ByteBuffer putDouble(double value);

Source Link

Document

Writes the given double to the current position and increases the position by 8.

Usage

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;// www . j  a v a  2s  . c o  m
    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:com.diozero.sandpit.imu.invensense.MPU9150DriverMqttPublisher.java

private void mqttPublish(ImuData imu_data, double[] ypr) throws MqttException {
    if (mqttClient != null) {
        MqttMessage message = new MqttMessage();
        message.setQos(MQTT_QOS_AT_MOST_ONCE);

        // 4 sets of 3 doubles, 1 set of 4 doubles
        byte[] bytes = new byte[4 * 3 * 8 + 1 * 4 * 8];
        ByteBuffer buffer = ByteBuffer.wrap(bytes);

        buffer.putDouble(imu_data.getCompass().getX());
        buffer.putDouble(imu_data.getCompass().getY());
        buffer.putDouble(imu_data.getCompass().getZ());

        buffer.putDouble(imu_data.getAccel().getX());
        buffer.putDouble(imu_data.getAccel().getY());
        buffer.putDouble(imu_data.getAccel().getZ());

        buffer.putDouble(imu_data.getGyro().getX());
        buffer.putDouble(imu_data.getGyro().getY());
        buffer.putDouble(imu_data.getGyro().getZ());

        buffer.putDouble(imu_data.getQuaternion().getQ0());
        buffer.putDouble(imu_data.getQuaternion().getQ1());
        buffer.putDouble(imu_data.getQuaternion().getQ2());
        buffer.putDouble(imu_data.getQuaternion().getQ3());

        buffer.putDouble(ypr[0]);/*from  w  w  w.  ja v a2 s  .c om*/
        buffer.putDouble(ypr[1]);
        buffer.putDouble(ypr[2]);

        buffer.flip();
        message.setPayload(bytes);
        mqttClient.publish(MQTT_TOPIC_IMU, message);
    }
}

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:hivemall.mf.OnlineMatrixFactorizationUDTF.java

protected void beforeTrain(final long rowNum, final int user, final int item, final double rating)
        throws HiveException {
    if (inputBuf != null) {
        assert (fileIO != null);
        final ByteBuffer buf = inputBuf;
        int remain = buf.remaining();
        if (remain < RECORD_BYTES) {
            writeBuffer(buf, fileIO, lastWritePos);
            this.lastWritePos = rowNum;
        }/*from w  w w.  ja  va  2  s  . c o  m*/
        buf.putInt(user);
        buf.putInt(item);
        buf.putDouble(rating);
    }
}

From source file:hivemall.fm.FactorizationMachineUDTF.java

protected void recordTrain(@Nonnull final Feature[] x, final double y) throws HiveException {
    if (_iterations <= 1) {
        return;//www  . ja v  a2s.c  o m
    }

    ByteBuffer inputBuf = _inputBuf;
    NioStatefullSegment dst = _fileIO;
    if (inputBuf == null) {
        final File file;
        try {
            file = File.createTempFile("hivemall_fm", ".sgmt");
            file.deleteOnExit();
            if (!file.canWrite()) {
                throw new UDFArgumentException("Cannot write a temporary file: " + file.getAbsolutePath());
            }
            LOG.info("Record training examples to a file: " + file.getAbsolutePath());
        } catch (IOException ioe) {
            throw new UDFArgumentException(ioe);
        } catch (Throwable e) {
            throw new UDFArgumentException(e);
        }

        this._inputBuf = inputBuf = ByteBuffer.allocateDirect(1024 * 1024); // 1 MiB
        this._fileIO = dst = new NioStatefullSegment(file, false);
    }

    int xBytes = Feature.requiredBytes(x);
    int recordBytes = (Integer.SIZE + Double.SIZE) / 8 + xBytes;
    int requiredBytes = (Integer.SIZE / 8) + recordBytes;
    int remain = inputBuf.remaining();
    if (remain < requiredBytes) {
        writeBuffer(inputBuf, dst);
    }

    inputBuf.putInt(recordBytes);
    inputBuf.putInt(x.length);
    for (Feature f : x) {
        f.writeTo(inputBuf);
    }
    inputBuf.putDouble(y);
}

From source file:org.apache.carbondata.core.indexstore.blockletindex.BlockletDataMap.java

/**
 * Fill the measures min values with minimum , this is needed for backward version compatability
 * as older versions don't store min values for measures
 *//*ww  w .  j a  v  a2s .  co  m*/
private byte[][] updateMinValues(byte[][] minValues, int[] minMaxLen) {
    byte[][] updatedValues = minValues;
    if (minValues.length < minMaxLen.length) {
        updatedValues = new byte[minMaxLen.length][];
        System.arraycopy(minValues, 0, updatedValues, 0, minValues.length);
        List<CarbonMeasure> measures = segmentProperties.getMeasures();
        ByteBuffer buffer = ByteBuffer.allocate(8);
        for (int i = 0; i < measures.size(); i++) {
            buffer.rewind();
            DataType dataType = measures.get(i).getDataType();
            if (dataType == DataTypes.BYTE) {
                buffer.putLong(Byte.MIN_VALUE);
                updatedValues[minValues.length + i] = buffer.array().clone();
            } else if (dataType == DataTypes.SHORT) {
                buffer.putLong(Short.MIN_VALUE);
                updatedValues[minValues.length + i] = buffer.array().clone();
            } else if (dataType == DataTypes.INT) {
                buffer.putLong(Integer.MIN_VALUE);
                updatedValues[minValues.length + i] = buffer.array().clone();
            } else if (dataType == DataTypes.LONG) {
                buffer.putLong(Long.MIN_VALUE);
                updatedValues[minValues.length + i] = buffer.array().clone();
            } else if (DataTypes.isDecimal(dataType)) {
                updatedValues[minValues.length + i] = DataTypeUtil
                        .bigDecimalToByte(BigDecimal.valueOf(Long.MIN_VALUE));
            } else {
                buffer.putDouble(Double.MIN_VALUE);
                updatedValues[minValues.length + i] = buffer.array().clone();
            }
        }
    }
    return updatedValues;
}

From source file:org.apache.carbondata.core.indexstore.blockletindex.BlockletDataMap.java

/**
 * Fill the measures max values with maximum , this is needed for backward version compatability
 * as older versions don't store max values for measures
 *//*  www  .j a v a 2s  . c o  m*/
private byte[][] updateMaxValues(byte[][] maxValues, int[] minMaxLen) {
    byte[][] updatedValues = maxValues;
    if (maxValues.length < minMaxLen.length) {
        updatedValues = new byte[minMaxLen.length][];
        System.arraycopy(maxValues, 0, updatedValues, 0, maxValues.length);
        List<CarbonMeasure> measures = segmentProperties.getMeasures();
        ByteBuffer buffer = ByteBuffer.allocate(8);
        for (int i = 0; i < measures.size(); i++) {
            buffer.rewind();
            DataType dataType = measures.get(i).getDataType();
            if (dataType == DataTypes.BYTE) {
                buffer.putLong(Byte.MAX_VALUE);
                updatedValues[maxValues.length + i] = buffer.array().clone();
            } else if (dataType == DataTypes.SHORT) {
                buffer.putLong(Short.MAX_VALUE);
                updatedValues[maxValues.length + i] = buffer.array().clone();
            } else if (dataType == DataTypes.INT) {
                buffer.putLong(Integer.MAX_VALUE);
                updatedValues[maxValues.length + i] = buffer.array().clone();
            } else if (dataType == DataTypes.LONG) {
                buffer.putLong(Long.MAX_VALUE);
                updatedValues[maxValues.length + i] = buffer.array().clone();
            } else if (DataTypes.isDecimal(dataType)) {
                updatedValues[maxValues.length + i] = DataTypeUtil
                        .bigDecimalToByte(BigDecimal.valueOf(Long.MAX_VALUE));
            } else {
                buffer.putDouble(Double.MAX_VALUE);
                updatedValues[maxValues.length + i] = buffer.array().clone();
            }
        }
    }
    return updatedValues;
}

From source file:com.healthmarketscience.jackcess.Column.java

/**
 * Writes a date value./*from w  w w  . j  av a2  s . c om*/
 */
private void writeDateValue(ByteBuffer buffer, Object value) {
    if (value == null) {
        buffer.putDouble(0d);
    } else if (value instanceof DateExt) {

        // this is a Date value previously read from readDateValue().  use the
        // original bits to store the value so we don't lose any precision
        buffer.putLong(((DateExt) value).getDateBits());

    } else {

        buffer.putDouble(toDateDouble(value));
    }
}

From source file:com.healthmarketscience.jackcess.impl.ColumnImpl.java

protected ByteBuffer writeFixedLengthField(Object obj, ByteBuffer buffer) throws IOException {
    // since booleans are not written by this method, it's safe to convert any
    // incoming boolean into an integer.
    obj = booleanToInteger(obj);/*from   w  w w  .j  a va  2 s.  co  m*/

    switch (getType()) {
    case BOOLEAN:
        //Do nothing
        break;
    case BYTE:
        buffer.put(toNumber(obj).byteValue());
        break;
    case INT:
        buffer.putShort(toNumber(obj).shortValue());
        break;
    case LONG:
        buffer.putInt(toNumber(obj).intValue());
        break;
    case MONEY:
        writeCurrencyValue(buffer, obj);
        break;
    case FLOAT:
        buffer.putFloat(toNumber(obj).floatValue());
        break;
    case DOUBLE:
        buffer.putDouble(toNumber(obj).doubleValue());
        break;
    case SHORT_DATE_TIME:
        writeDateValue(buffer, obj);
        break;
    case TEXT:
        // apparently text numeric values are also occasionally written as fixed
        // length...
        int numChars = getLengthInUnits();
        // force uncompressed encoding for fixed length text
        buffer.put(encodeTextValue(obj, numChars, numChars, true));
        break;
    case GUID:
        writeGUIDValue(buffer, obj);
        break;
    case NUMERIC:
        // yes, that's right, occasionally numeric values are written as fixed
        // length...
        writeNumericValue(buffer, obj);
        break;
    case BINARY:
    case UNKNOWN_0D:
    case UNKNOWN_11:
    case COMPLEX_TYPE:
        buffer.putInt(toNumber(obj).intValue());
        break;
    case UNSUPPORTED_FIXEDLEN:
        byte[] bytes = toByteArray(obj);
        if (bytes.length != getLength()) {
            throw new IOException(
                    "Invalid fixed size binary data, size " + getLength() + ", got " + bytes.length);
        }
        buffer.put(bytes);
        break;
    default:
        throw new IOException("Unsupported data type: " + getType());
    }
    return buffer;
}

From source file:com.healthmarketscience.jackcess.Column.java

/**
 * Serialize an Object into a raw byte value for this column
 * @param obj Object to serialize//from ww  w. jav  a  2 s  . co m
 * @param order Order in which to serialize
 * @return A buffer containing the bytes
 * @usage _advanced_method_
 */
public ByteBuffer writeFixedLengthField(Object obj, ByteOrder order) throws IOException {
    int size = getType().getFixedSize(_columnLength);

    // create buffer for data
    ByteBuffer buffer = getPageChannel().createBuffer(size, order);

    // since booleans are not written by this method, it's safe to convert any
    // incoming boolean into an integer.
    obj = booleanToInteger(obj);

    switch (getType()) {
    case BOOLEAN:
        //Do nothing
        break;
    case BYTE:
        buffer.put(toNumber(obj).byteValue());
        break;
    case INT:
        buffer.putShort(toNumber(obj).shortValue());
        break;
    case LONG:
        buffer.putInt(toNumber(obj).intValue());
        break;
    case MONEY:
        writeCurrencyValue(buffer, obj);
        break;
    case FLOAT:
        buffer.putFloat(toNumber(obj).floatValue());
        break;
    case DOUBLE:
        buffer.putDouble(toNumber(obj).doubleValue());
        break;
    case SHORT_DATE_TIME:
        writeDateValue(buffer, obj);
        break;
    case TEXT:
        // apparently text numeric values are also occasionally written as fixed
        // length...
        int numChars = getLengthInUnits();
        // force uncompressed encoding for fixed length text
        buffer.put(encodeTextValue(obj, numChars, numChars, true));
        break;
    case GUID:
        writeGUIDValue(buffer, obj, order);
        break;
    case NUMERIC:
        // yes, that's right, occasionally numeric values are written as fixed
        // length...
        writeNumericValue(buffer, obj);
        break;
    case BINARY:
    case UNKNOWN_0D:
    case UNKNOWN_11:
    case COMPLEX_TYPE:
        buffer.putInt(toNumber(obj).intValue());
        break;
    case UNSUPPORTED_FIXEDLEN:
        byte[] bytes = toByteArray(obj);
        if (bytes.length != getLength()) {
            throw new IOException(
                    "Invalid fixed size binary data, size " + getLength() + ", got " + bytes.length);
        }
        buffer.put(bytes);
        break;
    default:
        throw new IOException("Unsupported data type: " + getType());
    }
    buffer.flip();
    return buffer;
}