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

The following are Jave code examples for showing how to use putFloat() 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: openjdk-jdk10   File: DirectByteBufferTest.java   View Source Code Vote up 7 votes
byte[] unalignedWriteSnippet(byte a, short b, int c, long d, double e, float f) {
    byte[] ret = new byte[27];
    ByteBuffer buffer = makeDirect(27, byteOrder);

    buffer.put(a);
    buffer.putShort(b);
    buffer.putInt(c);
    buffer.putLong(d);
    buffer.putDouble(e);
    buffer.putFloat(f);

    buffer.position(0);
    buffer.get(ret);

    return ret;
}
 
Example 2
Project: Word2VecfJava   File: WordVectorSerializer.java   View Source Code Vote up 6 votes
/** Save the word2vec model as binary file */
@SuppressWarnings("unused")
public static void saveWord2VecToBinary(String toPath, Word2Vec w2v){
	final Charset cs = StandardCharsets.UTF_8;
	try {
		final OutputStream os = new FileOutputStream(new File(toPath));
		final String header = String.format("%d %d\n", w2v.wordVocabSize(), w2v.getLayerSize());
		os.write(header.getBytes(cs));
		final ByteBuffer buffer = ByteBuffer.allocate(4 * w2v.getLayerSize());
		buffer.order(byteOrder);
		for (int i = 0; i < w2v.wordVocabSize(); ++i) {
			os.write(String.format("%s ", w2v.getWordVocab().get(i)).getBytes(cs)); // Write one word in byte format, add a space.
			buffer.clear();
			for (int j = 0; j < w2v.getLayerSize(); ++j) {
				buffer.putFloat(w2v.getWordVectors().getFloat(i, j));
			}
			os.write(buffer.array()); // Write all float values of one vector in byte format.
			os.write('\n'); // Add a newline.
		}
		os.flush();
		os.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example 3
Project: G2Dj   File: Mat4.java   View Source Code Vote up 6 votes
public ByteBuffer toDbb(ByteBuffer res, int index) {
    res.putFloat(index + 0 * glm.Constants.FloatBYTES, m00);
    res.putFloat(index + 1 * glm.Constants.FloatBYTES, m01);
    res.putFloat(index + 2 * glm.Constants.FloatBYTES, m02);
    res.putFloat(index + 3 * glm.Constants.FloatBYTES, m03);
    res.putFloat(index + 4 * glm.Constants.FloatBYTES, m10);
    res.putFloat(index + 5 * glm.Constants.FloatBYTES, m11);
    res.putFloat(index + 6 * glm.Constants.FloatBYTES, m12);
    res.putFloat(index + 7 * glm.Constants.FloatBYTES, m13);
    res.putFloat(index + 8 * glm.Constants.FloatBYTES, m20);
    res.putFloat(index + 9 * glm.Constants.FloatBYTES, m21);
    res.putFloat(index + 10 * glm.Constants.FloatBYTES, m22);
    res.putFloat(index + 11 * glm.Constants.FloatBYTES, m23);
    res.putFloat(index + 12 * glm.Constants.FloatBYTES, m30);
    res.putFloat(index + 13 * glm.Constants.FloatBYTES, m31);
    res.putFloat(index + 14 * glm.Constants.FloatBYTES, m32);
    res.putFloat(index + 15 * glm.Constants.FloatBYTES, m33);
    return res;
}
 
Example 4
Project: penguins-in-space   File: NetworkSystem.java   View Source Code Vote up 6 votes
@Override
protected void processEntity(Entity entity, float deltaTime) {
    TransformComponent transform = transformMapper.get(entity);
    MovementComponent movement = movementMapper.get(entity);
    ByteBuffer buffer = ByteBuffer.allocate(4 * 8 + 1);
    buffer.put(MOVE);
    buffer.putFloat(transform.position.x);
    buffer.putFloat(transform.position.y);
    buffer.putFloat(transform.rotation.x);
    buffer.putFloat(transform.rotation.y);
    buffer.putFloat(movement.velocity.x);
    buffer.putFloat(movement.velocity.y);
    buffer.putFloat(movement.acceleration.x);
    buffer.putFloat(movement.acceleration.y);
    networkService.sendUnreliableMessageToOthers(buffer.array());
}
 
Example 5
Project: penguins-in-space   File: NetworkSystem.java   View Source Code Vote up 6 votes
private void sendBullet(Entity entity) {
//        Gdx.app.debug(TAG, "sendBullet: ");
        IdComponent bulletId = idMapper.get(entity);
        IdComponent playerId = idMapper.get(player.first());
        if (!Objects.equals(bulletId.participantId, playerId.participantId)) {
            return;
        }
        TransformComponent transform = transformMapper.get(entity);
        MovementComponent movement = movementMapper.get(entity);
        ByteBuffer buffer = ByteBuffer.allocate(4 * 4 + 1);
        buffer.put(BULLET);
        buffer.putFloat(transform.position.x);
        buffer.putFloat(transform.position.y);
        buffer.putFloat(movement.velocity.x);
        buffer.putFloat(movement.velocity.y);
        networkService.sendUnreliableMessageToOthers(buffer.array());
    }
 
Example 6
Project: candlelight   File: Matrix2f.java   View Source Code Vote up 6 votes
@Override
public ByteBuffer getTransposed(int index, ByteBuffer buffer)
{
	if (index == 0)
	{
		buffer.putFloat(0, m00);
		buffer.putFloat(4, m10);
		buffer.putFloat(8, m01);
		buffer.putFloat(12, m11);
	}
	else
	{
		buffer.putFloat(index + 0, m00);
		buffer.putFloat(index + 4, m10);
		buffer.putFloat(index + 8, m01);
		buffer.putFloat(index + 12, m11);
	}
	return buffer;
}
 
Example 7
Project: neoscada   File: FloatVariable.java   View Source Code Vote up 6 votes
@Override
protected NotifyFuture<WriteResult> handleWrite ( final Variant value )
{
    final MemoryRequestBlock block = this.block;
    if ( block == null )
    {
        return new InstantErrorFuture<> ( new IllegalStateException ( "Device is not connected" ) );
    }

    final Double d = value.asDouble ( null );
    if ( d != null )
    {
        final ByteBuffer b = ByteBuffer.allocate ( 4 );
        b.putFloat ( d.floatValue () );
        block.writeData ( toAddress ( this.index ), b.array () );
        return new InstantFuture<WriteResult> ( new WriteResult () );
    }
    else
    {
        return new InstantErrorFuture<WriteResult> ( new IllegalArgumentException ( String.format ( "Can only write doubles: %s is not a double", value ) ) );
    }
}
 
Example 8
Project: openjdk-jdk10   File: ByteBufferTest.java   View Source Code Vote up 5 votes
void putOne(ByteBuffer b, PrimitiveType t) {
    switch (t) {
    case BYTE: b.put((byte)0); break;
    case CHAR: b.putChar('0'); break;
    case SHORT: b.putShort((short)0); break;
    case INT: b.putInt(0); break;
    case LONG: b.putLong(0); break;
    case FLOAT: b.putFloat(0); break;
    case DOUBLE: b.putDouble(0); break;
    }
}
 
Example 9
Project: openjdk-jdk10   File: ByteBufferTest.java   View Source Code Vote up 5 votes
byte[] alignedWriteSnippet(byte a, byte b, short c, int d, long e, double f, float g) {
    byte[] ret = new byte[28];
    ByteBuffer buffer = ByteBuffer.wrap(ret).order(byteOrder);

    buffer.put(a);
    buffer.put(b);
    buffer.putShort(c);
    buffer.putInt(d);
    buffer.putLong(e);
    buffer.putDouble(f);
    buffer.putFloat(g);

    return ret;
}
 
Example 10
Project: Mindustry   File: Packets.java   View Source Code Vote up 5 votes
@Override
public void write(ByteBuffer buffer) {
    for(int i = 0; i < items.length; i ++){
        buffer.putInt(items[i]);
    }

    buffer.putFloat(countdown);
    buffer.putFloat(time);
    buffer.putShort((short)enemies);
    buffer.putShort((short)wave);
    buffer.putLong(timestamp);
}
 
Example 11
Project: Mindustry   File: Packets.java   View Source Code Vote up 5 votes
@Override
public void write(ByteBuffer buffer) {
    buffer.put(weaponid);
    buffer.putFloat(x);
    buffer.putFloat(y);
    buffer.putFloat(rotation);
    buffer.putInt(playerid);
}
 
Example 12
Project: debug   File: DebugIT.java   View Source Code Vote up 5 votes
@Test
public void testUseFloatBufferViewOfByteBufferWithWrongByteOrder() {
    window = glfwCreateWindow(800, 600, "", 0L, 0L);
    glfwMakeContextCurrent(window);
    createCapabilities();
    ByteBuffer bb = ByteBuffer.allocateDirect(16 * 4);
    bb.putFloat(0, 1.0f);
    FloatBuffer fb = bb.asFloatBuffer();
    assertThrows(IllegalArgumentException.class, () -> glLoadMatrixf(fb), "buffer contains values written using non-native endianness.");
}
 
Example 13
Project: AndroidSDK2.0   File: Fdot.java   View Source Code Vote up 5 votes
/**
     * 필요 dot data만 byte array로.
     * (저장 시에.)
     *
     * @return byte [ ]
     */
    public byte[] toCompactByteArray() {
        ByteBuffer buffer = ByteBuffer.allocate(DOT_DATA_COMPACT_BYTE_ALIGN);

        buffer.put( (byte) dotType );
        buffer.putFloat( x );
        buffer.putFloat( y);
        buffer.putLong(timestamp);
//        buffer.put((byte) fx);
//        buffer.put((byte) fy);
        buffer.put((byte) pressure);

        return buffer.array();
    }
 
Example 14
Project: penguins-in-space   File: NetworkSystem.java   View Source Code Vote up 5 votes
private void sendObstacle(Entity entity) {
    TransformComponent transform = transformMapper.get(entity);
    MovementComponent movement = movementMapper.get(entity);
    ByteBuffer buffer = ByteBuffer.allocate(4 * 4 + 1);
    buffer.put(OBSTACLE);
    buffer.putFloat(transform.position.x);
    buffer.putFloat(transform.position.y);
    buffer.putFloat(movement.velocity.x);
    buffer.putFloat(movement.velocity.y);
    networkService.sendUnreliableMessageToOthers(buffer.array());
}
 
Example 15
Project: debug   File: RT.java   View Source Code Vote up 4 votes
public static ByteBuffer putFloat(ByteBuffer buf, float value) {
    buf.putFloat(value);
    writeByteBuffer(buf);
    return buf;
}
 
Example 16
Project: CodeBroker   File: DefaultSFSDataSerializer.java   View Source Code Vote up 4 votes
private ByteBuffer binEncode_FLOAT(ByteBuffer buffer, Float value) {
    ByteBuffer buf = ByteBuffer.allocate(5);
    buf.put((byte) DataType.FLOAT.typeID);
    buf.putFloat(value.floatValue());
    return this.addData(buffer, buf.array());
}
 
Example 17
Project: debug   File: RT.java   View Source Code Vote up 4 votes
public static ByteBuffer putFloat(ByteBuffer buf, int index, float value) {
    buf.putFloat(index, value);
    writeByteBuffer(buf);
    return buf;
}
 
Example 18
Project: LowPolyTerrain   File: DataStoring.java   View Source Code Vote up 4 votes
private static void storePosition(ByteBuffer buffer, float x, float y, float z) {
	buffer.putFloat(x);
	buffer.putFloat(y);
	buffer.putFloat(z);
}
 
Example 19
Project: EasyDiameter   File: Float32AVP.java   View Source Code Vote up 4 votes
@Override
public void encodeData(ByteBuffer buffer) {
	buffer.putFloat(data);
}
 
Example 20
Project: LowPolyWater   File: DataStoring.java   View Source Code Vote up 4 votes
private static void store3Floats(ByteBuffer buffer, float a, float b, float c) {
	buffer.putFloat(a);
	buffer.putFloat(b);
	buffer.putFloat(c);
}