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:com.ibm.bi.dml.runtime.matrix.data.MatrixBlock.java

/**
 * /*from   w w  w .  j  a v  a 2s  .co  m*/
 * @param out
 * @throws IOException
 */
private void writeDenseToSparse(DataOutput out) throws IOException {
    out.writeByte(BlockType.SPARSE_BLOCK.ordinal()); //block type
    writeNnzInfo(out, false);

    int start = 0;
    for (int r = 0; r < rlen; r++) {
        //count nonzeros
        int nr = 0;
        for (int i = start; i < start + clen; i++)
            if (denseBlock[i] != 0.0)
                nr++;
        out.writeInt(nr);
        for (int c = 0; c < clen; c++) {
            if (denseBlock[start] != 0.0) {
                out.writeInt(c);
                out.writeDouble(denseBlock[start]);
            }
            start++;
        }
    }
}

From source file:com.ibm.bi.dml.runtime.matrix.data.MatrixBlock.java

/**
 * //from  w  ww.  j a  v a 2s.  c  o m
 * @param out
 * @throws IOException
 */
private void writeSparseBlock(DataOutput out) throws IOException {
    out.writeByte(BlockType.SPARSE_BLOCK.ordinal());
    writeNnzInfo(out, false);

    if (out instanceof MatrixBlockDataOutput) //fast serialize
        ((MatrixBlockDataOutput) out).writeSparseRows(rlen, sparseRows);
    else //general case (if fast serialize not supported)
    {
        int r = 0;
        for (; r < Math.min(rlen, sparseRows.length); r++) {
            if (sparseRows[r] == null)
                out.writeInt(0);
            else {
                int nr = sparseRows[r].size();
                out.writeInt(nr);
                int[] cols = sparseRows[r].getIndexContainer();
                double[] values = sparseRows[r].getValueContainer();
                for (int j = 0; j < nr; j++) {
                    out.writeInt(cols[j]);
                    out.writeDouble(values[j]);
                }

            }
        }
        for (; r < rlen; r++)
            out.writeInt(0);
    }
}

From source file:com.ibm.bi.dml.runtime.matrix.data.MatrixBlock.java

/**
 * //from w w w  .j  av a  2s  .co m
 * @param out
 * @throws IOException
 */
private void writeDenseToUltraSparse(DataOutput out) throws IOException {
    out.writeByte(BlockType.ULTRA_SPARSE_BLOCK.ordinal());
    writeNnzInfo(out, true);

    long wnnz = 0;

    if (clen > 1) //ULTRA-SPARSE BLOCK
    {
        //block: write ijv-triples
        for (int r = 0, ix = 0; r < rlen; r++)
            for (int c = 0; c < clen; c++, ix++)
                if (denseBlock[ix] != 0) {
                    out.writeInt(r);
                    out.writeInt(c);
                    out.writeDouble(denseBlock[ix]);
                    wnnz++;
                }
    } else //ULTRA-SPARSE COL
    {
        //col: write iv-pairs
        for (int r = 0; r < rlen; r++)
            if (denseBlock[r] != 0) {
                out.writeInt(r);
                out.writeDouble(denseBlock[r]);
                wnnz++;
            }
    }

    //validity check (nnz must exactly match written nnz)
    if (nonZeros != wnnz) {
        throw new IOException(
                "Invalid number of serialized non-zeros: " + wnnz + " (expected: " + nonZeros + ")");
    }
}

From source file:com.ibm.bi.dml.runtime.matrix.data.MatrixBlock.java

/**
 * /*from  ww w.  jav  a  2  s . c  om*/
 * @param out
 * @throws IOException
 */
private void writeSparseToUltraSparse(DataOutput out) throws IOException {
    out.writeByte(BlockType.ULTRA_SPARSE_BLOCK.ordinal());
    writeNnzInfo(out, true);

    long wnnz = 0;
    if (clen > 1) //ULTRA-SPARSE BLOCK
    {
        //block: write ijv-triples
        for (int r = 0; r < Math.min(rlen, sparseRows.length); r++)
            if (sparseRows[r] != null && !sparseRows[r].isEmpty()) {
                int alen = sparseRows[r].size();
                int[] aix = sparseRows[r].getIndexContainer();
                double[] avals = sparseRows[r].getValueContainer();
                for (int j = 0; j < alen; j++) {
                    //ultra-sparse block: write ijv-triples
                    out.writeInt(r);
                    out.writeInt(aix[j]);
                    out.writeDouble(avals[j]);
                    wnnz++;
                }
            }
    } else //ULTRA-SPARSE COL
    {
        //block: write iv-pairs (should never happen since always dense)
        for (int r = 0; r < Math.min(rlen, sparseRows.length); r++)
            if (sparseRows[r] != null && !sparseRows[r].isEmpty()) {
                out.writeInt(r);
                out.writeDouble(sparseRows[r].getValueContainer()[0]);
                wnnz++;
            }
    }

    //validity check (nnz must exactly match written nnz)
    if (nonZeros != wnnz) {
        throw new IOException(
                "Invalid number of serialized non-zeros: " + wnnz + " (expected: " + nonZeros + ")");
    }
}

From source file:com.ibm.bi.dml.runtime.matrix.data.MatrixBlock.java

/**
 * /*www.  j ava2 s  . c  o  m*/
 * @param out
 * @throws IOException
 */
private void writeSparseToDense(DataOutput out) throws IOException {
    //write block type 'dense'
    out.writeByte(BlockType.DENSE_BLOCK.ordinal());

    //write data (from sparse to dense)
    if (sparseRows == null) //empty block
        for (int i = 0; i < rlen * clen; i++)
            out.writeDouble(0);
    else //existing sparse block
    {
        for (int i = 0; i < rlen; i++) {
            if (i < sparseRows.length && sparseRows[i] != null && !sparseRows[i].isEmpty()) {
                SparseRow arow = sparseRows[i];
                int alen = arow.size();
                int[] aix = arow.getIndexContainer();
                double[] avals = arow.getValueContainer();
                //foreach non-zero value, fill with 0s if required
                for (int j = 0, j2 = 0; j2 < alen; j++, j2++) {
                    for (; j < aix[j2]; j++)
                        out.writeDouble(0);
                    out.writeDouble(avals[j2]);
                }
                //remaining 0 values in row
                for (int j = aix[alen - 1] + 1; j < clen; j++)
                    out.writeDouble(0);
            } else //empty row
                for (int j = 0; j < clen; j++)
                    out.writeDouble(0);
        }
    }
}

From source file:com.ibm.bi.dml.runtime.matrix.data.MatrixBlock.java

/**
 * /*  w ww .  j ava 2s  .c o  m*/
 * @param out
 * @throws IOException
 */
private void writeEmptyBlock(DataOutput out) throws IOException {
    //empty blocks do not need to materialize row information
    out.writeByte(BlockType.EMPTY_BLOCK.ordinal());
}

From source file:org.apache.cassandra.utils.ByteBufferUtil.java

public static void writeWithShortLength(ByteBuffer buffer, DataOutput out) {
    int length = buffer.remaining();
    assert 0 <= length && length <= FBUtilities.MAX_UNSIGNED_SHORT : length;
    try {//  w  w  w  . j a v a 2  s .c  o m
        out.writeByte((length >> 8) & 0xFF);
        out.writeByte(length & 0xFF);
        write(buffer, out); // writing data bytes to output source
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.fop.afp.goca.GraphicsSetProcessColor.java

/** {@inheritDoc} */
public void writeToStream(OutputStream os) throws IOException {
    float[] colorComponents = color.getColorComponents(null);

    // COLSPCE/*from www.  j  a  v a  2  s.c o  m*/
    byte colspace;
    ColorSpace cs = color.getColorSpace();
    int colSpaceType = cs.getType();
    ByteArrayOutputStream baout = new ByteArrayOutputStream();
    byte[] colsizes;
    if (colSpaceType == ColorSpace.TYPE_CMYK) {
        colspace = CMYK;
        colsizes = new byte[] { 0x08, 0x08, 0x08, 0x08 };
        for (int i = 0; i < colorComponents.length; i++) {
            baout.write(Math.round(colorComponents[i] * 255));
        }
    } else if (colSpaceType == ColorSpace.TYPE_RGB) {
        colspace = RGB;
        colsizes = new byte[] { 0x08, 0x08, 0x08, 0x00 };
        for (int i = 0; i < colorComponents.length; i++) {
            baout.write(Math.round(colorComponents[i] * 255));
        }
    } else if (cs instanceof CIELabColorSpace) {
        colspace = CIELAB;
        colsizes = new byte[] { 0x08, 0x08, 0x08, 0x00 };
        DataOutput dout = new java.io.DataOutputStream(baout);
        //According to GOCA, I'd expect the multiplicator below to be 255f, not 100f
        //But only IBM AFP Workbench seems to support Lab colors and it requires "c * 100f"
        int l = Math.round(colorComponents[0] * 100f);
        int a = Math.round(colorComponents[1] * 255f) - 128;
        int b = Math.round(colorComponents[2] * 255f) - 128;
        dout.writeByte(l);
        dout.writeByte(a);
        dout.writeByte(b);
    } else {
        throw new IllegalStateException();
    }

    int len = getDataLength();
    byte[] data = new byte[12];
    data[0] = getOrderCode(); // GSPCOL order code
    data[1] = (byte) (len - 2); // LEN
    data[2] = 0x00; // reserved; must be zero
    data[3] = colspace; // COLSPCE
    data[4] = 0x00; // reserved; must be zero
    data[5] = 0x00; // reserved; must be zero
    data[6] = 0x00; // reserved; must be zero
    data[7] = 0x00; // reserved; must be zero
    data[8] = colsizes[0]; // COLSIZE(S)
    data[9] = colsizes[1];
    data[10] = colsizes[2];
    data[11] = colsizes[3];

    os.write(data);
    baout.writeTo(os);
}

From source file:org.apache.geode.internal.InternalDataSerializer.java

private static void initializeWellKnownSerializers() {
    // ArrayBlockingQueue does not have zero-arg constructor
    // LinkedBlockingQueue does have zero-arg constructor but no way to get capacity

    classesToSerializers.put("java.lang.String", new WellKnownPdxDS() {
        @Override/* w ww .j a  va  2  s . c om*/
        public boolean toData(Object o, DataOutput out) throws IOException {
            try {
                writeString((String) o, out);
            } catch (UTFDataFormatException ex) {
                // See bug 30428
                String s = "While writing a String of length " + ((String) o).length();
                UTFDataFormatException ex2 = new UTFDataFormatException(s);
                ex2.initCause(ex);
                throw ex2;
            }
            return true;
        }
    });
    classesToSerializers.put("java.net.InetAddress", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            InetAddress address = (InetAddress) o;
            out.writeByte(INET_ADDRESS);
            writeInetAddress(address, out);
            return true;
        }
    });
    classesToSerializers.put("java.net.Inet4Address", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            InetAddress address = (InetAddress) o;
            out.writeByte(INET_ADDRESS);
            writeInetAddress(address, out);
            return true;
        }
    });
    classesToSerializers.put("java.net.Inet6Address", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            InetAddress address = (InetAddress) o;
            out.writeByte(INET_ADDRESS);
            writeInetAddress(address, out);
            return true;
        }
    });
    classesToSerializers.put("java.lang.Class", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Class c = (Class) o;
            if (c.isPrimitive()) {
                writePrimitiveClass(c, out);
            } else {
                out.writeByte(CLASS);
                writeClass(c, out);
            }
            return true;
        }
    });
    classesToSerializers.put("java.lang.Boolean", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Boolean value = (Boolean) o;
            out.writeByte(BOOLEAN);
            writeBoolean(value, out);
            return true;
        }
    });
    classesToSerializers.put("java.lang.Character", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Character value = (Character) o;
            out.writeByte(CHARACTER);
            writeCharacter(value, out);
            return true;
        }
    });
    classesToSerializers.put("java.lang.Byte", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Byte value = (Byte) o;
            out.writeByte(BYTE);
            writeByte(value, out);
            return true;
        }
    });
    classesToSerializers.put("java.lang.Short", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Short value = (Short) o;
            out.writeByte(SHORT);
            writeShort(value, out);
            return true;
        }
    });
    classesToSerializers.put("java.lang.Integer", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Integer value = (Integer) o;
            out.writeByte(INTEGER);
            writeInteger(value, out);
            return true;
        }
    });
    classesToSerializers.put("java.lang.Long", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Long value = (Long) o;
            out.writeByte(LONG);
            writeLong(value, out);
            return true;
        }
    });
    classesToSerializers.put("java.lang.Float", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Float value = (Float) o;
            out.writeByte(FLOAT);
            writeFloat(value, out);
            return true;
        }
    });
    classesToSerializers.put("java.lang.Double", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Double value = (Double) o;
            out.writeByte(DOUBLE);
            writeDouble(value, out);
            return true;
        }
    });
    classesToSerializers.put("[Z", // boolean[]
            new WellKnownPdxDS() {
                @Override
                public boolean toData(Object o, DataOutput out) throws IOException {
                    out.writeByte(BOOLEAN_ARRAY);
                    writeBooleanArray((boolean[]) o, out);
                    return true;
                }
            });
    classesToSerializers.put("[B", // byte[]
            new WellKnownPdxDS() {
                @Override
                public boolean toData(Object o, DataOutput out) throws IOException {
                    byte[] array = (byte[]) o;
                    out.writeByte(BYTE_ARRAY);
                    writeByteArray(array, out);
                    return true;
                }
            });
    classesToSerializers.put("[C", // char[]
            new WellKnownPdxDS() {
                @Override
                public boolean toData(Object o, DataOutput out) throws IOException {
                    out.writeByte(CHAR_ARRAY);
                    writeCharArray((char[]) o, out);
                    return true;
                }
            });
    classesToSerializers.put("[D", // double[]
            new WellKnownPdxDS() {
                @Override
                public boolean toData(Object o, DataOutput out) throws IOException {
                    double[] array = (double[]) o;
                    out.writeByte(DOUBLE_ARRAY);
                    writeDoubleArray(array, out);
                    return true;
                }
            });
    classesToSerializers.put("[F", // float[]
            new WellKnownPdxDS() {
                @Override
                public boolean toData(Object o, DataOutput out) throws IOException {
                    float[] array = (float[]) o;
                    out.writeByte(FLOAT_ARRAY);
                    writeFloatArray(array, out);
                    return true;
                }
            });
    classesToSerializers.put("[I", // int[]
            new WellKnownPdxDS() {
                @Override
                public boolean toData(Object o, DataOutput out) throws IOException {
                    int[] array = (int[]) o;
                    out.writeByte(INT_ARRAY);
                    writeIntArray(array, out);
                    return true;
                }
            });
    classesToSerializers.put("[J", // long[]
            new WellKnownPdxDS() {
                @Override
                public boolean toData(Object o, DataOutput out) throws IOException {
                    long[] array = (long[]) o;
                    out.writeByte(LONG_ARRAY);
                    writeLongArray(array, out);
                    return true;
                }
            });
    classesToSerializers.put("[S", // short[]
            new WellKnownPdxDS() {
                @Override
                public boolean toData(Object o, DataOutput out) throws IOException {
                    short[] array = (short[]) o;
                    out.writeByte(SHORT_ARRAY);
                    writeShortArray(array, out);
                    return true;
                }
            });
    classesToSerializers.put("[Ljava.lang.String;", // String[]
            new WellKnownPdxDS() {
                @Override
                public boolean toData(Object o, DataOutput out) throws IOException {
                    String[] array = (String[]) o;
                    out.writeByte(STRING_ARRAY);
                    writeStringArray(array, out);
                    return true;
                }
            });
    classesToSerializers.put(TimeUnit.NANOSECONDS.getClass().getName(), new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(TIME_UNIT);
            out.writeByte(TIME_UNIT_NANOSECONDS);
            return true;
        }
    });
    classesToSerializers.put(TimeUnit.MICROSECONDS.getClass().getName(), new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(TIME_UNIT);
            out.writeByte(TIME_UNIT_MICROSECONDS);
            return true;
        }
    });
    classesToSerializers.put(TimeUnit.MILLISECONDS.getClass().getName(), new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(TIME_UNIT);
            out.writeByte(TIME_UNIT_MILLISECONDS);
            return true;
        }
    });
    classesToSerializers.put(TimeUnit.SECONDS.getClass().getName(), new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(TIME_UNIT);
            out.writeByte(TIME_UNIT_SECONDS);
            return true;
        }
    });
    classesToSerializers.put("java.util.Date", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Date date = (Date) o;
            out.writeByte(DATE);
            writeDate(date, out);
            return true;
        }
    });
    classesToSerializers.put("java.io.File", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            File file = (File) o;
            out.writeByte(FILE);
            writeFile(file, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.ArrayList", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            ArrayList list = (ArrayList) o;
            out.writeByte(ARRAY_LIST);
            writeArrayList(list, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.LinkedList", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            LinkedList list = (LinkedList) o;
            out.writeByte(LINKED_LIST);
            writeLinkedList(list, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.Vector", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(VECTOR);
            writeVector((Vector) o, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.Stack", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(STACK);
            writeStack((Stack) o, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.HashSet", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            HashSet list = (HashSet) o;
            out.writeByte(HASH_SET);
            writeHashSet(list, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.LinkedHashSet", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(LINKED_HASH_SET);
            writeLinkedHashSet((LinkedHashSet) o, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.HashMap", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            HashMap list = (HashMap) o;
            out.writeByte(HASH_MAP);
            writeHashMap(list, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.IdentityHashMap", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(IDENTITY_HASH_MAP);
            writeIdentityHashMap((IdentityHashMap) o, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.Hashtable", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(HASH_TABLE);
            writeHashtable((Hashtable) o, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.Properties", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Properties props = (Properties) o;
            out.writeByte(PROPERTIES);
            writeProperties(props, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.TreeMap", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(TREE_MAP);
            writeTreeMap((TreeMap) o, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.TreeSet", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(TREE_SET);
            writeTreeSet((TreeSet) o, out);
            return true;
        }
    });
    if (is662SerializationEnabled()) {
        classesToSerializers.put("java.math.BigInteger", new WellKnownDS() {
            @Override
            public boolean toData(Object o, DataOutput out) throws IOException {
                out.writeByte(BIG_INTEGER);
                writeBigInteger((BigInteger) o, out);
                return true;
            }
        });
        classesToSerializers.put("java.math.BigDecimal", new WellKnownDS() {
            @Override
            public boolean toData(Object o, DataOutput out) throws IOException {
                out.writeByte(BIG_DECIMAL);
                writeBigDecimal((BigDecimal) o, out);
                return true;
            }
        });
        classesToSerializers.put("java.util.UUID", new WellKnownDS() {
            @Override
            public boolean toData(Object o, DataOutput out) throws IOException {
                out.writeByte(UUID);
                writeUUID((UUID) o, out);
                return true;
            }
        });
        classesToSerializers.put("java.sql.Timestamp", new WellKnownDS() {
            @Override
            public boolean toData(Object o, DataOutput out) throws IOException {
                out.writeByte(TIMESTAMP);
                writeTimestamp((Timestamp) o, out);
                return true;
            }
        });
    }
}

From source file:org.apache.geode.internal.InternalDataSerializer.java

public static void writeDSFIDHeader(int dsfid, DataOutput out) throws IOException {
    if (dsfid == DataSerializableFixedID.ILLEGAL) {
        throw new IllegalStateException(
                LocalizedStrings.InternalDataSerializer_ATTEMPTED_TO_SERIALIZE_ILLEGAL_DSFID
                        .toLocalizedString());
    }/*from  w  w  w.ja v  a 2s.c  o  m*/
    if (dsfid <= Byte.MAX_VALUE && dsfid >= Byte.MIN_VALUE) {
        out.writeByte(DS_FIXED_ID_BYTE);
        out.writeByte(dsfid);
    } else if (dsfid <= Short.MAX_VALUE && dsfid >= Short.MIN_VALUE) {
        out.writeByte(DS_FIXED_ID_SHORT);
        out.writeShort(dsfid);
    } else {
        out.writeByte(DS_FIXED_ID_INT);
        out.writeInt(dsfid);
    }
}