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:com.antsdb.saltedfish.server.mysql.util.BufferUtils.java

public static float readFloat(ByteBuf in) {
    return Float.intBitsToFloat(readLong(in));
}

From source file:org.apache.flex.swf.io.InputBitStream.java

@Override
public float readFLOAT() {
    return Float.intBitsToFloat(readSI32());
}

From source file:com.google.code.or.binlog.impl.parser.AbstractRowEventParser.java

/**
 * /*  w  w w.  j  a  v  a  2s.c  o  m*/
 */
protected Row parseRow(XInputStream is, TableMapEvent tme, BitColumn usedColumns) throws IOException {
    //
    int unusedColumnCount = 0;
    final byte[] types = tme.getColumnTypes();
    final Metadata metadata = tme.getColumnMetadata();
    final BitColumn nullColumns = is.readBit(types.length, true);
    final List<Column> columns = new ArrayList<Column>(types.length);
    for (int i = 0; i < types.length; ++i) {
        //
        int length = 0;
        final int meta = metadata.getMetadata(i);
        int type = CodecUtils.toUnsigned(types[i]);
        int fspLen = 0; // fraction second part length for time data types
        if (type == MySQLConstants.TYPE_STRING && meta > 256) {
            final int meta0 = meta >> 8;
            final int meta1 = meta & 0xFF;
            if ((meta0 & 0x30) != 0x30) { // a long CHAR() field: see #37426
                type = meta0 | 0x30;
                length = meta1 | (((meta0 & 0x30) ^ 0x30) << 4);
            } else {
                switch (meta0) {
                case MySQLConstants.TYPE_SET:
                case MySQLConstants.TYPE_ENUM:
                case MySQLConstants.TYPE_STRING:
                    type = meta0;
                    length = meta1;
                    break;
                default:
                    throw new NestableRuntimeException("assertion failed, unknown column type: " + type);
                }
            }
        } else if (type == MySQLConstants.TYPE_TIMESTAMP2 || type == MySQLConstants.TYPE_TIME2
                || type == MySQLConstants.TYPE_DATETIME2) {
            // http://dev.mysql.com/doc/internals/en/date-and-time-data-type-representation.html
            fspLen = meta > 0 ? (meta + 1) >> 1 : 0;
        }

        if (!usedColumns.get(i)) {
            unusedColumnCount++;
            continue;
        } else if (nullColumns.get(i - unusedColumnCount)) {
            columns.add(NullColumn.valueOf(type));
            continue;
        }

        //
        switch (type) {
        case MySQLConstants.TYPE_TINY:
            columns.add(TinyColumn.valueOf(is.readInt(1)));
            break;
        case MySQLConstants.TYPE_SHORT:
            columns.add(ShortColumn.valueOf(is.readInt(2)));
            break;
        case MySQLConstants.TYPE_INT24:
            columns.add(Int24Column.valueOf(is.readInt(3)));
            break;
        case MySQLConstants.TYPE_LONG:
            columns.add(LongColumn.valueOf(is.readInt(4)));
            break;
        case MySQLConstants.TYPE_LONGLONG:
            columns.add(LongLongColumn.valueOf(is.readLong(8)));
            break;
        case MySQLConstants.TYPE_FLOAT:
            columns.add(FloatColumn.valueOf(Float.intBitsToFloat(is.readInt(4))));
            break;
        case MySQLConstants.TYPE_DOUBLE:
            columns.add(DoubleColumn.valueOf(Double.longBitsToDouble(is.readLong(8))));
            break;
        case MySQLConstants.TYPE_YEAR:
            columns.add(YearColumn.valueOf(MySQLUtils.toYear(is.readInt(1))));
            break;
        case MySQLConstants.TYPE_DATE:
            columns.add(DateColumn.valueOf(MySQLUtils.toDate(is.readInt(3))));
            break;
        case MySQLConstants.TYPE_TIME:
            columns.add(TimeColumn.valueOf(MySQLUtils.toTime(is.readInt(3))));
            break;
        case MySQLConstants.TYPE_TIMESTAMP2:
            columns.add(TimestampColumn.valueOf(MySQLUtils.toTimestamp(
                    CodecUtils.convertBigEndianToLong(is.readBytes(4), 4),
                    fspLen > 0 ? (int) CodecUtils.convertBigEndianToLong(is.readBytes(fspLen), fspLen) : 0,
                    fspLen)));
            break;
        case MySQLConstants.TYPE_TIME2:
            columns.add(TimeColumn.valueOf(MySQLUtils.toTime(
                    CodecUtils.convertBigEndianToLong(is.readBytes(3), 3),
                    fspLen > 0 ? (int) CodecUtils.convertBigEndianToLong(is.readBytes(fspLen), fspLen) : 0,
                    fspLen)));
            break;
        case MySQLConstants.TYPE_DATETIME2:
            columns.add(DatetimeColumn.valueOf(MySQLUtils.toDatetime(
                    CodecUtils.convertBigEndianToLong(is.readBytes(5), 5),
                    fspLen > 0 ? (int) CodecUtils.convertBigEndianToLong(is.readBytes(fspLen), fspLen) : 0,
                    fspLen)));
            break;
        case MySQLConstants.TYPE_TIMESTAMP:
            columns.add(TimestampColumn.valueOf(MySQLUtils.toTimestamp(is.readLong(4))));
            break;
        case MySQLConstants.TYPE_DATETIME:
            columns.add(DatetimeColumn.valueOf(MySQLUtils.toDatetime(is.readLong(8))));
            break;
        case MySQLConstants.TYPE_ENUM:
            columns.add(EnumColumn.valueOf(is.readInt(length)));
            break;
        case MySQLConstants.TYPE_SET:
            columns.add(SetColumn.valueOf(is.readLong(length)));
            break;
        case MySQLConstants.TYPE_STRING:
            final int stringLength = length < 256 ? is.readInt(1) : is.readInt(2);
            columns.add(is.readFixedLengthString(stringLength));
            break;
        case MySQLConstants.TYPE_BIT:
            final int bitLength = (meta >> 8) * 8 + (meta & 0xFF);
            columns.add(is.readBit(bitLength, false));
            break;
        case MySQLConstants.TYPE_NEWDECIMAL:
            final int precision = meta & 0xFF;
            final int scale = meta >> 8;
            final int decimalLength = MySQLUtils.getDecimalBinarySize(precision, scale);
            columns.add(DecimalColumn.valueOf(
                    MySQLUtils.toDecimal(precision, scale, is.readBytes(decimalLength)), precision, scale));
            break;
        case MySQLConstants.TYPE_BLOB:
            final int blobLength = is.readInt(meta);
            columns.add(BlobColumn.valueOf(is.readBytes(blobLength)));
            break;
        case MySQLConstants.TYPE_VARCHAR:
        case MySQLConstants.TYPE_VAR_STRING:
            final int varcharLength = meta < 256 ? is.readInt(1) : is.readInt(2);
            columns.add(is.readFixedLengthString(varcharLength));
            break;
        default:
            throw new NestableRuntimeException("assertion failed, unknown column type: " + type);
        }
    }
    return new Row(columns);
}

From source file:TypeConversionHelper.java

/**
 * Convert a byte[] into an instance of our value class.
 *
 * @param buf byte array to be converted
 *
 * @return converted float array as object
 *///  w  w  w . j a  v  a  2s .c  o m
public static Object getFloatArrayFromByteArray(byte[] buf) {
    int n = buf.length / 4;
    float[] a = new float[n];
    int i = 0;
    int j = 0;

    for (; i < n;) {
        a[i++] = Float.intBitsToFloat(((buf[j++] & 0xFF) << 24) + ((buf[j++] & 0xFF) << 16)
                + ((buf[j++] & 0xFF) << 8) + (buf[j++] & 0xFF));
    }

    return a;
}

From source file:com.dianping.puma.parser.mysql.event.AbstractRowsEvent.java

/**
 * /*from w  w w  . j a v  a2 s. c o  m*/
 * @see http://code.google.com/p/open-replicator/
 * @param buf
 * @param usedColumns
 * @return
 * @throws IOException
 */
protected Row parseRow(ByteBuffer buf, BitSet usedColumns) throws IOException {
    int unusedColumnCount = 0;
    byte[] types = tableMapEvent.getColumnTypes();
    Metadata metadata = tableMapEvent.getColumnMetadata();
    BitSet nullColumns = PacketUtils.readBitSet(buf, types.length);
    List<Column> columns = new ArrayList<Column>(types.length);
    for (int i = 0; i < types.length; ++i) {
        int length = 0;
        int meta = metadata.getMetadata(i);
        int type = CodecUtils.toUnsigned(types[i]);
        if (type == BinlogConstants.MYSQL_TYPE_STRING && meta > 256) {
            int meta0 = meta >> 8;
            int meta1 = meta & 0xFF;
            if ((meta0 & 0x30) != 0x30) {
                type = meta0 | 0x30;
                length = meta1 | (((meta0 & 0x30) ^ 0x30) << 4);
            } else {
                switch (meta0) {
                case BinlogConstants.MYSQL_TYPE_SET:
                case BinlogConstants.MYSQL_TYPE_ENUM:
                case BinlogConstants.MYSQL_TYPE_STRING:
                    type = meta0;
                    length = meta1;
                    break;
                default:
                    throw new NestableRuntimeException("assertion failed, unknown column type: " + type);
                }
            }
        }

        if (!usedColumns.get(i)) {
            unusedColumnCount++;
            continue;
        } else if (nullColumns.get(i - unusedColumnCount)) {
            columns.add(NullColumn.valueOf(type));
            continue;
        }

        int value = 0;
        switch (type) {
        case BinlogConstants.MYSQL_TYPE_TINY:
            value = PacketUtils.readInt(buf, 1);
            value = (value << 24) >> 24;
            columns.add(TinyColumn.valueOf(value));
            break;
        case BinlogConstants.MYSQL_TYPE_SHORT:
            value = PacketUtils.readInt(buf, 2);
            value = (value << 16) >> 16;
            columns.add(ShortColumn.valueOf(value));
            break;
        case BinlogConstants.MYSQL_TYPE_INT24:
            value = PacketUtils.readInt(buf, 3);
            value = (value << 8) >> 8;
            columns.add(Int24Column.valueOf(value));
            break;
        case BinlogConstants.MYSQL_TYPE_INT:
            columns.add(IntColumn.valueOf(PacketUtils.readInt(buf, 4)));
            break;
        case BinlogConstants.MYSQL_TYPE_LONGLONG:
            columns.add(LongLongColumn.valueOf(PacketUtils.readLong(buf, 8)));
            break;
        case BinlogConstants.MYSQL_TYPE_FLOAT:
            columns.add(FloatColumn.valueOf(Float.intBitsToFloat(PacketUtils.readInt(buf, 4))));
            break;
        case BinlogConstants.MYSQL_TYPE_DOUBLE:
            columns.add(DoubleColumn.valueOf(Double.longBitsToDouble(PacketUtils.readLong(buf, 8))));
            break;
        case BinlogConstants.MYSQL_TYPE_YEAR:
            columns.add(YearColumn.valueOf(MySQLUtils.toYear((short) PacketUtils.readInt(buf, 1))));
            break;
        case BinlogConstants.MYSQL_TYPE_DATE:
            columns.add(DateColumn.valueOf(MySQLUtils.toDate(PacketUtils.readInt(buf, 3))));
            break;
        case BinlogConstants.MYSQL_TYPE_TIME:
            columns.add(TimeColumn.valueOf(MySQLUtils.toTime(PacketUtils.readInt(buf, 3))));
            break;
        case BinlogConstants.MYSQL_TYPE_TIMESTAMP:
            columns.add(TimestampColumn.valueOf(PacketUtils.readLong(buf, 4)));
            break;
        case BinlogConstants.MYSQL_TYPE_DATETIME:
            columns.add(DatetimeColumn.valueOf(MySQLUtils.toDatetime(PacketUtils.readLong(buf, 8))));
            break;
        case BinlogConstants.MYSQL_TYPE_ENUM:
            columns.add(EnumColumn.valueOf(PacketUtils.readInt(buf, length)));
            break;
        case BinlogConstants.MYSQL_TYPE_SET:
            columns.add(SetColumn.valueOf(PacketUtils.readLong(buf, length)));
            break;
        case BinlogConstants.MYSQL_TYPE_STRING:
            final int stringLength = length < 256 ? PacketUtils.readInt(buf, 1) : PacketUtils.readInt(buf, 2);
            columns.add(StringColumn.valueOf(PacketUtils.readBytes(buf, stringLength)));
            break;
        case BinlogConstants.MYSQL_TYPE_BIT:
            final int bitLength = (meta >> 8) * 8 + (meta & 0xFF);
            columns.add(BitColumn.valueOf(bitLength, PacketUtils.readBit(buf, bitLength, false)));
            break;
        case BinlogConstants.MYSQL_TYPE_NEWDECIMAL:
            final int precision = meta & 0xFF;
            final int scale = meta >> 8;
            final int decimalLength = MySQLUtils.getDecimalBinarySize(precision, scale);
            columns.add(DecimalColumn.valueOf(
                    MySQLUtils.toDecimal(precision, scale, PacketUtils.readBytes(buf, decimalLength)),
                    precision, scale));
            break;
        case BinlogConstants.MYSQL_TYPE_BLOB:
            final int blobLength = PacketUtils.readInt(buf, meta);
            columns.add(BlobColumn.valueOf(PacketUtils.readBytes(buf, blobLength)));
            break;
        case BinlogConstants.MYSQL_TYPE_VARCHAR:
        case BinlogConstants.MYSQL_TYPE_VAR_STRING:
            final int varcharLength = meta < 256 ? PacketUtils.readInt(buf, 1) : PacketUtils.readInt(buf, 2);
            columns.add(StringColumn.valueOf(PacketUtils.readBytes(buf, varcharLength)));
            break;
        case BinlogConstants.MYSQL_TYPE_TIME2:
            final int timeValue = PacketUtils.readInt(buf, 3, false);
            final int timeNanos = PacketUtils.readInt(buf, (meta + 1) / 2, false);
            columns.add(Time2Column.valueOf(MySQLUtils.toTime2(timeValue, timeNanos, meta)));
            break;
        case BinlogConstants.MYSQL_TYPE_DATETIME2:
            final long dateTimeValue = PacketUtils.readLong(buf, 5, false);
            final int dateTimenanos = PacketUtils.readInt(buf, (meta + 1) / 2, false);
            columns.add(Datetime2Column.valueOf(MySQLUtils.toDatetime2(dateTimeValue, dateTimenanos, meta)));
            break;
        case BinlogConstants.MYSQL_TYPE_TIMESTAMP2:
            final long timeStampValue = PacketUtils.readLong(buf, 4, false);
            final int timeStampNanos = PacketUtils.readInt(buf, (meta + 1) / 2, false);
            columns.add(
                    Timestamp2Column.valueOf(MySQLUtils.toTimestamp2(timeStampValue, timeStampNanos, meta)));
            break;
        default:
            throw new NestableRuntimeException("assertion failed, unknown column type: " + type);
        }
    }
    return new Row(columns);
}

From source file:com.bizosys.hsearch.index.DocMeta.java

public int fromBytes(byte[] bytes, int pos) {
    byte docTypeLen = bytes[pos];
    pos++;/* www  . j a  va2s . c o  m*/
    if (0 != docTypeLen) {
        byte[] docTypeB = new byte[docTypeLen];
        System.arraycopy(bytes, pos, docTypeB, 0, docTypeLen);
        this.docType = Storable.getString(docTypeB);
        pos = pos + docTypeLen;
    }

    byte stateLen = bytes[pos];
    pos++;
    if (0 != stateLen) {
        byte[] stateB = new byte[stateLen];
        System.arraycopy(bytes, pos, stateB, 0, stateLen);
        this.state = Storable.getString(stateB);
        pos = pos + stateLen;
    }

    byte orgUnitLen = bytes[pos];
    pos++;
    if (0 != orgUnitLen) {
        byte[] orgUnitB = new byte[orgUnitLen];
        System.arraycopy(bytes, pos, orgUnitB, 0, orgUnitLen);
        this.team = Storable.getString(orgUnitB);
        pos = pos + orgUnitLen;
    }

    byte geoHouseLen = bytes[pos];
    pos++;
    if (0 != geoHouseLen) {
        byte[] geoHouseB = new byte[geoHouseLen];
        System.arraycopy(bytes, pos, geoHouseB, 0, geoHouseLen);
        this.geoHouse = Storable.getString(geoHouseB);
        pos = pos + geoHouseLen;
    }

    byte flag_1B = bytes[pos++];
    boolean[] flag_1 = Storable.byteToBits(flag_1B);

    byte flag_2B = bytes[pos++];
    boolean[] flag_2 = Storable.byteToBits(flag_2B);

    int bitPos = 0;
    if (flag_1[bitPos++]) {
        this.eastering = Float.intBitsToFloat(Storable.getInt(pos, bytes));
        pos = pos + 4;
    }

    if (flag_1[bitPos++]) {
        this.northing = Float.intBitsToFloat(Storable.getInt(pos, bytes));
        pos = pos + 4;
    }

    if (flag_1[bitPos++]) {
        this.weight = Storable.getInt(pos, bytes);
        pos = pos + 4;
    }

    if (flag_1[bitPos++]) {
        this.ipHouse = Storable.getInt(pos, bytes);
        pos = pos + 4;
    }

    this.securityHigh = flag_1[bitPos++];
    this.sentimentPositive = flag_1[bitPos++];

    if (flag_1[bitPos++]) {
        short len = Storable.getShort(pos, bytes);
        pos = pos + 2;
        byte[] tagsB = new byte[len];
        System.arraycopy(bytes, pos, tagsB, 0, len);
        this.tags = Storable.getString(tagsB);
        pos = pos + tagsB.length;
    }

    if (flag_1[bitPos++]) {
        short len = Storable.getShort(pos, bytes);
        pos = pos + 2;
        byte[] socialTextB = new byte[len];
        System.arraycopy(bytes, pos, socialTextB, 0, len);
        this.socialText = Storable.getString(socialTextB);
        pos = pos + socialTextB.length;
    }

    bitPos = 0;
    if (flag_2[bitPos++]) {
        this.createdOn = new Date(Storable.getLong(pos, bytes));
        pos = pos + 8;
    }

    if (flag_2[bitPos++]) {
        this.modifiedOn = new Date(Storable.getLong(pos, bytes));
        pos = pos + 8;
    }

    if (flag_2[bitPos++]) {
        this.validTill = new Date(Storable.getLong(pos, bytes));
        pos = pos + 8;
    }
    return pos;
}

From source file:tudarmstadt.lt.ABSentiment.featureExtractor.util.W2vSpace.java

/**
 * Read a Vector - Array of Floats from the binary model
 * @param ds input data stream//from   w  w  w  .  j  a  va 2s .  co m
 * @param vectorSize length of each word vector
 * @return an array of float containing the word vector representation
 */
public static float[] readFloatVector(DataInputStream ds, int vectorSize) throws IOException {
    // Vector is an Array of Floats...
    float[] vector = new float[vectorSize];
    // Floats stored as 4 bytes
    byte[] vectorBuffer = new byte[4 * vectorSize];
    // Read the full vector in a single chunk:
    ds.read(vectorBuffer);
    // Parse bytes into floats
    for (int i = 0; i < vectorSize; i++) {
        // & with 0xFF to get unsigned byte value as int
        int byte1 = (vectorBuffer[(i * 4) + 0] & 0xFF) << 0;
        int byte2 = (vectorBuffer[(i * 4) + 1] & 0xFF) << 8;
        int byte3 = (vectorBuffer[(i * 4) + 2] & 0xFF) << 16;
        int byte4 = (vectorBuffer[(i * 4) + 3] & 0xFF) << 24;
        // Encode the 4 byte values (0-255) above into a single int
        // Reverse bytes for endian compatibility
        int reverseBytes = (byte1 | byte2 | byte3 | byte4);
        vector[i] = Float.intBitsToFloat(reverseBytes);
    }
    return vector;
}

From source file:org.apache.ignite.GridTestIoUtils.java

/**
 * Gets float value from byte array assuming that value stored in little-endian byte order.
 *
 * @param arr Byte array.//from w  w  w .  j  av a2s.  c  o  m
 * @param off Offset.
 */
public static float getFloatByByteLE(byte[] arr, int off) {
    return Float.intBitsToFloat(getIntByByteLE(arr, off));
}

From source file:org.lwes.serializer.Deserializer.java

public static Float deserializeFLOAT(DeserializerState myState, byte[] bytes) {
    int off = myState.currentIndex();
    int i = ((bytes[off + 3] & 0xFF) << 0) + ((bytes[off + 2] & 0xFF) << 8) + ((bytes[off + 1] & 0xFF) << 16)
            + ((bytes[off + 0]) << 24);
    myState.incr(4);//from w w w .  j  a va  2  s  .co  m
    return Float.intBitsToFloat(i);
}

From source file:Main.java

/**
 * Returns the floating-point number adjacent to the first
 * argument in the direction of the second argument.  If both
 * arguments compare as equal, the second argument is returned.
 *
 * <p>//www  .j av a2s  .  co  m
 * Special cases:
 * <ul>
 * <li> If either argument is a NaN, then NaN is returned.
 *
 * <li> If both arguments are signed zeros, a <code>float</code>
 * zero with the same sign as <code>direction</code> is returned
 * (as implied by the requirement of returning the second argument
 * if the arguments compare as equal).
 *
 * <li> If <code>start</code> is
 * &plusmn;<code>Float.MIN_VALUE</code> and <code>direction</code>
 * has a value such that the result should have a smaller
 * magnitude, then a zero with the same sign as <code>start</code>
 * is returned.
 *
 * <li> If <code>start</code> is infinite and
 * <code>direction</code> has a value such that the result should
 * have a smaller magnitude, <code>Float.MAX_VALUE</code> with the
 * same sign as <code>start</code> is returned.
 *
 * <li> If <code>start</code> is equal to &plusmn;
 * <code>Float.MAX_VALUE</code> and <code>direction</code> has a
 * value such that the result should have a larger magnitude, an
 * infinity with same sign as <code>start</code> is returned.
 * </ul>
 *
 * @param start     starting floating-point value
 * @param direction value indicating which of
 * <code>start</code>'s neighbors or <code>start</code> should
 * be returned
 * @return The floating-point number adjacent to <code>start</code> in the
 * direction of <code>direction</code>.
 * @author Joseph D. Darcy
 */
public static float nextAfter(float start, double direction) {
    /*
     * The cases:
     *
     * nextAfter(+infinity, 0)  == MAX_VALUE
     * nextAfter(+infinity, +infinity)  == +infinity
     * nextAfter(-infinity, 0)  == -MAX_VALUE
     * nextAfter(-infinity, -infinity)  == -infinity
     *
     * are naturally handled without any additional testing
     */

    // First check for NaN values
    if (isNaN(start) || isNaN(direction)) {
        // return a NaN derived from the input NaN(s)
        return start + (float) direction;
    } else if (start == direction) {
        return (float) direction;
    } else { // start > direction or start < direction
        // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0)
        // then bitwise convert start to integer.
        int transducer = Float.floatToRawIntBits(start + 0.0f);

        /*
         * IEEE 754 floating-point numbers are lexicographically
         * ordered if treated as signed- magnitude integers .
         * Since Java's integers are two's complement,
         * incrementing" the two's complement representation of a
         * logically negative floating-point value *decrements*
         * the signed-magnitude representation. Therefore, when
         * the integer representation of a floating-point values
         * is less than zero, the adjustment to the representation
         * is in the opposite direction than would be expected at
         * first.
         */
        if (direction > start) {// Calculate next greater value
            transducer = transducer + (transducer >= 0 ? 1 : -1);
        } else { // Calculate next lesser value
            assert direction < start;
            if (transducer > 0)
                --transducer;
            else if (transducer < 0)
                ++transducer;
            /*
             * transducer==0, the result is -MIN_VALUE
             *
             * The transition from zero (implicitly
             * positive) to the smallest negative
             * signed magnitude value must be done
             * explicitly.
             */
            else
                transducer = FloatConsts.SIGN_BIT_MASK | 1;
        }

        return Float.intBitsToFloat(transducer);
    }
}