Example usage for java.io DataOutput writeFloat

List of usage examples for java.io DataOutput writeFloat

Introduction

In this page you can find the example usage for java.io DataOutput writeFloat.

Prototype

void writeFloat(float v) throws IOException;

Source Link

Document

Writes a float value, which is comprised of four bytes, to the output stream.

Usage

From source file:hivemall.fm.FFMPredictionModel.java

private static void writeEntry(@Nonnull final Entry e, final int factors, @Nonnull final float[] Vf,
        @Nonnull final DataOutput out) throws IOException {
    final float W = e.getW();
    e.getV(Vf);//w w  w.ja  v  a2  s  .  c  o m

    if (ArrayUtils.almostEquals(Vf, 0.f)) {
        if (HalfFloat.isRepresentable(W)) {
            out.writeByte(W_ONLY_HALF_FLOAT_ENTRY);
            out.writeShort(HalfFloat.floatToHalfFloat(W));
        } else {
            out.writeByte(W_ONLY_FLOAT_ENTRY);
            out.writeFloat(W);
        }
    } else if (isRepresentableAsHalfFloat(W, Vf)) {
        out.writeByte(HALF_FLOAT_ENTRY);
        out.writeShort(HalfFloat.floatToHalfFloat(W));
        for (int i = 0; i < factors; i++) {
            out.writeShort(HalfFloat.floatToHalfFloat(Vf[i]));
        }
    } else {
        out.writeByte(FLOAT_ENTRY);
        out.writeFloat(W);
        IOUtils.writeFloats(Vf, factors, out);
    }
}

From source file:com.chinamobile.bcbsp.examples.bytearray.pagerank.PRVertexLiteNew.java

@Override
public void write(DataOutput out) throws IOException {
    out.writeInt(this.vertexID);
    out.writeFloat(this.vertexValue);
}

From source file:edu.nyu.vida.data_polygamy.scalar_function.Median.java

@Override
public void write(DataOutput out) throws IOException {
    out.writeInt(count);//  www  . j  av a  2  s. c  om
    out.writeInt(floatValues.size());
    for (int i = 0; i < floatValues.size(); i++)
        out.writeFloat(floatValues.get(i));
}

From source file:dk.statsbiblioteket.util.LineReaderTest.java

public void writeSample(DataOutput out) throws Exception {
    out.writeInt(12345);/*from w  ww . ja  va 2 s  . c o m*/
    out.writeInt(-87);
    out.writeLong(123456789L);
    out.write("Hello World!\n".getBytes("utf-8"));
    out.write("Another world\n".getBytes("utf-8"));
    out.writeFloat(0.5f);
    out.writeBoolean(true);
    out.writeBoolean(false);
    out.writeByte(12);
    out.writeByte(-12);
    out.write(129);
    out.writeShort(-4567);
    out.writeBytes("ASCII");
}

From source file:com.chinamobile.bcbsp.sync.SuperStepReportContainer.java

@Override
public void write(DataOutput out) throws IOException {
    out.writeInt(this.partitionId);
    out.writeInt(this.stageFlag);
    out.writeInt(this.dirFlag.length);
    int count = this.dirFlag.length;
    for (int i = 0; i < count; i++) {
        Text.writeString(out, this.dirFlag[i]);
    }/*from ww  w.ja  va2s .com*/
    out.writeLong(this.judgeFlag);
    out.writeInt(this.localBarrierNum);
    out.writeInt(this.port1);
    out.writeInt(this.port2);
    out.writeInt(this.aggValues.length);
    count = this.aggValues.length;
    for (int i = 0; i < count; i++) {
        Text.writeString(out, this.aggValues[i]);
    }
    out.writeLong(this.staffRunTime);
    out.writeInt(this.sid);
    out.writeInt(this.currentSuperStep);
    out.writeLong(this.migrateCost);
    out.writeFloat(this.splitEdgefactor);
}

From source file:org.apache.hadoop.hbase.io.HbaseObjectWritable.java

/**
 * Write a {@link Writable}, {@link String}, primitive type, or an array of
 * the preceding./*  w  ww  . ja  v  a 2s . c  o m*/
 * @param out
 * @param instance
 * @param declaredClass
 * @param conf
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public static void writeObject(DataOutput out, Object instance, Class declaredClass, Configuration conf)
        throws IOException {

    Object instanceObj = instance;
    Class declClass = declaredClass;

    if (instanceObj == null) { // null
        instanceObj = new NullInstance(declClass, conf);
        declClass = Writable.class;
    }
    writeClassCode(out, declClass);
    if (declClass.isArray()) { // array
        // If bytearray, just dump it out -- avoid the recursion and
        // byte-at-a-time we were previously doing.
        if (declClass.equals(byte[].class)) {
            Bytes.writeByteArray(out, (byte[]) instanceObj);
        } else if (declClass.equals(Result[].class)) {
            Result.writeArray(out, (Result[]) instanceObj);
        } else {
            //if it is a Generic array, write the element's type
            if (getClassCode(declaredClass) == GENERIC_ARRAY_CODE) {
                Class<?> componentType = declaredClass.getComponentType();
                writeClass(out, componentType);
            }

            int length = Array.getLength(instanceObj);
            out.writeInt(length);
            for (int i = 0; i < length; i++) {
                Object item = Array.get(instanceObj, i);
                writeObject(out, item, item.getClass(), conf);
            }
        }
    } else if (List.class.isAssignableFrom(declClass)) {
        List list = (List) instanceObj;
        int length = list.size();
        out.writeInt(length);
        for (int i = 0; i < length; i++) {
            Object elem = list.get(i);
            writeObject(out, elem, elem == null ? Writable.class : elem.getClass(), conf);
        }
    } else if (declClass == String.class) { // String
        Text.writeString(out, (String) instanceObj);
    } else if (declClass.isPrimitive()) { // primitive type
        if (declClass == Boolean.TYPE) { // boolean
            out.writeBoolean(((Boolean) instanceObj).booleanValue());
        } else if (declClass == Character.TYPE) { // char
            out.writeChar(((Character) instanceObj).charValue());
        } else if (declClass == Byte.TYPE) { // byte
            out.writeByte(((Byte) instanceObj).byteValue());
        } else if (declClass == Short.TYPE) { // short
            out.writeShort(((Short) instanceObj).shortValue());
        } else if (declClass == Integer.TYPE) { // int
            out.writeInt(((Integer) instanceObj).intValue());
        } else if (declClass == Long.TYPE) { // long
            out.writeLong(((Long) instanceObj).longValue());
        } else if (declClass == Float.TYPE) { // float
            out.writeFloat(((Float) instanceObj).floatValue());
        } else if (declClass == Double.TYPE) { // double
            out.writeDouble(((Double) instanceObj).doubleValue());
        } else if (declClass == Void.TYPE) { // void
        } else {
            throw new IllegalArgumentException("Not a primitive: " + declClass);
        }
    } else if (declClass.isEnum()) { // enum
        Text.writeString(out, ((Enum) instanceObj).name());
    } else if (Message.class.isAssignableFrom(declaredClass)) {
        Text.writeString(out, instanceObj.getClass().getName());
        ((Message) instance).writeDelimitedTo(DataOutputOutputStream.constructOutputStream(out));
    } else if (Writable.class.isAssignableFrom(declClass)) { // Writable
        Class<?> c = instanceObj.getClass();
        Integer code = CLASS_TO_CODE.get(c);
        if (code == null) {
            out.writeByte(NOT_ENCODED);
            Text.writeString(out, c.getName());
        } else {
            writeClassCode(out, c);
        }
        ((Writable) instanceObj).write(out);
    } else if (Serializable.class.isAssignableFrom(declClass)) {
        Class<?> c = instanceObj.getClass();
        Integer code = CLASS_TO_CODE.get(c);
        if (code == null) {
            out.writeByte(NOT_ENCODED);
            Text.writeString(out, c.getName());
        } else {
            writeClassCode(out, c);
        }
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(instanceObj);
            byte[] value = bos.toByteArray();
            out.writeInt(value.length);
            out.write(value);
        } finally {
            if (bos != null)
                bos.close();
            if (oos != null)
                oos.close();
        }
    } else {
        throw new IOException("Can't write: " + instanceObj + " as " + declClass);
    }
}

From source file:org.apache.hadoop.hbase.security.access.HbaseObjectWritableFor96Migration.java

/**
 * Write a {@link Writable}, {@link String}, primitive type, or an array of
 * the preceding./*from   ww w .java  2 s. co m*/
 * @param out
 * @param instance
 * @param declaredClass
 * @param conf
 * @throws IOException
 */
@SuppressWarnings("unchecked")
static void writeObject(DataOutput out, Object instance, Class declaredClass, Configuration conf)
        throws IOException {

    Object instanceObj = instance;
    Class declClass = declaredClass;

    if (instanceObj == null) { // null
        instanceObj = new NullInstance(declClass, conf);
        declClass = Writable.class;
    }
    writeClassCode(out, declClass);
    if (declClass.isArray()) { // array
        // If bytearray, just dump it out -- avoid the recursion and
        // byte-at-a-time we were previously doing.
        if (declClass.equals(byte[].class)) {
            Bytes.writeByteArray(out, (byte[]) instanceObj);
        } else {
            //if it is a Generic array, write the element's type
            if (getClassCode(declaredClass) == GENERIC_ARRAY_CODE) {
                Class<?> componentType = declaredClass.getComponentType();
                writeClass(out, componentType);
            }

            int length = Array.getLength(instanceObj);
            out.writeInt(length);
            for (int i = 0; i < length; i++) {
                Object item = Array.get(instanceObj, i);
                writeObject(out, item, item.getClass(), conf);
            }
        }
    } else if (List.class.isAssignableFrom(declClass)) {
        List list = (List) instanceObj;
        int length = list.size();
        out.writeInt(length);
        for (int i = 0; i < length; i++) {
            Object elem = list.get(i);
            writeObject(out, elem, elem == null ? Writable.class : elem.getClass(), conf);
        }
    } else if (declClass == String.class) { // String
        Text.writeString(out, (String) instanceObj);
    } else if (declClass.isPrimitive()) { // primitive type
        if (declClass == Boolean.TYPE) { // boolean
            out.writeBoolean(((Boolean) instanceObj).booleanValue());
        } else if (declClass == Character.TYPE) { // char
            out.writeChar(((Character) instanceObj).charValue());
        } else if (declClass == Byte.TYPE) { // byte
            out.writeByte(((Byte) instanceObj).byteValue());
        } else if (declClass == Short.TYPE) { // short
            out.writeShort(((Short) instanceObj).shortValue());
        } else if (declClass == Integer.TYPE) { // int
            out.writeInt(((Integer) instanceObj).intValue());
        } else if (declClass == Long.TYPE) { // long
            out.writeLong(((Long) instanceObj).longValue());
        } else if (declClass == Float.TYPE) { // float
            out.writeFloat(((Float) instanceObj).floatValue());
        } else if (declClass == Double.TYPE) { // double
            out.writeDouble(((Double) instanceObj).doubleValue());
        } else if (declClass == Void.TYPE) { // void
        } else {
            throw new IllegalArgumentException("Not a primitive: " + declClass);
        }
    } else if (declClass.isEnum()) { // enum
        Text.writeString(out, ((Enum) instanceObj).name());
    } else if (Message.class.isAssignableFrom(declaredClass)) {
        Text.writeString(out, instanceObj.getClass().getName());
        ((Message) instance).writeDelimitedTo(DataOutputOutputStream.constructOutputStream(out));
    } else if (Writable.class.isAssignableFrom(declClass)) { // Writable
        Class<?> c = instanceObj.getClass();
        Integer code = CLASS_TO_CODE.get(c);
        if (code == null) {
            out.writeByte(NOT_ENCODED);
            Text.writeString(out, c.getName());
        } else {
            writeClassCode(out, c);
        }
        ((Writable) instanceObj).write(out);
    } else if (Serializable.class.isAssignableFrom(declClass)) {
        Class<?> c = instanceObj.getClass();
        Integer code = CLASS_TO_CODE.get(c);
        if (code == null) {
            out.writeByte(NOT_ENCODED);
            Text.writeString(out, c.getName());
        } else {
            writeClassCode(out, c);
        }
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(instanceObj);
            byte[] value = bos.toByteArray();
            out.writeInt(value.length);
            out.write(value);
        } finally {
            if (bos != null)
                bos.close();
            if (oos != null)
                oos.close();
        }
    } else if (Scan.class.isAssignableFrom(declClass)) {
        Scan scan = (Scan) instanceObj;
        byte[] scanBytes = ProtobufUtil.toScan(scan).toByteArray();
        out.writeInt(scanBytes.length);
        out.write(scanBytes);
    } else {
        throw new IOException("Can't write: " + instanceObj + " as " + declClass);
    }
}

From source file:org.apache.hadoop.mapred.TaskStatus.java

public void write(DataOutput out) throws IOException {
    taskid.write(out);/*from w ww  . ja  v  a 2  s  . co  m*/
    out.writeFloat(progress);
    out.writeInt(numSlots);
    WritableUtils.writeEnum(out, runState);
    Text.writeString(out, diagnosticInfo);
    Text.writeString(out, stateString);
    WritableUtils.writeEnum(out, phase);
    out.writeLong(startTime);
    out.writeLong(finishTime);
    out.writeBoolean(includeCounters);
    out.writeLong(outputSize);
    if (includeCounters) {
        counters.write(out);
    }
    nextRecordRange.write(out);
}

From source file:org.apache.hama.bsp.TaskStatus.java

@Override
public void write(DataOutput out) throws IOException {
    jobId.write(out);//from  w  w  w  .ja va 2s .c o  m
    taskId.write(out);
    out.writeFloat(progress);
    WritableUtils.writeEnum(out, runState);
    Text.writeString(out, stateString);
    WritableUtils.writeEnum(out, phase);
    out.writeLong(startTime);
    out.writeLong(finishTime);

    counters.write(out);
}

From source file:org.apache.hawq.pxf.service.io.GPDBWritable.java

@Override
public void write(DataOutput out) throws IOException {
    int numCol = colType.length;
    boolean[] nullBits = new boolean[numCol];
    int[] colLength = new int[numCol];
    byte[] enumType = new byte[numCol];
    int[] padLength = new int[numCol];
    byte[] padbytes = new byte[8];

    /**//from   w  w w .j a v a 2s. co m
     * Compute the total payload and header length
     * header = total length (4 byte), Version (2 byte), Error (1 byte), #col (2 byte)
     * col type array = #col * 1 byte
     * null bit array = ceil(#col/8)
     */
    int datlen = 4 + 2 + 1 + 2;
    datlen += numCol;
    datlen += getNullByteArraySize(numCol);

    for (int i = 0; i < numCol; i++) {
        /* Get the enum type */
        DBType coldbtype;
        switch (DataType.get(colType[i])) {
        case BIGINT:
            coldbtype = DBType.BIGINT;
            break;
        case BOOLEAN:
            coldbtype = DBType.BOOLEAN;
            break;
        case FLOAT8:
            coldbtype = DBType.FLOAT8;
            break;
        case INTEGER:
            coldbtype = DBType.INTEGER;
            break;
        case REAL:
            coldbtype = DBType.REAL;
            break;
        case SMALLINT:
            coldbtype = DBType.SMALLINT;
            break;
        case BYTEA:
            coldbtype = DBType.BYTEA;
            break;
        default:
            coldbtype = DBType.TEXT;
        }
        enumType[i] = (byte) (coldbtype.ordinal());

        /* Get the actual value, and set the null bit */
        if (colValue[i] == null) {
            nullBits[i] = true;
            colLength[i] = 0;
        } else {
            nullBits[i] = false;

            /*
                 * For fixed length type, we get the fixed length.
             * For var len binary format, the length is in the col value.
             * For text format, we must convert encoding first.
             */
            if (!coldbtype.isVarLength()) {
                colLength[i] = coldbtype.getTypeLength();
            } else if (!isTextForm(colType[i])) {
                colLength[i] = ((byte[]) colValue[i]).length;
            } else {
                colLength[i] = ((String) colValue[i]).getBytes(CHARSET).length;
            }

            /* calculate and add the type alignment padding */
            padLength[i] = roundUpAlignment(datlen, coldbtype.getAlignment()) - datlen;
            datlen += padLength[i];

            /* for variable length type, we add a 4 byte length header */
            if (coldbtype.isVarLength()) {
                datlen += 4;
            }
        }
        datlen += colLength[i];
    }

    /*
     * Add the final alignment padding for the next record
     */
    int endpadding = roundUpAlignment(datlen, 8) - datlen;
    datlen += endpadding;

    /* Construct the packet header */
    out.writeInt(datlen);
    out.writeShort(VERSION);
    out.writeByte(errorFlag);
    out.writeShort(numCol);

    /* Write col type */
    for (int i = 0; i < numCol; i++) {
        out.writeByte(enumType[i]);
    }

    /* Nullness */
    byte[] nullBytes = boolArrayToByteArray(nullBits);
    out.write(nullBytes);

    /* Column Value */
    for (int i = 0; i < numCol; i++) {
        if (!nullBits[i]) {
            /* Pad the alignment byte first */
            if (padLength[i] > 0) {
                out.write(padbytes, 0, padLength[i]);
            }

            /* Now, write the actual column value */
            switch (DataType.get(colType[i])) {
            case BIGINT:
                out.writeLong(((Long) colValue[i]));
                break;
            case BOOLEAN:
                out.writeBoolean(((Boolean) colValue[i]));
                break;
            case FLOAT8:
                out.writeDouble(((Double) colValue[i]));
                break;
            case INTEGER:
                out.writeInt(((Integer) colValue[i]));
                break;
            case REAL:
                out.writeFloat(((Float) colValue[i]));
                break;
            case SMALLINT:
                out.writeShort(((Short) colValue[i]));
                break;

            /* For BYTEA format, add 4byte length header at the beginning  */
            case BYTEA:
                out.writeInt(colLength[i]);
                out.write((byte[]) colValue[i]);
                break;

            /* For text format, add 4byte length header. string is already '\0' terminated */
            default: {
                out.writeInt(colLength[i]);
                byte[] data = ((String) colValue[i]).getBytes(CHARSET);
                out.write(data);
                break;
            }
            }
        }
    }

    /* End padding */
    out.write(padbytes, 0, endpadding);
}