List of usage examples for java.io DataOutput writeByte
void writeByte(int v) throws IOException;
v
. From source file:org.apache.sysml.runtime.matrix.data.MatrixBlock.java
private void writeSparseBlock(DataOutput out) throws IOException { out.writeByte(BlockType.SPARSE_BLOCK.ordinal()); writeNnzInfo(out, false);/* w w w . ja v a 2s. com*/ if (out instanceof MatrixBlockDataOutput) //fast serialize ((MatrixBlockDataOutput) out).writeSparseRows(rlen, sparseBlock); else //general case (if fast serialize not supported) { int r = 0; for (; r < Math.min(rlen, sparseBlock.numRows()); r++) { if (sparseBlock.isEmpty(r)) out.writeInt(0); else { int pos = sparseBlock.pos(r); int nr = sparseBlock.size(r); int[] cols = sparseBlock.indexes(r); double[] values = sparseBlock.values(r); out.writeInt(nr); for (int j = pos; j < pos + nr; j++) { out.writeInt(cols[j]); out.writeDouble(values[j]); } } } for (; r < rlen; r++) out.writeInt(0); } }
From source file:org.apache.sysml.runtime.matrix.data.MatrixBlock.java
private void writeSparseToUltraSparse(DataOutput out) throws IOException { out.writeByte(BlockType.ULTRA_SPARSE_BLOCK.ordinal()); writeNnzInfo(out, true);//w w w.ja v a 2 s.c o m long wnnz = 0; if (clen > 1) //ULTRA-SPARSE BLOCK { //block: write ijv-triples for (int r = 0; r < Math.min(rlen, sparseBlock.numRows()); r++) if (!sparseBlock.isEmpty(r)) { int apos = sparseBlock.pos(r); int alen = sparseBlock.size(r); int[] aix = sparseBlock.indexes(r); double[] avals = sparseBlock.values(r); for (int j = apos; j < apos + 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, sparseBlock.numRows()); r++) if (!sparseBlock.isEmpty(r)) { int pos = sparseBlock.pos(r); out.writeInt(r); out.writeDouble(sparseBlock.values(r)[pos]); 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:org.apache.sysml.runtime.matrix.data.MatrixBlock.java
private void writeSparseToDense(DataOutput out) throws IOException { //write block type 'dense' out.writeByte(BlockType.DENSE_BLOCK.ordinal()); //write data (from sparse to dense) if (sparseBlock == null) //empty block for (int i = 0; i < rlen * clen; i++) out.writeDouble(0);/*from www .j a v a 2 s. co m*/ else //existing sparse block { SparseBlock a = sparseBlock; for (int i = 0; i < rlen; i++) { if (i < a.numRows() && !a.isEmpty(i)) { int apos = a.pos(i); int alen = a.size(i); int[] aix = a.indexes(i); double[] avals = a.values(i); //foreach non-zero value, fill with 0s if required for (int j = 0, j2 = 0; j2 < alen; j++, j2++) { for (; j < aix[apos + j2]; j++) out.writeDouble(0); out.writeDouble(avals[apos + j2]); } //remaining 0 values in row for (int j = aix[apos + 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:org.apache.sysml.runtime.matrix.data.MatrixBlock.java
private void writeDenseToUltraSparse(DataOutput out) throws IOException { out.writeByte(BlockType.ULTRA_SPARSE_BLOCK.ordinal()); writeNnzInfo(out, true);/* www . j a va 2 s.c o m*/ 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:org.apache.sysml.runtime.matrix.data.MatrixBlock.java
private void writeDenseToSparse(DataOutput out) throws IOException { out.writeByte(BlockType.SPARSE_BLOCK.ordinal()); //block type writeNnzInfo(out, false);// www .ja v a 2s. c o m 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:org.cloudata.core.common.io.CObjectWritable.java
/** Write a {@link CWritable}, {@link String}, primitive type, or an array of * the preceding. *//*from w w w. ja v a 2 s . co m*/ public static void writeObject(DataOutput out, Object instance, Class declaredClass, CloudataConf conf, boolean arrayComponent) throws IOException { if (instance == null) { // null instance = new NullInstance(declaredClass, conf); declaredClass = CWritable.class; arrayComponent = false; } if (!arrayComponent) { CUTF8.writeString(out, declaredClass.getName()); // always write declared //System.out.println("Write:declaredClass.getName():" + declaredClass.getName()); } if (declaredClass.isArray()) { // array int length = Array.getLength(instance); out.writeInt(length); //System.out.println("Write:length:" + length); if (declaredClass.getComponentType() == Byte.TYPE) { out.write((byte[]) instance); } else if (declaredClass.getComponentType() == ColumnValue.class) { //ColumnValue? Deserialize? ?? ? ?? ? . writeColumnValue(out, instance, declaredClass, conf, length); } else { for (int i = 0; i < length; i++) { writeObject(out, Array.get(instance, i), declaredClass.getComponentType(), conf, !declaredClass.getComponentType().isArray()); } } } else if (declaredClass == String.class) { // String CUTF8.writeString(out, (String) instance); } else if (declaredClass.isPrimitive()) { // primitive type if (declaredClass == Boolean.TYPE) { // boolean out.writeBoolean(((Boolean) instance).booleanValue()); } else if (declaredClass == Character.TYPE) { // char out.writeChar(((Character) instance).charValue()); } else if (declaredClass == Byte.TYPE) { // byte out.writeByte(((Byte) instance).byteValue()); } else if (declaredClass == Short.TYPE) { // short out.writeShort(((Short) instance).shortValue()); } else if (declaredClass == Integer.TYPE) { // int out.writeInt(((Integer) instance).intValue()); } else if (declaredClass == Long.TYPE) { // long out.writeLong(((Long) instance).longValue()); } else if (declaredClass == Float.TYPE) { // float out.writeFloat(((Float) instance).floatValue()); } else if (declaredClass == Double.TYPE) { // double out.writeDouble(((Double) instance).doubleValue()); } else if (declaredClass == Void.TYPE) { // void } else { throw new IllegalArgumentException("Not a primitive: " + declaredClass); } } else if (declaredClass.isEnum()) { // enum CUTF8.writeString(out, ((Enum) instance).name()); } else if (CWritable.class.isAssignableFrom(declaredClass)) { // Writable if (instance.getClass() == declaredClass) { out.writeShort(TYPE_SAME); // ? ?? ? ?? //System.out.println("Write:TYPE_SAME:" + TYPE_SAME); } else { out.writeShort(TYPE_DIFF); //System.out.println("Write:TYPE_DIFF:" + TYPE_DIFF); CUTF8.writeString(out, instance.getClass().getName()); //System.out.println("Write:instance.getClass().getName():" + instance.getClass().getName()); } ((CWritable) instance).write(out); //System.out.println("Write:instance value"); } else { throw new IOException("Can't write: " + instance + " as " + declaredClass); } }
From source file:org.cloudata.core.common.io.CWritableUtils.java
/** * Serializes a long to a binary stream with zero-compressed encoding. * For -112 <= i <= 127, only one byte is used with the actual value. * For other values of i, the first byte value indicates whether the * long is positive or negative, and the number of bytes that follow. * If the first byte value v is between -113 and -120, the following long * is positive, with number of bytes that follow are -(v+112). * If the first byte value v is between -121 and -128, the following long * is negative, with number of bytes that follow are -(v+120). Bytes are * stored in the high-non-zero-byte-first order. * //from w w w . ja v a 2s . c om * @param stream Binary output stream * @param i Long to be serialized * @throws java.io.IOException */ public static void writeVLong(DataOutput stream, long i) throws IOException { if (i >= -112 && i <= 127) { stream.writeByte((byte) i); return; } int len = -112; if (i < 0) { i ^= -1L; // take one's complement' len = -120; } long tmp = i; while (tmp != 0) { tmp = tmp >> 8; len--; } stream.writeByte((byte) len); len = (len < -120) ? -(len + 120) : -(len + 112); for (int idx = len; idx != 0; idx--) { int shiftbits = (idx - 1) * 8; long mask = 0xFFL << shiftbits; stream.writeByte((byte) ((i & mask) >> shiftbits)); } }
From source file:org.kalypso.shape.dbf.DBFField.java
@Override public byte[] writeValue(final DataOutput output, final Object value, final Charset charset) throws DBaseException, IOException { final byte[] bytes = m_formatter.toBytes(value, charset); final int bytesToWrite = Math.min(bytes.length, m_fieldLength); output.write(bytes, 0, bytesToWrite); for (int j = bytesToWrite; j < m_fieldLength; j++) output.writeByte((byte) 0x20); return bytes; }
From source file:org.mitre.la.mapred.io.DenseVectorWritable.java
/** * Serialize the fields of this object to <code>out</code>. * * @param out <code>DataOuput</code> to serialize this object into. * @throws IOException// www . j av a2s . co m */ @Override public void write(DataOutput out) throws IOException { int length = this.dv.getCardinality(); out.writeByte(0); // Space for versioning out.writeUTF(this.dv.getLabel()); out.writeInt(length); for (int i = 0; i < length; i++) { out.writeDouble(this.dv.get(i)); } }
From source file:org.mule.transformer.simple.ModbusRequestToByteArray.java
public Object doTransform(Object src, String outputEncoding) throws TransformerException { ModbusRequest request = (ModbusRequest) src; ByteArrayOutputStream os = new ByteArrayOutputStream(); DataOutput dout = new DataOutputStream(os); try {/* w w w . ja va 2s. c o m*/ if (!request.isHeadless()) { dout.writeShort(request.getTransactionID()); dout.writeShort(request.getProtocolID()); dout.writeShort(request.getDataLength()); } dout.writeByte(request.getUnitID()); dout.writeByte(request.getFunctionCode()); request.writeData(dout); return os.toByteArray(); } catch (Exception e) { e.printStackTrace(System.out); return null; } }