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

The following are Jave code examples for showing how to use getDouble() of the java.nio.ByteBuffer class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: MTG-Card-Recognizer   File: ImageDesc.java   View Source Code Vote up 6 votes
public static ImageDesc readIn(ByteBuffer buf)
{
	FastQueue<BrightFeature> des = UtilFeature.createQueue(detDesc,0);
	int dts = buf.getInt();
	for(int i=0;i<dts;i++)
	{
		int vs = buf.getInt();
		BrightFeature f = new BrightFeature(vs);
		double[] vls = new double[vs];
		for(int j=0;j<vs;j++)
		{
			vls[j]=buf.getDouble();
		}
		f.set(vls);
		des.add(f);
	}
	AverageHash h = AverageHash.readIn(buf);
	return new ImageDesc(des,h);
}
 
Example 2
Project: abhot   File: ValueSerializer.java   View Source Code Vote up 5 votes
public static double getDoubleFromByteBuffer(ByteBuffer byteBuffer)
{
	byte flag = byteBuffer.get();
	double ret = 0;

	if (flag == FLOAT_VALUE)
		ret = byteBuffer.getFloat();
	else
		ret = byteBuffer.getDouble();

	return (ret);
}
 
Example 3
Project: openjdk-jdk10   File: ByteBufferTest.java   View Source Code Vote up 5 votes
Ret alignedReadSnippet(byte[] arg) {
    ByteBuffer buffer = ByteBuffer.wrap(arg).order(byteOrder);

    Ret ret = new Ret();
    ret.byteValue = buffer.get();
    ret.byteValue += buffer.get();
    ret.shortValue = buffer.getShort();
    ret.intValue = buffer.getInt();
    ret.longValue = buffer.getLong();
    ret.doubleValue = buffer.getDouble();
    ret.floatValue = buffer.getFloat();

    return ret;
}
 
Example 4
Project: openjdk-jdk10   File: DirectByteBufferTest.java   View Source Code Vote up 5 votes
Ret unalignedReadSnippet(byte[] arg) {
    ByteBuffer buffer = makeDirect(arg, byteOrder);

    Ret ret = new Ret();
    ret.byteValue = buffer.get();
    ret.shortValue = buffer.getShort();
    ret.intValue = buffer.getInt();
    ret.longValue = buffer.getLong();
    ret.doubleValue = buffer.getDouble();
    ret.floatValue = buffer.getFloat();

    return ret;
}
 
Example 5
Project: simulacron   File: CqlMapper.java   View Source Code Vote up 5 votes
@Override
Double decodeInternal(ByteBuffer input) {
  if (input == null || input.remaining() == 0) return 0.0;
  if (input.remaining() != 8)
    throw new InvalidTypeException(
        "Invalid 64-bits double value, expecting 8 bytes but got " + input.remaining());

  return input.getDouble(input.position());
}
 
Example 6
Project: guava-mock   File: Stats.java   View Source Code Vote up 5 votes
/**
 * Creates a Stats instance from the byte representation read from the given {@link ByteBuffer}.
 *
 * <p><b>Note:</b> No guarantees are made regarding stability of the representation between
 * versions.
 *
 * @param buffer A {@link ByteBuffer} with at least BYTES {@link ByteBuffer#remaining}, ordered as
 *     {@link ByteOrder#LITTLE_ENDIAN}, from which a BYTES-long byte representation of this
 *     instance is read. In the process increases the position of {@link ByteBuffer} by BYTES.
 */
static Stats readFrom(ByteBuffer buffer) {
  checkNotNull(buffer);
  checkArgument(
      buffer.remaining() >= BYTES,
      "Expected at least Stats.BYTES = %s remaining , got %s",
      BYTES,
      buffer.remaining());
  return new Stats(
      buffer.getLong(),
      buffer.getDouble(),
      buffer.getDouble(),
      buffer.getDouble(),
      buffer.getDouble());
}
 
Example 7
Project: googles-monorepo-demo   File: PairedStats.java   View Source Code Vote up 5 votes
/**
 * Creates a {@link PairedStats} instance from the given byte representation which was obtained by
 * {@link #toByteArray}.
 *
 * <p><b>Note:</b> No guarantees are made regarding stability of the representation between
 * versions.
 */
public static PairedStats fromByteArray(byte[] byteArray) {
  checkNotNull(byteArray);
  checkArgument(
      byteArray.length == BYTES,
      "Expected PairedStats.BYTES = %s, got %s",
      BYTES,
      byteArray.length);
  ByteBuffer buffer = ByteBuffer.wrap(byteArray).order(ByteOrder.LITTLE_ENDIAN);
  Stats xStats = Stats.readFrom(buffer);
  Stats yStats = Stats.readFrom(buffer);
  double sumOfProductsOfDeltas = buffer.getDouble();
  return new PairedStats(xStats, yStats, sumOfProductsOfDeltas);
}
 
Example 8
Project: osu-beatmap-utils   File: OsuReader.java   View Source Code Vote up 5 votes
/**
 * 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 9
Project: Synapse   File: DbHelper.java   View Source Code Vote up 5 votes
@Nullable
private static Matrix[] byteArray2MatrixArray(byte... array) {
    if (array == null) {
        return null;
    }

    Matrix[] res = null;

    try {
        final ByteBuffer buffer = ByteBuffer.wrap(array);

        final int len = buffer.getInt();
        res = new Matrix[len];

        for (int i = 0; i < len; ++i) {
            final int row = buffer.getInt();
            final int col = buffer.getInt();
            final double[] doubles = new double[row * col];

            for (int j = 0, jLen = doubles.length; j < jLen; ++j) {
                doubles[j] = buffer.getDouble();
            }

            res[i] = Matrix.array(doubles, row);
        }
    } catch (Exception e) {
        e.printStackTrace();

        res = null;
    }

    return res;
}
 
Example 10
Project: cruise-control   File: TopicMetric.java   View Source Code Vote up 5 votes
static TopicMetric 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);
  }
  MetricType metricType = MetricType.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);
  double value = buffer.getDouble();
  return new TopicMetric(metricType, time, brokerId, topic, value);
}
 
Example 11
Project: multiple-dimension-spread   File: ConstantColumnBinaryMaker.java   View Source Code Vote up 4 votes
@Override
public void loadInMemoryStorage( final ColumnBinary columnBinary , final IMemoryAllocator allocator ) throws IOException{
  ByteBuffer wrapBuffer = ByteBuffer.wrap( columnBinary.binary , columnBinary.binaryStart , columnBinary.binaryLength );
  switch( columnBinary.columnType ){
    case BOOLEAN:
      boolean booleanObj = wrapBuffer.get() == 1;
      for( int i = 0 ; i < columnBinary.rowCount ; i++ ){
        allocator.setBoolean( i , booleanObj );
      }
      break;
    case BYTE:
      byte byteObj = wrapBuffer.get();
      for( int i = 0 ; i < columnBinary.rowCount ; i++ ){
        allocator.setByte( i , byteObj );
      }
      break;
    case SHORT:
      short shortObj = wrapBuffer.getShort();
      for( int i = 0 ; i < columnBinary.rowCount ; i++ ){
        allocator.setShort( i , shortObj );
      }
      break;
    case INTEGER:
      int intObj = wrapBuffer.getInt();
      for( int i = 0 ; i < columnBinary.rowCount ; i++ ){
        allocator.setInteger( i , intObj );
      }
      break;
    case LONG:
      long longObj = wrapBuffer.getLong();
      for( int i = 0 ; i < columnBinary.rowCount ; i++ ){
        allocator.setLong( i , longObj );
      }
      break;
    case FLOAT:
      float floatObj = wrapBuffer.getFloat();
      for( int i = 0 ; i < columnBinary.rowCount ; i++ ){
        allocator.setFloat( i , floatObj );
      }
      break;
    case DOUBLE:
      double doubleObj = wrapBuffer.getDouble();
      for( int i = 0 ; i < columnBinary.rowCount ; i++ ){
        allocator.setDouble( i , doubleObj );
      }
      break;
    case STRING:
      int stringLength = wrapBuffer.getInt();
      byte[] stringBytes = new byte[stringLength];
      wrapBuffer.get( stringBytes );
      String utf8 = new String( stringBytes , "UTF-8" );
      for( int i = 0 ; i < columnBinary.rowCount ; i++ ){
        allocator.setString( i , utf8 );
      }
      break;
    case BYTES:
      int byteLength = wrapBuffer.getInt();
      byte[] byteBytes = new byte[byteLength];
      wrapBuffer.get( byteBytes );
      for( int i = 0 ; i < columnBinary.rowCount ; i++ ){
        allocator.setBytes( i , byteBytes );
      }
      break;
    default:
      throw new IOException( "Unknown primitive type." );
  }
  allocator.setValueCount( columnBinary.rowCount );
}
 
Example 12
Project: dble   File: ByteBufferUtil.java   View Source Code Vote up 4 votes
public static double toDouble(ByteBuffer bytes) {
    return bytes.getDouble(bytes.position());
}
 
Example 13
Project: sstable-adaptor   File: ByteBufferUtil.java   View Source Code Vote up 4 votes
public static double toDouble(ByteBuffer bytes)
{
    return bytes.getDouble(bytes.position());
}
 
Example 14
Project: EasyDiameter   File: Float64AVP.java   View Source Code Vote up 4 votes
@Override
public void decodeData(ByteBuffer buffer, int length) {
	data = buffer.getDouble();
	addDataLength(length);
}
 
Example 15
Project: BIMplatform   File: ReadWriteVirtualObject.java   View Source Code Vote up 4 votes
@SuppressWarnings({ "unchecked", "rawtypes" })
protected Object readFeature(ByteBuffer buffer) {
	int typeValue = buffer.getInt();
	ReadWriteType type = ReadWriteType.getType(typeValue);
	Object value = null;
	switch (type) {
		case String:
			int length = buffer.getInt();
			if (length != -1) {
				value = BinUtils.readString(buffer, length);
			} 
			break;
		case Integer:
			value = buffer.getInt();
			break;
		case Double:
			value = buffer.getDouble();
			break;
		case Float:
			value = buffer.getFloat();
			break;
		case Long:
			value = buffer.getLong();
			break;
		case Boolean:
			value = (buffer.get() == 1);
			break;
		case List:
			List list = new ArrayList<>();
			int size = buffer.getInt();
			for (int i = 0; i < size; i++) {
				Object valueInList = readFeature(buffer);
				list.add(valueInList);
			}
			value = list;
			break;
		case ByteArray:
			int byteLength = buffer.getInt();
			if (byteLength != 0) {
				byte[] valueByte = new byte[byteLength];
				buffer.get(valueByte);
				value = valueByte;
			}
			break;
		case ReadWriteVirtualObject:
			ReadWriteVirtualObject readWriteVirtualObject = createReadWriteVirtualObject();
			readWriteVirtualObject.read(buffer);
			value = readWriteVirtualObject;
			break;
		default:
			break;
	}
	return value;
}
 
Example 16
Project: BIMplatform   File: BinUtils.java   View Source Code Vote up 4 votes
public static double byteArrayToDouble(byte[] data) {
	ByteBuffer byteBuffer = ByteBuffer.wrap(data);
	return byteBuffer.getDouble();
}
 
Example 17
Project: neoscada   File: DataFileAccessorImpl.java   View Source Code Vote up 4 votes
@Override
public boolean visitFirstValue ( final ValueVisitor visitor ) throws Exception
{
    logger.debug ( "Welcome backwards seeking visitor: {}", visitor );

    final long startPosition = this.channel.position ();

    logger.debug ( "Seeking at position: {}", startPosition );

    final ByteBuffer buffer = ByteBuffer.allocate ( ENTRY_SIZE );

    try
    {
        while ( this.channel.position () > HEADER_SIZE )
        {
            logger.debug ( "At position: {}", this.channel.position () );

            this.channel.position ( this.channel.position () - ENTRY_SIZE );
            if ( safeRead ( buffer ) != ENTRY_SIZE )
            {
                break;
            }

            buffer.flip ();

            final double value = buffer.getDouble ();
            final Date timestamp = new Date ( buffer.getLong () );
            final byte flags = buffer.get ();

            logger.debug ( "Stumbled upon {}/{}/{} when searching backwards", new Object[] { value, timestamp, flags } );

            if ( ( flags & FLAG_HEARTBEAT ) == 0 && ( flags & FLAG_DELETED ) == 0 && !Double.isNaN ( value ) )
            {
                visitor.value ( value, timestamp, ( flags & FLAG_ERROR ) > 0, ( flags & FLAG_MANUAL ) > 0 );
                return true;
            }

            buffer.clear ();
            this.channel.position ( this.channel.position () - ENTRY_SIZE );
        }
    }
    finally
    {
        this.channel.position ( startPosition );
        logger.debug ( "Returned to position: {}", startPosition );
    }
    return false;
}
 
Example 18
Project: highfrequencyrobots   File: CodexMagic.java   View Source Code Vote up 4 votes
@Override
public Double decodeSingle(ByteBuffer pData) {
  return pData.getDouble();
}
 
Example 19
Project: neoscada   File: DataFileAccessorImpl.java   View Source Code Vote up 4 votes
@Override
public void forwardCorrect ( final double value, final Date afterDate ) throws Exception
{
    final long startTimestamp = afterDate.getTime ();

    final long position = this.channel.position ();
    try
    {
        this.channel.position ( HEADER_SIZE );

        final ByteBuffer buffer = ByteBuffer.allocate ( ENTRY_SIZE );

        while ( safeRead ( buffer ) == ENTRY_SIZE )
        {
            buffer.flip ();

            final double entryValue = buffer.getDouble ();
            final long entryTimestamp = buffer.getLong ();
            final byte flags = buffer.get ();

            logger.debug ( "Checking value - flag: {}", flags );

            if ( ( flags & FLAG_HEARTBEAT ) == 0 && ( flags & FLAG_DELETED ) == 0 )
            {
                if ( entryTimestamp > startTimestamp )
                {
                    logger.info ( "Rewriting history - delete - timestamp: {}, value: {}", entryTimestamp, entryValue );
                    // replace the flag value, mark as deleted
                    // the flag is one byte behind the current position
                    this.channel.position ( this.channel.position () - 1 );
                    this.channel.write ( ByteBuffer.wrap ( new byte[] { (byte) ( flags | FLAG_DELETED ) } ) );
                }
            }
            buffer.clear ();
        }

    }
    finally
    {
        this.channel.position ( position );
        logger.debug ( "Returned to position: {}", position );
    }
}
 
Example 20
Project: neoscada   File: HSDBValueSource.java   View Source Code Vote up 4 votes
private boolean parseFile ( final FileEntry entry, final ValueVisitor visitor ) throws IOException
{
    final InputStream input = new BufferedInputStream ( new FileInputStream ( entry.getFile () ) );
    try
    {
        final byte[] header = new byte[8 + 8];
        if ( input.read ( header ) != header.length )
        {
            throw new IOException ( "Corrupt header" );
        }

        final ByteBuffer headerBuffer = ByteBuffer.wrap ( header );
        headerBuffer.getLong ();
        final long dataStart = headerBuffer.getLong ();

        if ( dataStart <= header.length )
        {
            throw new IOException ( "Illegal data start address" );
        }

        input.skip ( dataStart - header.length );
        final byte[] recordArray = new byte[41];
        final ByteBuffer record = ByteBuffer.wrap ( recordArray );

        while ( input.read ( record.array () ) == recordArray.length )
        {
            byte parityCalc = 0x5A;
            for ( int i = 0; i < 40; i++ )
            {
                parityCalc = (byte) ( parityCalc ^ record.get ( i ) );
            }

            final Date timestamp = new Date ( record.getLong () );
            final long quality = record.getLong ();
            final long manual = record.getLong ();
            @SuppressWarnings ( "unused" )
            final long sourceCount = record.getLong ();
            final double value = record.getDouble ();
            final byte parity = record.get ();

            record.clear ();

            if ( parity != parityCalc )
            {
                logger.warn ( "Parity does not match - read: {}, calc: {}", parity, parityCalc );
                continue;
            }

            logger.trace ( "Visiting value - timestamp: {}, value: {}", timestamp, value );
            if ( !visitor.value ( value, timestamp, quality == 100, manual != 0 ) )
            {

                return false;
            }
        }

        return true;
    }
    finally
    {
        input.close ();
    }
}