Java Code Examples for io.netty.buffer.ByteBuf.resetWriterIndex()

The following are Jave code examples for showing how to use resetWriterIndex() of the io.netty.buffer.ByteBuf class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: message-broker   File: BindingDaoImpl.java   Source Code and License Vote up 6 votes
@Override
public void persist(String exchangeName, Binding binding) throws BrokerException {
    Connection connection = null;
    PreparedStatement statement = null;
    try {
        connection = getConnection();
        statement = connection.prepareStatement(RDBMSConstants.PS_INSERT_BINDING);
        statement.setString(1, exchangeName);
        statement.setString(2, binding.getQueue().getName());
        statement.setString(3, binding.getRoutingKey());
        FieldTable arguments = binding.getArguments();
        byte[] bytes = new byte[(int) arguments.getSize()];
        ByteBuf byteBuf = Unpooled.wrappedBuffer(bytes);
        byteBuf.resetWriterIndex();
        arguments.write(byteBuf);
        statement.setBytes(4, bytes);

        statement.executeUpdate();
        connection.commit();
    } catch (SQLException e) {
        throw new BrokerException("Error occurred while storing binding " + binding, e);
    } finally {
        close(connection, statement);
    }
}
 
Example 2
Project: message-broker   File: MessageDaoImpl.java   Source Code and License Vote up 6 votes
private Metadata prepareMetadata(PreparedStatement metadataStmt, Message message) throws SQLException {
    Metadata metadata = message.getMetadata();
    metadataStmt.setLong(1, metadata.getInternalId());
    metadataStmt.setString(2, metadata.getExchangeName());
    metadataStmt.setString(3, metadata.getRoutingKey());
    metadataStmt.setLong(4, metadata.getContentLength());
    long size = metadata.getProperties().getSize() + metadata.getHeaders().getSize();
    byte[] bytes = new byte[(int) size];
    ByteBuf buffer = Unpooled.wrappedBuffer(bytes);

    try {
        buffer.resetWriterIndex();

        metadata.getProperties().write(buffer);
        metadata.getHeaders().write(buffer);

        metadataStmt.setBytes(5, bytes);
        metadataStmt.addBatch();
    } finally {
        buffer.release();
    }
    return metadata;
}