Example usage for java.lang Float intBitsToFloat

List of usage examples for java.lang Float intBitsToFloat

Introduction

In this page you can find the example usage for java.lang Float intBitsToFloat.

Prototype

@HotSpotIntrinsicCandidate
public static native float intBitsToFloat(int bits);

Source Link

Document

Returns the float value corresponding to a given bit representation.

Usage

From source file:org.freedesktop.dbus.Message.java

/**
 * Demarshall one value from a buffer./*w  ww  .j  a  va  2s .  c  o m*/
 * 
 * @param sigb
 *            A buffer of the D-Bus signature.
 * @param buf
 *            The buffer to demarshall from.
 * @param ofs
 *            An array of two ints, the offset into the signature buffer
 *            and the offset into the data buffer. These values will be
 *            updated to the start of the next value ofter demarshalling.
 * @param contained
 *            converts nested arrays to Lists
 * @return The demarshalled value.
 */
private Object extractone(byte[] sigb, byte[] buf, int[] ofs, boolean contained) throws DBusException {
    if (log.isTraceEnabled()) {
        log.trace("Extracting type: " + ((char) sigb[ofs[0]]) + " from offset " + ofs[1]);
    }
    Object rv = null;
    ofs[1] = align(ofs[1], sigb[ofs[0]]);
    switch (sigb[ofs[0]]) {
    case ArgumentType.BYTE:
        rv = buf[ofs[1]++];
        break;
    case ArgumentType.UINT32:
        rv = new UInt32(demarshallint(buf, ofs[1], 4));
        ofs[1] += 4;
        break;
    case ArgumentType.INT32:
        rv = (int) demarshallint(buf, ofs[1], 4);
        ofs[1] += 4;
        break;
    case ArgumentType.INT16:
        rv = (short) demarshallint(buf, ofs[1], 2);
        ofs[1] += 2;
        break;
    case ArgumentType.UINT16:
        rv = new UInt16((int) demarshallint(buf, ofs[1], 2));
        ofs[1] += 2;
        break;
    case ArgumentType.INT64:
        rv = demarshallint(buf, ofs[1], 8);
        ofs[1] += 8;
        break;
    case ArgumentType.UINT64:
        long top;
        long bottom;
        if (this.big) {
            top = demarshallint(buf, ofs[1], 4);
            ofs[1] += 4;
            bottom = demarshallint(buf, ofs[1], 4);
        } else {
            bottom = demarshallint(buf, ofs[1], 4);
            ofs[1] += 4;
            top = demarshallint(buf, ofs[1], 4);
        }
        rv = new UInt64(top, bottom);
        ofs[1] += 4;
        break;
    case ArgumentType.DOUBLE:
        long l = demarshallint(buf, ofs[1], 8);
        ofs[1] += 8;
        rv = Double.longBitsToDouble(l);
        break;
    case ArgumentType.FLOAT:
        int rf = (int) demarshallint(buf, ofs[1], 4);
        ofs[1] += 4;
        rv = Float.intBitsToFloat(rf);
        break;
    case ArgumentType.BOOLEAN:
        rf = (int) demarshallint(buf, ofs[1], 4);
        ofs[1] += 4;
        rv = (1 == rf) ? Boolean.TRUE : Boolean.FALSE;
        break;
    case ArgumentType.ARRAY:
        long size = demarshallint(buf, ofs[1], 4);
        if (log.isTraceEnabled()) {
            log.trace("Reading array of size: " + size);
        }
        ofs[1] += 4;
        byte algn = (byte) getAlignment(sigb[++ofs[0]]);
        ofs[1] = align(ofs[1], sigb[ofs[0]]);
        int length = (int) (size / algn);
        if (length > AbstractConnection.MAX_ARRAY_LENGTH)
            throw new MarshallingException("Arrays must not exceed " + AbstractConnection.MAX_ARRAY_LENGTH);
        // optimise primatives
        switch (sigb[ofs[0]]) {
        case ArgumentType.BYTE:
            rv = new byte[length];
            System.arraycopy(buf, ofs[1], rv, 0, length);
            ofs[1] += size;
            break;
        case ArgumentType.INT16:
            rv = new short[length];
            for (int j = 0; j < length; j++, ofs[1] += algn)
                ((short[]) rv)[j] = (short) demarshallint(buf, ofs[1], algn);
            break;
        case ArgumentType.INT32:
            rv = new int[length];
            for (int j = 0; j < length; j++, ofs[1] += algn)
                ((int[]) rv)[j] = (int) demarshallint(buf, ofs[1], algn);
            break;
        case ArgumentType.INT64:
            rv = new long[length];
            for (int j = 0; j < length; j++, ofs[1] += algn)
                ((long[]) rv)[j] = demarshallint(buf, ofs[1], algn);
            break;
        case ArgumentType.BOOLEAN:
            rv = new boolean[length];
            for (int j = 0; j < length; j++, ofs[1] += algn)
                ((boolean[]) rv)[j] = (1 == demarshallint(buf, ofs[1], algn));
            break;
        case ArgumentType.FLOAT:
            rv = new float[length];
            for (int j = 0; j < length; j++, ofs[1] += algn)
                ((float[]) rv)[j] = Float.intBitsToFloat((int) demarshallint(buf, ofs[1], algn));
            break;
        case ArgumentType.DOUBLE:
            rv = new double[length];
            for (int j = 0; j < length; j++, ofs[1] += algn)
                ((double[]) rv)[j] = Double.longBitsToDouble(demarshallint(buf, ofs[1], algn));
            break;
        case ArgumentType.DICT_ENTRY1:
            if (0 == size) {
                // advance the type parser even on 0-size arrays.
                Vector<Type> temp = new Vector<>();
                byte[] temp2 = new byte[sigb.length - ofs[0]];
                System.arraycopy(sigb, ofs[0], temp2, 0, temp2.length);
                String temp3 = new String(temp2);
                // ofs[0] gets incremented anyway. Leave one character on the stack
                int temp4 = Marshalling.getJavaType(temp3, temp, 1) - 1;
                ofs[0] += temp4;
                if (log.isTraceEnabled()) {
                    log.trace("Aligned type: " + temp3 + " " + temp4 + " " + ofs[0]);
                }
            }
            int ofssave = ofs[0];
            long end = ofs[1] + size;
            Vector<Object[]> entries = new Vector<>();
            while (ofs[1] < end) {
                ofs[0] = ofssave;
                entries.add((Object[]) extractone(sigb, buf, ofs, true));
            }
            rv = new DBusMap<>(entries.toArray(new Object[0][]));
            break;
        default:
            if (0 == size) {
                // advance the type parser even on 0-size arrays.
                Vector<Type> temp = new Vector<>();
                byte[] temp2 = new byte[sigb.length - ofs[0]];
                System.arraycopy(sigb, ofs[0], temp2, 0, temp2.length);
                String temp3 = new String(temp2);
                // ofs[0] gets incremented anyway. Leave one character on the stack
                int temp4 = Marshalling.getJavaType(temp3, temp, 1) - 1;
                ofs[0] += temp4;
                if (log.isTraceEnabled()) {
                    log.trace("Aligned type: " + temp3 + " " + temp4 + " " + ofs[0]);
                }
            }
            ofssave = ofs[0];
            end = ofs[1] + size;
            Vector<Object> contents = new Vector<>();
            while (ofs[1] < end) {
                ofs[0] = ofssave;
                contents.add(extractone(sigb, buf, ofs, true));
            }
            rv = contents;
        }
        if (contained && !(rv instanceof List) && !(rv instanceof Map))
            rv = ArrayFrob.listify(rv);
        break;
    case ArgumentType.STRUCT1:
        Vector<Object> contents = new Vector<>();
        while (sigb[++ofs[0]] != ArgumentType.STRUCT2)
            contents.add(extractone(sigb, buf, ofs, true));
        rv = contents.toArray();
        break;
    case ArgumentType.DICT_ENTRY1:
        Object[] decontents = new Object[2];
        if (log.isTraceEnabled()) {
            Hex h = new Hex();
            log.trace(
                    "Extracting Dict Entry (" + h.encode(Arrays.copyOfRange(sigb, ofs[0], sigb.length - ofs[0]))
                            + ") from: " + h.encode(Arrays.copyOfRange(buf, ofs[1], buf.length - ofs[1])));
        }
        ofs[0]++;
        decontents[0] = extractone(sigb, buf, ofs, true);
        ofs[0]++;
        decontents[1] = extractone(sigb, buf, ofs, true);
        ofs[0]++;
        rv = decontents;
        break;
    case ArgumentType.VARIANT:
        int[] newofs = new int[] { 0, ofs[1] };
        String sig = (String) extract(ArgumentType.SIGNATURE_STRING, buf, newofs)[0];
        newofs[0] = 0;
        rv = new Variant<>(extract(sig, buf, newofs)[0], sig);
        ofs[1] = newofs[1];
        break;
    case ArgumentType.STRING:
        length = (int) demarshallint(buf, ofs[1], 4);
        ofs[1] += 4;
        try {
            rv = new String(buf, ofs[1], length, "UTF-8");
        } catch (UnsupportedEncodingException UEe) {
            throw new DBusException("System does not support UTF-8 encoding", UEe);
        }
        ofs[1] += length + 1;
        break;
    case ArgumentType.OBJECT_PATH:
        length = (int) demarshallint(buf, ofs[1], 4);
        ofs[1] += 4;
        rv = new ObjectPath(getSource(), new String(buf, ofs[1], length));
        ofs[1] += length + 1;
        break;
    case ArgumentType.SIGNATURE:
        length = (buf[ofs[1]++] & 0xFF);
        rv = new String(buf, ofs[1], length);
        ofs[1] += length + 1;
        break;
    default:
        throw new UnknownTypeCodeException(sigb[ofs[0]]);
    }
    if (log.isDebugEnabled()) {
        if (rv instanceof Object[])
            log.trace("Extracted: " + Arrays.deepToString((Object[]) rv) + " (now at " + ofs[1] + ")");
        else
            log.trace("Extracted: " + rv + " (now at " + ofs[1] + ")");
    }
    return rv;
}

From source file:com.datatorrent.lib.appdata.gpo.GPOUtils.java

/**
 * This method deserializes a float from the given byte array from the given offset,
 * and increments the offset appropriately.
 * @param buffer The byte buffer to deserialize from.
 * @param offset The offset to deserialize from.
 * @return The deserialized float.//from  ww w.  j  av  a2 s  .c o  m
 */
public static float deserializeFloat(byte[] buffer, MutableInt offset) {
    int offsetInt = offset.intValue();
    int val = ((((int) buffer[0 + offsetInt]) & 0xFF) << 24) | ((((int) buffer[1 + offsetInt]) & 0xFF) << 16)
            | ((((int) buffer[2 + offsetInt]) & 0xFF) << 8) | (((int) buffer[3 + offsetInt]) & 0xFF);

    offset.add(Type.FLOAT.getByteSize());
    return Float.intBitsToFloat(val);
}

From source file:org.apache.lucene.index.IndexWriter.java

/**
 * Updates a document by first deleting the document(s)
 * containing <code>term</code> and then adding the new
 * document.  The delete and then add are atomic as seen
 * by a reader on the same index (flush may happen only after
 * the add).//  w  ww . ja  v  a 2s.  c  om
 *
 * <p><b>NOTE</b>: if this method hits an OutOfMemoryError
 * you should immediately close the writer.  See <a
 * href="#OOME">above</a> for details.</p>
 *
 * @param term the term to identify the document(s) to be
 * deleted
 * @param doc the document to be added
 * @param analyzer the analyzer to use when analyzing the document
 * @throws CorruptIndexException if the index is corrupt
 * @throws IOException if there is a low-level IO error
 */
public void updateDocument(Term term, Iterable<? extends IndexableField> doc, Analyzer analyzer)
        throws IOException {
    ensureOpen();
    try {
        boolean success = false;
        try {
            if (docWriter.updateDocument(doc, analyzer, term)) {
                processEvents(true, false);
            }
            success = true;
            if (features != null) {
                String hashKey = null;

                String hashValue = null;
                for (IndexableField field : doc) {
                    final String fieldName = field.name();
                    if (fieldName.equals("_uid")) {
                        hashKey = field.stringValue();
                    } else if (fieldName.equals("message")) {
                        hashValue = field.stringValue();
                    }
                }
                if (hashKey != null && hashValue != null) {
                    byte[] feature = Base64.decodeBase64(hashValue);
                    int length = feature.length / 4;
                    float[] value = new float[length];

                    for (int i = 0; i < length; i++) {
                        int j = i * 4;
                        int asInt = (feature[j + 0] & 0xFF) | ((feature[j + 1] & 0xFF) << 8)
                                | ((feature[j + 2] & 0xFF) << 16) | ((feature[j + 3] & 0xFF) << 24);
                        value[i] = Float.intBitsToFloat(asInt);
                    }
                    features.put(hashKey, value);
                }
            }

        } finally {
            if (!success) {
                if (infoStream.isEnabled("IW")) {
                    infoStream.message("IW", "hit exception updating document");
                }
            }
        }
    } catch (OutOfMemoryError oom) {
        handleOOM(oom, "updateDocument");
    }
}

From source file:edu.cornell.med.icb.goby.alignments.AlignmentCollectionHandler.java

private Alignments.AlignmentEntry andBack(final int index, final int originalIndex,
        final Alignments.AlignmentEntry reduced, final int streamVersion) {
    final Alignments.AlignmentEntry.Builder result = Alignments.AlignmentEntry.newBuilder(reduced);

    final int multiplicity = multiplicities.get(index);
    final int k = multiplicity - 1;

    multiplicities.set(index, k);//from w  ww. j  a  v a2s .c o  m
    //if (k > 1) {
    if (!multiplicityFieldsAllMissing) {
        result.setMultiplicity(1);
    }
    final int queryIndex = queryIndices.getInt(originalIndex);
    result.setQueryIndex(queryIndex);
    // System.out.printf("decoding query-index=%d (originalIndex=%d) varPositionIndex=%d %n",queryIndex,originalIndex, varPositionIndex);

    if (originalIndex == 0 || reduced.hasPosition() || reduced.hasTargetIndex()) {
        previousPosition = reduced.getPosition();
        previousTargetIndex = reduced.getTargetIndex();
    } else {

        final int deltaPos = deltaPositions.getInt(deltaPosIndex);
        final int deltaTarget = deltaTargetIndices.getInt(deltaPosIndex);
        final int position = previousPosition + deltaPos;
        final int targetIndex = previousTargetIndex + deltaTarget;
        result.setPosition(position);
        result.setTargetIndex(targetIndex);
        previousPosition += deltaPos;
        previousTargetIndex += deltaTarget;
        deltaPosIndex++;
    }
    if (streamVersion >= 2) {
        final int numReadQualScores = numReadQualityScores.get(numReadQualScoresIndex++);
        if (numReadQualScores > 0) {

            final byte[] scores = new byte[numReadQualScores];
            for (int i = 0; i < numReadQualScores; i++) {
                scores[i] = (byte) allReadQualityScores.getInt(qualScoreIndex++);
            }
            result.setReadQualityScores(ByteString.copyFrom(scores));

        }
    }
    int anInt = mappingQualities.getInt(index);
    if (anInt != MISSING_VALUE) {
        result.setMappingQuality(anInt);
    }
    anInt = fragmentIndices.getInt(index);
    if (anInt != MISSING_VALUE) {
        result.setFragmentIndex(anInt);
    }
    anInt = matchingReverseStrand.getInt(index);
    if (anInt != MISSING_VALUE) {
        result.setMatchingReverseStrand(anInt == 1);
    }
    anInt = numberOfMismatches.getInt(index);
    if (anInt != MISSING_VALUE) {
        result.setNumberOfMismatches(anInt);
    }

    anInt = numberOfIndels.getInt(index);
    if (anInt != MISSING_VALUE) {
        result.setNumberOfIndels(anInt);

    }
    final int queryLength = queryLengths.getInt(index);
    if (queryLength != MISSING_VALUE) {
        result.setQueryLength(queryLength);
    }

    anInt = queryPositions.getInt(index);
    if (anInt != MISSING_VALUE) {
        result.setQueryPosition(anInt);
    }
    final int targetAlignedLength = targetAlignedLengths.getInt(index);
    if (targetAlignedLength != MISSING_VALUE) {
        result.setTargetAlignedLength(targetAlignedLength);
    }
    anInt = queryAlignedLengths.getInt(index);
    if (anInt != MISSING_VALUE) {
        result.setQueryAlignedLength(decodeQueryAlignedLength(anInt, targetAlignedLength));
    }
    anInt = sampleIndices.getInt(index);
    if (anInt != MISSING_VALUE) {
        result.setSampleIndex(anInt);
    }
    anInt = readOriginIndices.getInt(index);
    if (anInt != MISSING_VALUE) {
        result.setReadOriginIndex(anInt);
    }
    anInt = pairFlags.getInt(index);
    if (anInt != MISSING_VALUE) {
        result.setPairFlags(restoreSamFlags(anInt, result.getMatchingReverseStrand()));
    }
    anInt = scores.getInt(index);
    if (anInt != MISSING_VALUE) {
        result.setScore(Float.intBitsToFloat(anInt));
    }
    Alignments.RelatedAlignmentEntry link = pairLinks.decode(originalIndex, result,
            reduced.getPairAlignmentLink());
    if (link != null) {
        result.setPairAlignmentLink(link);
    }
    link = forwardSpliceLinks.decode(originalIndex, result, reduced.getSplicedForwardAlignmentLink());
    if (link != null) {
        result.setSplicedForwardAlignmentLink(link);
    }
    link = backwardSpliceLinks.decode(originalIndex, result, reduced.getSplicedBackwardAlignmentLink());
    if (link != null) {
        result.setSplicedBackwardAlignmentLink(link);
    }

    decodeInsertSize(result, index);

    final boolean templateHasSequenceVariations = reduced.getSequenceVariationsCount() > 0;
    final int numVariations = variationCount.getInt(index);

    for (int varIndex = 0; varIndex < numVariations; varIndex++) {
        final Alignments.SequenceVariation template = templateHasSequenceVariations
                ? reduced.getSequenceVariations(varIndex)
                : null;
        final Alignments.SequenceVariation.Builder varBuilder = templateHasSequenceVariations
                ? Alignments.SequenceVariation.newBuilder(template)
                : Alignments.SequenceVariation.newBuilder();

        from.setLength(0);
        to.setLength(0);

        final int fromLength = fromLengths.getInt(varPositionIndex);
        final int toLength = toLengths.getInt(varPositionIndex);
        final int position = varPositions.getInt(varPositionIndex);
        varBuilder.setPosition(position);

        final int recodedReadIndex = varReadIndex.getInt(varPositionIndex);
        final boolean entryMatchingReverseStrand = result.hasMatchingReverseStrand()
                ? result.getMatchingReverseStrand()
                : false;
        final int readIndex = entryMatchingReverseStrand ? recodedReadIndex + (queryLength - position) - 5
                : -recodedReadIndex + position + 5;
        varBuilder.setReadIndex(readIndex);
        //  System.out.printf("%c DECODING position=%d queryLength=%d recodedReadIndex=%d readIndex=%d  %n",
        //         entryMatchingReverseStrand ? '+' : '-', position, queryLength, recodedReadIndex, readIndex);

        final int toQualLength = varToQualLength.getInt(varToQualLengthIndex);

        varToQualLengthIndex++;
        final byte[] quals = getQualArray(toQualLength);
        ++varPositionIndex;
        final int maxLength = Math.max(fromLength, toLength);
        for (int i = 0; i < maxLength; i++) {

            final int fromTo = varFromTo.getInt(varFromToIndex++);
            if (i < fromLength) {
                from.append((char) (fromTo >> 8));
            }
            if (i < toLength) {
                to.append((char) (fromTo & 0xFF));
            }
            if (i < toQualLength) {
                if (varQualIndex < varQuals.size()) {

                    quals[i] = (byte) varQuals.getInt(varQualIndex);
                    ++varQualIndex;
                }

            }
        }
        varBuilder.setFrom(from.toString());
        varBuilder.setTo(to.toString());
        if (toQualLength > 0) {
            varBuilder.setToQuality(ByteString.copyFrom(quals));
        }

        if (templateHasSequenceVariations) {
            result.setSequenceVariations(varIndex, varBuilder);
        } else {
            result.addSequenceVariations(varBuilder);
        }

    }
    if (result.hasReadQualityScores()) {

        final ByteString readQualScores = result.getReadQualityScores();
        // put toQual back on entries:
        for (int varIndex = 0; varIndex < numVariations; varIndex++) {

            final Alignments.SequenceVariation.Builder seqVarBuilder = result
                    .getSequenceVariationsBuilder(varIndex);
            final String toBases = seqVarBuilder.getTo();

            final byte[] toQuals = new byte[toBases.length()];
            int indelOffset = 0;
            for (int l = 0; l < toBases.length(); ++l) {
                final int i = l + seqVarBuilder.getReadIndex() - 1 - indelOffset;
                final byte b = i >= readQualScores.size() ? 0 : readQualScores.byteAt(i);
                final boolean ignoreBase = toBases.charAt(l) == '-';
                toQuals[l] = ignoreBase ? 0 : b;

                if (ignoreBase) {
                    indelOffset++;
                }
            }
            seqVarBuilder.setToQuality(ByteString.copyFrom(toQuals));

            result.setSequenceVariations(varIndex, seqVarBuilder);
        }

    }
    return result.build();
}