Example usage for java.io DataOutput writeByte

List of usage examples for java.io DataOutput writeByte

Introduction

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

Prototype

void writeByte(int v) throws IOException;

Source Link

Document

Writes to the output stream the eight low- order bits of the argument v.

Usage

From source file:Main.java

public static void writeBooleanArray(DataOutput out, boolean[] v) throws IOException {
    for (int chunk = 0; chunk < v.length; chunk += 8) {
        byte encoding = 0;
        for (int i = chunk; i < v.length && i < chunk + 8; i++) {
            encoding <<= 1;//w  w  w. j  a v  a2 s .co  m
            encoding += v[i] ? 1 : 0;
        }
        out.writeByte(encoding);
    }
}

From source file:com.aliyun.odps.io.TupleReaderWriter.java

/**
 * Tuple??/* w w w .  jav  a  2  s . c  o m*/
 *
 * @param out
 *     Tuple??
 * @param t
 *     Tuple
 * @throws IOException
 *     ?Tuple?field
 */
public static void writeTuple(DataOutput out, Tuple t) throws IOException {
    out.writeByte(TUPLE);
    int sz = t.size();
    out.writeInt(sz);
    for (int i = 0; i < sz; i++) {
        writeDatum(out, t.get(i));
    }
}

From source file:com.icloud.framework.core.util.FBUtilities.java

public static void writeShortByteArray(ByteBuffer name, DataOutput out) {
    int length = name.remaining();
    assert 0 <= length && length <= MAX_UNSIGNED_SHORT;
    try {//w w w .j a v  a  2s  .  co  m
        out.writeByte((length >> 8) & 0xFF);
        out.writeByte(length & 0xFF);
        out.write(name.array(), name.position() + name.arrayOffset(), name.remaining());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void writeBooleanArray(DataOutput out, boolean[] v, boolean extra) throws IOException {
    int len = v.length + 1;
    for (int chunk = 0; chunk < len; chunk += 8) {
        byte encoding = 0;
        for (int i = chunk; i < len && i < chunk + 8; i++) {
            encoding <<= 1;/*from w w  w  .j a v  a2 s . co  m*/
            if (i == v.length) {
                encoding += extra ? 1 : 0; //v[len] is the extra piece
            } else {
                encoding += v[i] ? 1 : 0;
            }
        }
        out.writeByte(encoding);
    }
}

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);/*from  w  ww.  j  av  a 2  s .  co  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.buaa.cfs.io.UTF8.java

private static void writeChars(DataOutput out, String s, int start, int length) throws IOException {
    final int end = start + length;
    for (int i = start; i < end; i++) {
        int code = s.charAt(i);
        if (code <= 0x7F) {
            out.writeByte((byte) code);
        } else if (code <= 0x07FF) {
            out.writeByte((byte) (0xC0 | ((code >> 6) & 0x1F)));
            out.writeByte((byte) (0x80 | code & 0x3F));
        } else {/*  ww  w.j a  v  a  2s .co m*/
            out.writeByte((byte) (0xE0 | ((code >> 12) & 0X0F)));
            out.writeByte((byte) (0x80 | ((code >> 6) & 0x3F)));
            out.writeByte((byte) (0x80 | (code & 0x3F)));
        }
    }
}

From source file:com.aliyun.odps.io.TupleReaderWriter.java

private static void writeDatum(DataOutput out, Writable val) throws IOException {
    // Read the data type
    byte type = findType(val);
    switch (type) {
    case TUPLE://from www.ja  va 2s. c o m
        Tuple t = (Tuple) val;
        out.writeByte(TUPLE);
        int sz = t.size();
        out.writeInt(sz);
        for (int i = 0; i < sz; i++) {
            writeDatum(out, t.get(i));
        }
        break;

    case NULL:
        out.writeByte(NULL);
        break;

    case INTWRITABLE:
        out.writeByte(INTWRITABLE);
        ((IntWritable) val).write(out);
        break;

    case LONGWRITABLE:
        out.writeByte(LONGWRITABLE);
        ((LongWritable) val).write(out);
        break;

    case DATETIMEWRITABLE:
        out.writeByte(DATETIMEWRITABLE);
        ((DatetimeWritable) val).write(out);
        break;

    case DOUBLEWRITABLE:
        out.writeByte(DOUBLEWRITABLE);
        ((DoubleWritable) val).write(out);
        break;

    case BOOLEANWRITABLE:
        out.writeByte(BOOLEANWRITABLE);
        ((BooleanWritable) val).write(out);
        break;

    case BYTESWRITABLE:
        out.writeByte(BYTESWRITABLE);
        ((BytesWritable) val).write(out);
        break;

    case TEXT:
        out.writeByte(TEXT);
        ((Text) val).write(out);
        break;

    case NULLWRITABLE:
        out.writeByte(NULLWRITABLE);
        ((NullWritable) val).write(out);
        break;

    case UNKNOWN:
        out.writeByte(UNKNOWN);
        out.writeUTF(val.getClass().getName());
        val.write(out);
        break;

    default:
        throw new RuntimeException("Unexpected data type " + type + " found in stream.");
    }
}

From source file:com.ibm.jaql.util.BaseUtil.java

/**
 * //from w  w w  . ja  va 2  s.c om
 * 0x00, 8 byte neg 0x01, 7 byte neg ... 0x07 1 byte neg 0x08 -120 0x09 -119
 * ... 0x80 0 ... 0xf6 118 0xf7 119 0xf8 1 byte pos ... 0xfe 7 byte pos 0xff 8
 * byte pos
 */
public static void writeVSLong(DataOutput out, long v) throws IOException {
    int flip;
    if (v >= -120) {
        if (v <= 119) {
            out.writeByte((byte) (v + 0x80));
            return;
        }
        // v >= 120
        flip = 0x00;
    } else
    // v <= -121
    {
        v = ~v; // take one's complement of negative numbers 
        flip = 0xff;
    }

    // 120 <= v <= maxLong
    v -= 120;
    int len = 1;
    long t = v >> 8;
    while (t > 0) {
        t >>= 8;
        len++;
    }

    out.writeByte((byte) ((0xf7 + len) ^ flip));

    do {
        len--;
        out.writeByte((byte) ((v >> (len << 3)) ^ flip));
    } while (len > 0);
}

From source file:CompressedNumber.java

/**
 * Write a compressed integer only supporting signed values. Formats are (with
 * x representing value bits):/*ww w. j  a v  a2s. co m*/
 * 
 * <PRE>
 * 
 * 1 Byte - 00xxxxxx Represents the value <= 63 (0x3f) 2 Byte - 01xxxxxx
 * xxxxxxxx Represents the value > 63 && <= 16383 (0x3fff) 4 byte - 1xxxxxxx
 * xxxxxxxx xxxxxxxx xxxxxxxx Represents the value > 16383 && <= MAX_INT
 * 
 * </PRE>
 * 
 * 
 * @exception IOException
 *              value is negative or an exception was thrown by a method on
 *              out.
 */
public static final int writeInt(DataOutput out, int value) throws IOException {

    if (value < 0)
        throw new IOException();

    if (value <= 0x3f) {

        out.writeByte(value);
        return 1;
    }

    if (value <= 0x3fff) {

        out.writeByte(0x40 | (value >>> 8));
        out.writeByte(value & 0xff);
        return 2;
    }

    out.writeByte(((value >>> 24) | 0x80) & 0xff);
    out.writeByte((value >>> 16) & 0xff);
    out.writeByte((value >>> 8) & 0xff);
    out.writeByte((value) & 0xff);
    return 4;
}

From source file:CompressedNumber.java

/**
 * Write a compressed long only supporting signed values.
 * // ww  w  . j a  v a2s.  c o m
 * Formats are (with x representing value bits):
 * 
 * <PRE>
 * 
 * 2 byte - 00xxxxxx xxxxxxxx Represents the value <= 16383 (0x3fff) 4 byte -
 * 01xxxxxx xxxxxxxx xxxxxxxx xxxxxxxx Represents the value > 16383 && <=
 * 0x3fffffff 8 byte - 1xxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
 * xxxxxxxx xxxxxxxx Represents the value > 0x3fffffff && <= MAX_LONG
 * 
 * </PRE>
 * 
 * 
 * @exception IOException
 *              value is negative or an exception was thrown by a method on
 *              out.
 */
public static final int writeLong(DataOutput out, long value) throws IOException {

    if (value < 0)
        throw new IOException();

    if (value <= 0x3fff) {

        out.writeByte((int) ((value >>> 8) & 0xff));
        out.writeByte((int) ((value) & 0xff));
        return 2;
    }

    if (value <= 0x3fffffff) {

        out.writeByte((int) (((value >>> 24) | 0x40) & 0xff));
        out.writeByte((int) ((value >>> 16) & 0xff));
        out.writeByte((int) ((value >>> 8) & 0xff));
        out.writeByte((int) ((value) & 0xff));
        return 4;
    }

    out.writeByte((int) (((value >>> 56) | 0x80) & 0xff));
    out.writeByte((int) ((value >>> 48) & 0xff));
    out.writeByte((int) ((value >>> 40) & 0xff));
    out.writeByte((int) ((value >>> 32) & 0xff));
    out.writeByte((int) ((value >>> 24) & 0xff));
    out.writeByte((int) ((value >>> 16) & 0xff));
    out.writeByte((int) ((value >>> 8) & 0xff));
    out.writeByte((int) ((value) & 0xff));
    return 8;
}