Example usage for java.nio ByteBuffer getDouble

List of usage examples for java.nio ByteBuffer getDouble

Introduction

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

Prototype

public abstract double getDouble();

Source Link

Document

Returns the double at the current position and increases the position by 8.

Usage

From source file:ConversionUtil.java

public static double convertToDouble(byte[] array) {
    ByteBuffer buffer = ByteBuffer.wrap(array);
    return buffer.getDouble();
    /*//from w w  w. ja v a 2 s . c om
    long longValue = convertToLong(array);
    return Double.longBitsToDouble(longValue);
     */
}

From source file:com.act.lcms.MzMLParser.java

protected static List<Double> base64ToDoubleList(String b64) {
    byte[] decodedBytes = Base64.getDecoder().decode(b64);
    ByteBuffer buf = ByteBuffer.wrap(decodedBytes).order(ByteOrder.LITTLE_ENDIAN);
    List<Double> values = new ArrayList<>(decodedBytes.length / 8);
    while (buf.hasRemaining()) {
        values.add(buf.getDouble());
    }//w ww . jav  a 2 s. c  o m
    return values;
}

From source file:org.opensha.commons.util.XMLUtils.java

public static double[] doubleArrayFromXML(Element doubleArrayEl) {
    byte[] data = byteArrayFromXML(doubleArrayEl);

    Preconditions.checkState(data.length % 8 == 0, "binary data not a multiple of 8 bits");
    int size = data.length / 8;

    ByteBuffer buf = ByteBuffer.wrap(data);
    double[] array = new double[size];
    for (int i = 0; i < size; i++)
        array[i] = buf.getDouble();

    return array;
}

From source file:edu.umn.msi.tropix.proteomics.conversion.impl.ConversionUtils.java

public static double[] extractDoublesLittleEndian(final byte[] bytes, final boolean is64Bit) {
    double[] doubles;
    ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    if (is64Bit) {
        doubles = new double[bytes.length / 8];
        for (int i = 0; i < doubles.length; i++) {
            doubles[i] = byteBuffer.getDouble();
        }//from   w ww  .java2s.  c o  m
    } else {
        doubles = new double[bytes.length / 4];
        for (int i = 0; i < doubles.length; i++) {
            doubles[i] = byteBuffer.getFloat();
        }
    }
    return doubles;
}

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

static public float getNextValue(RandomAccessFile raf, Grid g) {
    float f = Float.NaN;
    try {//from w  ww  .ja  va  2  s .  c  o  m
        int size;
        byte[] b;
        if (g.datatype.charAt(0) == 'B') {//"BYTE")) {
            f = raf.readByte();
        } else if (g.datatype.charAt(0) == 'U') {//equalsIgnoreCase("UBYTE")) {
            f = raf.readByte();
            if (f < 0) {
                f += 256;
            }
        } else if (g.datatype.charAt(0) == 'S') {//equalsIgnoreCase("SHORT")) {
            size = 2;
            b = new byte[size];
            raf.read(b);
            if (g.byteorderLSB) {
                f = (short) (((0xFF & b[1]) << 8) | (b[0] & 0xFF));
            } else {
                f = (short) (((0xFF & b[0]) << 8) | (b[1] & 0xFF));
            }
        } else if (g.datatype.charAt(0) == 'I') {//equalsIgnoreCase("INT")) {
            size = 4;
            b = new byte[size];
            raf.read(b);
            if (g.byteorderLSB) {
                f = ((0xFF & b[3]) << 24) | ((0xFF & b[2]) << 16) + ((0xFF & b[1]) << 8) + (b[0] & 0xFF);
            } else {
                f = ((0xFF & b[0]) << 24)
                        | ((0xFF & b[1]) << 16) + ((0xFF & b[2]) << 8) + ((0xFF & b[3]) & 0xFF);
            }
        } else if (g.datatype.charAt(0) == 'L') {//equalsIgnoreCase("LONG")) {
            size = 8;
            b = new byte[size];
            raf.read(b);
            if (g.byteorderLSB) {
                f = ((long) (0xFF & b[7]) << 56) + ((long) (0xFF & b[6]) << 48) + ((long) (0xFF & b[5]) << 40)
                        + ((long) (0xFF & b[4]) << 32) + ((long) (0xFF & b[3]) << 24)
                        + ((long) (0xFF & b[2]) << 16) + ((long) (0xFF & b[1]) << 8) + (0xFF & b[0]);
            } else {
                f = ((long) (0xFF & b[0]) << 56) + ((long) (0xFF & b[1]) << 48) + ((long) (0xFF & b[2]) << 40)
                        + ((long) (0xFF & b[3]) << 32) + ((long) (0xFF & b[4]) << 24)
                        + ((long) (0xFF & b[5]) << 16) + ((long) (0xFF & b[6]) << 8) + (0xFF & b[7]);
            }
        } else if (g.datatype.charAt(0) == 'F') {//.equalsIgnoreCase("FLOAT")) {
            size = 4;
            b = new byte[size];
            raf.read(b);
            ByteBuffer bb = ByteBuffer.wrap(b);
            if (g.byteorderLSB) {
                bb.order(ByteOrder.LITTLE_ENDIAN);
            }
            f = bb.getFloat();

        } else if (g.datatype.charAt(0) == 'D') {//.equalsIgnoreCase("DOUBLE")) {
            size = 8;
            b = new byte[8];
            raf.read(b);
            ByteBuffer bb = ByteBuffer.wrap(b);
            if (g.byteorderLSB) {
                bb.order(ByteOrder.LITTLE_ENDIAN);
            }
            f = (float) bb.getDouble();
        }
        //replace not a number            
        if ((float) f == (float) g.nodatavalue) {
            f = Float.NaN;
        }
    } catch (Exception e) {
    }
    return f;
}

From source file:org.mcisb.util.math.MathUtils.java

/**
 * //from  w  ww. j av  a 2  s. co m
 * @param encoded
 * @param bigEndian
 * @param doublePrecision
 * @return double[]
 */
public static double[] decode(final byte[] encoded, final boolean bigEndian, final boolean doublePrecision) {
    final byte[] bytes = Base64.decode(encoded);
    ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
    byteBuffer = byteBuffer.order(bigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);

    final int limit = byteBuffer.limit();
    double[] decoded = new double[((doublePrecision) ? limit / DOUBLE_LENGTH : limit / FLOAT_LENGTH)];
    int i = 0;

    while (byteBuffer.hasRemaining()) {
        if (doublePrecision) {
            decoded[i++] = byteBuffer.getDouble();
        } else {
            decoded[i++] = byteBuffer.getFloat();
        }
    }

    return decoded;
}

From source file:com.netflix.aegisthus.pig.AegisthusLoadCaster.java

@Override
public Double bytesToDouble(byte[] arg0) throws IOException {
    if (arg0 == null || arg0.length == 0) {
        return null;
    }// ww  w  .  j  av a2 s. co m
    try {
        byte[] by = hex.decode(arg0);
        ByteBuffer bb = ByteBuffer.allocate(by.length);
        bb.put(by);
        bb.position(0);
        return bb.getDouble();
    } catch (Exception e) {
        LOG.error("failed to convert " + new String(arg0) + " to double");
        return null;
    }
}

From source file:edu.umn.cs.spatialHadoop.indexing.BTRPartitioner.java

@Override
public void readFields(DataInput in) throws IOException {
    mbr.readFields(in);/*  www  .j  a va  2  s .c o m*/
    columns = in.readInt();
    rows = in.readInt();
    xSplits = new double[columns];
    ySplits = new double[columns * rows];

    int bufferLength = (xSplits.length + ySplits.length) * 8;
    byte[] buffer = new byte[bufferLength];
    in.readFully(buffer);
    ByteBuffer bbuffer = ByteBuffer.wrap(buffer);
    for (int i = 0; i < xSplits.length; i++)
        xSplits[i] = bbuffer.getDouble();
    for (int i = 0; i < ySplits.length; i++)
        ySplits[i] = bbuffer.getDouble();
    if (bbuffer.hasRemaining())
        throw new RuntimeException("Error reading STR partitioner");
}

From source file:hivemall.GeneralLearnerBaseUDTF.java

@Nonnull
private static FeatureValue readFeatureValue(@Nonnull final ByteBuffer buf,
        @Nonnull final FeatureType featureType) {
    final String featureStr = NIOUtils.getString(buf);
    final Object feature;
    switch (featureType) {
    case STRING:// www . j a va  2 s  .c  o  m
        feature = featureStr;
        break;
    case INT:
        feature = Integer.valueOf(featureStr);
        break;
    case LONG:
        feature = Long.valueOf(featureStr);
        break;
    default:
        throw new IllegalStateException(
                "Unexpected feature type " + featureType + " for feature: " + featureStr);
    }
    double value = buf.getDouble();
    return new FeatureValue(feature, value);
}

From source file:au.org.ala.spatial.util.RecordsSmall.java

private double[] getAllDouble(String file) throws Exception {
    File f = new File(file);

    int size = (int) f.length() / 8;
    double[] all = new double[size];

    byte[] e = FileUtils.readFileToByteArray(f);

    ByteBuffer bb = ByteBuffer.wrap(e);

    for (int i = 0; i < size; i++) {
        all[i] = bb.getDouble();
    }//from  www.  ja v  a  2 s  .c o  m

    return all;
}