List of usage examples for java.io DataOutput writeDouble
void writeDouble(double v) throws IOException;
double
value, which is comprised of eight bytes, to the output stream. 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.j a v a 2 s . co 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./*w w w . j a va2 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.hama.ml.ann.NeuralNetwork.java
@Override public void write(DataOutput output) throws IOException { // write model type WritableUtils.writeString(output, modelType); // write learning rate output.writeDouble(learningRate); // write model path if (this.modelPath != null) { WritableUtils.writeString(output, modelPath); } else {/*from w w w.j a va2 s . co m*/ WritableUtils.writeString(output, "null"); } // serialize the class Class<? extends FeatureTransformer> featureTransformerCls = this.featureTransformer.getClass(); byte[] featureTransformerBytes = SerializationUtils.serialize(featureTransformerCls); output.writeInt(featureTransformerBytes.length); output.write(featureTransformerBytes); }
From source file:org.apache.hama.ml.perception.SmallMultiLayerPerceptron.java
@Override public void write(DataOutput output) throws IOException { WritableUtils.writeString(output, MLPType); output.writeDouble(learningRate); output.writeDouble(regularization);//from w w w . j a v a2s . co m output.writeDouble(momentum); output.writeInt(numberOfLayers); WritableUtils.writeString(output, squashingFunctionName); WritableUtils.writeString(output, costFunctionName); // write the number of neurons for each layer for (int i = 0; i < this.numberOfLayers; ++i) { output.writeInt(this.layerSizeArray[i]); } for (int i = 0; i < numberOfLayers - 1; ++i) { MatrixWritable matrixWritable = new MatrixWritable(this.weightMatrice[i]); matrixWritable.write(output); } // serialize the feature transformer Class<? extends FeatureTransformer> featureTransformerCls = this.featureTransformer.getClass(); byte[] featureTransformerBytes = SerializationUtils.serialize(featureTransformerCls); output.writeInt(featureTransformerBytes.length); output.write(featureTransformerBytes); }
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 . ja v a2 s .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); }
From source file:org.apache.mahout.classifier.df.DFUtils.java
/** * Writes a double[] into a DataOutput/* w w w . j a v a2 s . co m*/ * @throws java.io.IOException */ public static void writeArray(DataOutput out, double[] array) throws IOException { out.writeInt(array.length); for (double value : array) { out.writeDouble(value); } }
From source file:org.apache.mahout.classifier.sgd.TPrior.java
@Override public void write(DataOutput out) throws IOException { out.writeDouble(df); }
From source file:org.apache.marmotta.kiwi.io.KiWiIO.java
/** * Efficiently serialize a KiWiDoubleLiteral to a DataOutput destination. * * @param out the destination/*from ww w . j a v a 2 s .co m*/ * @param literal the KiWiDoubleLiteral to serialize * @throws IOException */ public static void writeDoubleLiteral(DataOutput out, KiWiDoubleLiteral literal) throws IOException { if (literal == null) { out.writeLong(-1L); } else { out.writeLong(literal.getId()); out.writeDouble(literal.getDoubleContent()); writeURI(out, literal.getType()); out.writeLong(literal.getCreated().getTime()); } }
From source file:org.apache.pig.data.BinInterSedes.java
@Override @SuppressWarnings("unchecked") public void writeDatum(DataOutput out, Object val, byte type) throws IOException { switch (type) { case DataType.TUPLE: writeTuple(out, (Tuple) val); break;/*from ww w . j a v a 2 s . com*/ case DataType.BAG: writeBag(out, (DataBag) val); break; case DataType.MAP: { writeMap(out, (Map<String, Object>) val); break; } case DataType.INTERNALMAP: { out.writeByte(INTERNALMAP); Map<Object, Object> m = (Map<Object, Object>) val; out.writeInt(m.size()); Iterator<Map.Entry<Object, Object>> i = m.entrySet().iterator(); while (i.hasNext()) { Map.Entry<Object, Object> entry = i.next(); writeDatum(out, entry.getKey()); writeDatum(out, entry.getValue()); } break; } case DataType.INTEGER: int i = (Integer) val; if (i == 0) { out.writeByte(INTEGER_0); } else if (i == 1) { out.writeByte(INTEGER_1); } else if (Byte.MIN_VALUE <= i && i <= Byte.MAX_VALUE) { out.writeByte(INTEGER_INBYTE); out.writeByte(i); } else if (Short.MIN_VALUE <= i && i <= Short.MAX_VALUE) { out.writeByte(INTEGER_INSHORT); out.writeShort(i); } else { out.writeByte(INTEGER); out.writeInt(i); } break; case DataType.LONG: long lng = (Long) val; if (lng == 0) { out.writeByte(LONG_0); } else if (lng == 1) { out.writeByte(LONG_1); } else if (Byte.MIN_VALUE <= lng && lng <= Byte.MAX_VALUE) { out.writeByte(LONG_INBYTE); out.writeByte((int) lng); } else if (Short.MIN_VALUE <= lng && lng <= Short.MAX_VALUE) { out.writeByte(LONG_INSHORT); out.writeShort((int) lng); } else if (Integer.MIN_VALUE <= lng && lng <= Integer.MAX_VALUE) { out.writeByte(LONG_ININT); out.writeInt((int) lng); } else { out.writeByte(LONG); out.writeLong(lng); } break; case DataType.DATETIME: out.writeByte(DATETIME); out.writeLong(((DateTime) val).getMillis()); out.writeShort(((DateTime) val).getZone().getOffset((DateTime) val) / ONE_MINUTE); break; case DataType.FLOAT: out.writeByte(FLOAT); out.writeFloat((Float) val); break; case DataType.BIGINTEGER: out.writeByte(BIGINTEGER); writeBigInteger(out, (BigInteger) val); break; case DataType.BIGDECIMAL: out.writeByte(BIGDECIMAL); writeBigDecimal(out, (BigDecimal) val); break; case DataType.DOUBLE: out.writeByte(DOUBLE); out.writeDouble((Double) val); break; case DataType.BOOLEAN: if ((Boolean) val) out.writeByte(BOOLEAN_TRUE); else out.writeByte(BOOLEAN_FALSE); break; case DataType.BYTE: out.writeByte(BYTE); out.writeByte((Byte) val); break; case DataType.BYTEARRAY: { DataByteArray bytes = (DataByteArray) val; SedesHelper.writeBytes(out, bytes.mData); break; } case DataType.CHARARRAY: { SedesHelper.writeChararray(out, (String) val); break; } case DataType.GENERIC_WRITABLECOMPARABLE: out.writeByte(GENERIC_WRITABLECOMPARABLE); // store the class name, so we know the class to create on read writeDatum(out, val.getClass().getName()); Writable writable = (Writable) val; writable.write(out); break; case DataType.NULL: out.writeByte(NULL); break; default: throw new RuntimeException("Unexpected data type " + val.getClass().getName() + " found in stream. " + "Note only standard Pig type is supported when you output from UDF/LoadFunc"); } }
From source file:org.apache.pig.data.DataReaderWriter.java
@SuppressWarnings("unchecked") public static void writeDatum(DataOutput out, Object val) throws IOException { // Read the data type byte type = DataType.findType(val); switch (type) { case DataType.TUPLE: // Because tuples are written directly by hadoop, the // tuple's write method needs to write the indicator byte. // So don't write the indicator byte here as it is for // everyone else. ((Tuple) val).write(out); break;//from w ww .j a v a2s .co m case DataType.BAG: out.writeByte(DataType.BAG); ((DataBag) val).write(out); break; case DataType.MAP: { out.writeByte(DataType.MAP); Map<String, Object> m = (Map<String, Object>) val; out.writeInt(m.size()); Iterator<Map.Entry<String, Object>> i = m.entrySet().iterator(); while (i.hasNext()) { Map.Entry<String, Object> entry = i.next(); writeDatum(out, entry.getKey()); writeDatum(out, entry.getValue()); } break; } case DataType.INTERNALMAP: { out.writeByte(DataType.INTERNALMAP); Map<Object, Object> m = (Map<Object, Object>) val; out.writeInt(m.size()); Iterator<Map.Entry<Object, Object>> i = m.entrySet().iterator(); while (i.hasNext()) { Map.Entry<Object, Object> entry = i.next(); writeDatum(out, entry.getKey()); writeDatum(out, entry.getValue()); } break; } case DataType.INTEGER: out.writeByte(DataType.INTEGER); out.writeInt((Integer) val); break; case DataType.LONG: out.writeByte(DataType.LONG); out.writeLong((Long) val); break; case DataType.FLOAT: out.writeByte(DataType.FLOAT); out.writeFloat((Float) val); break; case DataType.DOUBLE: out.writeByte(DataType.DOUBLE); out.writeDouble((Double) val); break; case DataType.BOOLEAN: out.writeByte(DataType.BOOLEAN); out.writeBoolean((Boolean) val); break; case DataType.BYTE: out.writeByte(DataType.BYTE); out.writeByte((Byte) val); break; case DataType.BYTEARRAY: { out.writeByte(DataType.BYTEARRAY); DataByteArray bytes = (DataByteArray) val; out.writeInt(bytes.size()); out.write(bytes.mData); break; } case DataType.CHARARRAY: { String s = (String) val; byte[] utfBytes = s.getBytes(DataReaderWriter.UTF8); int length = utfBytes.length; if (length < DataReaderWriter.UNSIGNED_SHORT_MAX) { out.writeByte(DataType.CHARARRAY); out.writeShort(length); out.write(utfBytes); } else { out.writeByte(DataType.BIGCHARARRAY); out.writeInt(length); out.write(utfBytes); } break; } case DataType.GENERIC_WRITABLECOMPARABLE: out.writeByte(DataType.GENERIC_WRITABLECOMPARABLE); //store the class name, so we know the class to create on read writeDatum(out, val.getClass().getName()); Writable writable = (Writable) val; writable.write(out); break; case DataType.NULL: out.writeByte(DataType.NULL); break; default: throw new RuntimeException("Unexpected data type " + type + " found in stream."); } }