Example usage for java.nio ByteBuffer putFloat

List of usage examples for java.nio ByteBuffer putFloat

Introduction

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

Prototype

public abstract ByteBuffer putFloat(int index, float value);

Source Link

Document

Writes the given float to the specified index of this buffer.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer bbuf = ByteBuffer.allocate(10);
    int capacity = bbuf.capacity(); // 10
    System.out.println(capacity);
    bbuf.putFloat(2, 123F);

    System.out.println(Arrays.toString(bbuf.array()));
}

From source file:au.org.ala.delta.translation.dist.DistItemsFileWriter.java

private void encodeOrderedMultistateAttribute(ByteBuffer work, int wordOffset,
        IdentificationKeyCharacter keyChar, MultiStateAttribute attribute) {
    List<Integer> states = keyChar.getPresentStates(attribute);
    double average = average(states);
    work.putFloat(wordOffset * 4, new Float(average));

}

From source file:au.org.ala.delta.translation.dist.DistItemsFileWriter.java

private void encodeNumericAttribute(ByteBuffer work, int wordOffset, IdentificationKeyCharacter keyChar,
        NumericAttribute attribute) {//from w w w.ja v  a2 s. c  o  m
    List<NumericRange> ranges = attribute.getNumericValue();
    List<Double> values = new ArrayList<Double>();

    for (NumericRange range : ranges) {
        values.add(range.middle());

    }
    double average = average(values);
    work.putFloat(wordOffset * 4, new Float(average));
}

From source file:au.org.ala.delta.translation.dist.DistItemsFileWriter.java

protected Pair<List<Integer>, List<Integer>> writeItems(int[] wordOffsets, int[] bitOffsets) {
    final int BYTES_IN_WORD = 4;
    List<Integer> itemRecords = new ArrayList<Integer>();
    List<Integer> nameLengths = new ArrayList<Integer>();
    int size = BinaryKeyFile.RECORD_LENGTH_BYTES;
    for (int offset : wordOffsets) {
        size = Math.max(size, offset);
    }//from w w  w.ja v a2s  .  c  om
    Iterator<Item> items = _dataSet.unfilteredItems();
    while (items.hasNext()) {
        Item item = items.next();
        String description = _itemFormatter.formatItemDescription(item);
        nameLengths.add(description.length());
        byte[] bytes = new byte[(size + 1) * BYTES_IN_WORD];
        Arrays.fill(bytes, (byte) 0);

        ByteBuffer work = ByteBuffer.wrap(bytes);
        work.order(ByteOrder.LITTLE_ENDIAN);

        Iterator<IdentificationKeyCharacter> chars = _dataSet.unfilteredIdentificationKeyCharacterIterator();
        while (chars.hasNext()) {
            IdentificationKeyCharacter keyChar = chars.next();
            int charNum = keyChar.getCharacterNumber();
            if (!keyChar.getCharacterType().isText()) {
                int offset = wordOffsets[keyChar.getCharacterNumber() - 1] - 1;
                if (!(keyChar.getCharacterType() == CharacterType.UnorderedMultiState)) {
                    work.putFloat(offset * BYTES_IN_WORD, -9999.0f);
                }
                Attribute attribute = item.getAttribute(keyChar.getCharacter());
                if (attribute == null || attribute.isUnknown()) {
                    continue;
                }
                switch (keyChar.getCharacterType()) {
                case UnorderedMultiState:
                    encodeUnorderedMultistateAttribute(work, wordOffsets[charNum - 1] - 1,
                            bitOffsets[charNum - 1], keyChar, (MultiStateAttribute) attribute);

                    break;
                case OrderedMultiState:
                    encodeOrderedMultistateAttribute(work, wordOffsets[charNum - 1] - 1, keyChar,
                            (MultiStateAttribute) attribute);
                    break;
                case IntegerNumeric:
                case RealNumeric:
                    encodeNumericAttribute(work, wordOffsets[charNum - 1] - 1, keyChar,
                            (NumericAttribute) attribute);

                    break;
                }
            }

        }
        itemRecords.add(_itemsFile.writeItem(description, work));
    }
    return new Pair<List<Integer>, List<Integer>>(itemRecords, nameLengths);
}