Java Code Examples for java.nio.ByteBuffer#getDouble()

The following examples show how to use java.nio.ByteBuffer#getDouble() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BinarySearch.java    From indexr with Apache License 2.0 6 votes vote down vote up
public static int binarySearchDoubles(ByteBuffer buffer, int count, double key) {
    int from = 0;
    int to = count;

    --to;

    while (from <= to) {
        int mid = from + to >>> 1;
        double midVal = buffer.getDouble(mid << 3);
        if (midVal < key) {
            from = mid + 1;
        } else if (midVal > key) {
            to = mid - 1;
        } else {
            return mid;
        }
    }

    return -(from + 1);
}
 
Example 2
Source File: PartitionMetric.java    From cruise-control with BSD 2-Clause "Simplified" License 6 votes vote down vote up
static PartitionMetric fromBuffer(ByteBuffer buffer) throws UnknownVersionException {
  byte version = buffer.get();
  if (version > METRIC_VERSION) {
    throw new UnknownVersionException("Cannot deserialize the topic metrics for version " + version + ". "
                                          + "Current version is " + METRIC_VERSION);
  }
  RawMetricType rawMetricType = RawMetricType.forId(buffer.get());
  long time = buffer.getLong();
  int brokerId = buffer.getInt();
  int topicLength = buffer.getInt();
  String topic = new String(buffer.array(), buffer.arrayOffset() + buffer.position(), topicLength, StandardCharsets.UTF_8);
  buffer.position(buffer.position() + topicLength);
  int partition = buffer.getInt();
  double value = buffer.getDouble();
  return new PartitionMetric(rawMetricType, time, brokerId, topic, partition, value);
}
 
Example 3
Source File: ModelReader.java    From xgboost-predictor-java with Apache License 2.0 6 votes vote down vote up
public double[] readDoubleArrayBE(int numValues) throws IOException {
    int numBytesRead = fillBuffer(numValues * 8);
    if (numBytesRead < numValues * 8) {
        throw new EOFException(
                String.format("Cannot read double array (shortage): expected = %d, actual = %d",
                        numValues * 8, numBytesRead));
    }

    ByteBuffer byteBuffer = ByteBuffer.wrap(buffer).order(ByteOrder.BIG_ENDIAN);

    double[] result = new double[numValues];
    for (int i = 0; i < numValues; i++) {
        result[i] = byteBuffer.getDouble();
    }

    return result;
}
 
Example 4
Source File: OsuReader.java    From opsu-dance with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Reads an 8-byte little endian double.
 */
public double readDouble() throws IOException {
	byte[] bytes = new byte[8];
	this.reader.readFully(bytes);
	ByteBuffer bb = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
	return bb.getDouble();
}
 
Example 5
Source File: GeoObjectDimensionValues.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Override
public void fromBinary(final byte[] bytes) {
  final ByteBuffer b = ByteBuffer.wrap(bytes);
  count = VarintUtils.readUnsignedLong(b);
  x = b.getDouble();
  y = b.getDouble();
  z = b.getDouble();
  distance = b.getDouble();
  int i = VarintUtils.readUnsignedInt(b);
  values = new double[i];
  for (; i > 0; i--) {
    values[i - 1] = b.getDouble();
  }
}
 
Example 6
Source File: BrokerMetric.java    From cruise-control with BSD 2-Clause "Simplified" License 5 votes vote down vote up
static BrokerMetric fromBuffer(ByteBuffer buffer) throws UnknownVersionException {
  byte version = buffer.get();
  if (version > METRIC_VERSION) {
    throw new UnknownVersionException("Cannot deserialize the topic metrics for version " + version + ". "
                                          + "Current version is " + METRIC_VERSION);
  }
  RawMetricType rawMetricType = RawMetricType.forId(buffer.get());
  long time = buffer.getLong();
  int brokerId = buffer.getInt();
  double value = buffer.getDouble();
  return new BrokerMetric(rawMetricType, time, brokerId, value);
}
 
Example 7
Source File: ReadWriteIOUtils.java    From incubator-iotdb with Apache License 2.0 5 votes vote down vote up
public static Object readObject(ByteBuffer buffer) {
  ClassSerializeId serializeId = ClassSerializeId.values()[buffer.get()];
  switch (serializeId) {
    case BOOLEAN:
      return buffer.get() == 1;
    case FLOAT:
      return buffer.getFloat();
    case DOUBLE:
      return buffer.getDouble();
    case LONG:
      return buffer.getLong();
    case INTEGER:
      return buffer.getInt();
    case BINARY:
      int length = buffer.getInt();
      byte[] bytes = new byte[length];
      buffer.get(bytes);
      return new Binary(bytes);
    case NULL:
      return null;
    case STRING:
    default:
      length = buffer.getInt();
      bytes = new byte[length];
      buffer.get(bytes);
      return new String(bytes);
  }
}
 
Example 8
Source File: AMFDecoder.java    From stream-m with MIT License 5 votes vote down vote up
public double readNumber() {
    if (buffer[offset] != 0x00) {
        throw new IllegalArgumentException("Number type ID (0x00) expected.");
    }
    offset++;
    ByteBuffer bb = ByteBuffer.wrap(buffer, offset, 8);
    double result = bb.getDouble();
    offset += 8;
    return result;
}
 
Example 9
Source File: Resolution.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Override
public void fromBinary(final byte[] bytes) {
  final ByteBuffer buf = ByteBuffer.wrap(bytes);
  final int length = bytes.length / 8;
  resolutionPerDimension = new double[length];
  for (int i = 0; i < length; i++) {
    resolutionPerDimension[i] = buf.getDouble();
  }
}
 
Example 10
Source File: TlvDecoder.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Decodes a byte array into a float value.
 */
public static Number decodeFloat(byte[] value) throws TlvException {
    ByteBuffer floatBb = ByteBuffer.wrap(value);
    if (value.length == 4) {
        return floatBb.getFloat();
    } else if (value.length == 8) {
        return floatBb.getDouble();
    } else {
        throw new TlvException("Invalid length for a float value: " + value.length);
    }
}
 
Example 11
Source File: ByteBufferTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
void getOne(ByteBuffer b, PrimitiveType t, int index) {
    switch (t) {
    case BYTE: b.get(index); break;
    case CHAR: b.getChar(index); break;
    case SHORT: b.getShort(index); break;
    case INT: b.getInt(index); break;
    case LONG: b.getLong(index); break;
    case FLOAT: b.getFloat(index); break;
    case DOUBLE: b.getDouble(index); break;
    }
}
 
Example 12
Source File: DataBufferStruct.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public static DataBuffer createFromByteBuffer(ByteBuffer bb,int bb_pos,DataBuffer.Type type,int length,int elementSize) {
    bb.order(ByteOrder.LITTLE_ENDIAN);
    DataBuffer ret = Nd4j.createBuffer(ByteBuffer.allocateDirect(length *   elementSize),type,length,0);
    switch(type) {
        case DOUBLE:
            for(int i = 0; i < ret.length(); i++) {
                double doubleGet = bb.getDouble(bb.capacity() - bb_pos + (i * elementSize));
                ret.put(i,doubleGet);
            }
            break;
        case FLOAT:
            for(int i = 0; i < ret.length(); i++) {
                float floatGet = bb.getFloat(bb.capacity() - bb_pos + (i * elementSize));
                ret.put(i,floatGet);
            }
            break;
        case INT:
            for(int i = 0; i < ret.length(); i++) {
                int intGet = bb.getInt(bb.capacity() - bb_pos  + (i * elementSize));
                ret.put(i,intGet);
            }
            break;
        case LONG:
            for(int i = 0; i < ret.length(); i++) {
                long longGet = bb.getLong(bb.capacity() - bb_pos  + (i * elementSize));
                ret.put(i,longGet);
            }
            break;
    }

    return ret;
}
 
Example 13
Source File: ShapeFileManage.java    From MeteoInfo with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static VectorLayer readPolylineShapes(DataInputStream br, int shapeNum) throws IOException {
    VectorLayer aLayer = new VectorLayer(ShapeTypes.Polyline);
    int RecordNum, ContentLength, aShapeType;
    double x, y;
    byte[] bytes;
    ByteBuffer buffer;
    
    //PointD aPoint;
    for (int i = 0; i < shapeNum; i++) {
        bytes = new byte[8];
        br.read(bytes);
        buffer = ByteBuffer.wrap(bytes);
        //br.skipBytes(12); 
        buffer.order(ByteOrder.BIG_ENDIAN);
        RecordNum = buffer.getInt();
        ContentLength = buffer.getInt();
        
        bytes = new byte[ContentLength * 2];
        br.read(bytes);
        buffer = ByteBuffer.wrap(bytes);
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        aShapeType = buffer.getInt();

        PolylineShape aPL = new PolylineShape();
        Extent extent = new Extent();
        extent.minX = buffer.getDouble();
        extent.minY = buffer.getDouble();
        extent.maxX = buffer.getDouble();
        extent.maxY = buffer.getDouble();
        aPL.setExtent(extent);

        aPL.setPartNum(buffer.getInt());
        int numPoints = buffer.getInt();
        aPL.parts = new int[aPL.getPartNum()];
        List<PointD> points = new ArrayList<>();

        //firstly read out parts begin pos in file 
        for (int j = 0; j < aPL.getPartNum(); j++) {
            aPL.parts[j] = buffer.getInt();
        }

        //read out coordinates 
        for (int j = 0; j < numPoints; j++) {
            x = buffer.getDouble();
            y = buffer.getDouble();
            PointD aPoint = new PointD();
            aPoint.X = x;
            aPoint.Y = y;
            points.add(aPoint);
        }
        aPL.setPoints(points);
        aLayer.addShape(aPL);
    }

    //Create legend scheme            
    aLayer.setLegendScheme(LegendManage.createSingleSymbolLegendScheme(ShapeTypes.Polyline, Color.darkGray, 1.0F));

    return aLayer;
}
 
Example 14
Source File: DoublePacker.java    From twister2 with Apache License 2.0 4 votes vote down vote up
@Override
public Double getFromBuffer(ByteBuffer byteBuffer) {
  return byteBuffer.getDouble();
}
 
Example 15
Source File: MinMaxRangePair.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static MinMaxRangePair fromByteBuffer(ByteBuffer byteBuffer) {
  return new MinMaxRangePair(byteBuffer.getDouble(), byteBuffer.getDouble());
}
 
Example 16
Source File: BinUtils.java    From BIMserver with GNU Affero General Public License v3.0 4 votes vote down vote up
public static double byteArrayToDouble(byte[] data) {
	ByteBuffer byteBuffer = ByteBuffer.wrap(data);
	return byteBuffer.getDouble();
}
 
Example 17
Source File: ByteBufferUtil.java    From dble with GNU General Public License v2.0 4 votes vote down vote up
public static double toDouble(ByteBuffer bytes) {
    return bytes.getDouble(bytes.position());
}
 
Example 18
Source File: DoubleSerializer.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public Double deserialize(ByteBuffer in) {
    return in.getDouble();
}
 
Example 19
Source File: DoubleValue.java    From vespa with Apache License 2.0 2 votes vote down vote up
/**
 * Create by decoding the value from the given buffer
 *
 * @param src buffer where the value is stored
 **/
DoubleValue(ByteBuffer src) {
    value = src.getDouble();
}
 
Example 20
Source File: ByteUtilities.java    From hortonmachine with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Convert a byte array to an double (little endian).
 * 
 * @param data the byte array to convert.
 * @return the double.
 */
public static double byteArrayToDoubleLE( byte[] data ) {
    ByteBuffer buffer = ByteBuffer.wrap(data);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    return buffer.getDouble();
}