/** Write an operation to the edit log */ void logEdit(byte op, Writable w1, Writable w2) { synchronized (editlog) { try { editlog.write(op); if (w1 != null) { w1.write(editlog); } if (w2 != null) { w2.write(editlog); } } catch (IOException ie) { } } }
/** {@inheritDoc} */ @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(id); boolean writable = innerSplit instanceof Writable; out.writeUTF(writable ? innerSplit.getClass().getName() : null); if (writable) ((Writable) innerSplit).write(out); else out.writeObject(innerSplit); }
/** Convert writables to a byte array */ public static byte[] toByteArray(Writable... writables) { final DataOutputBuffer out = new DataOutputBuffer(); try { for (Writable w : writables) { w.write(out); } out.close(); } catch (IOException e) { throw new RuntimeException("Fail to convert writables to a byte array", e); } return out.getData(); }
/** Write a {@link Writable}, {@link String}, primitive type, or an array of the preceding. */ public static void writeObject( DataOutput out, Object instance, Class declaredClass, Configuration conf) throws IOException { if (instance == null) { // null instance = new NullInstance(declaredClass, conf); declaredClass = Writable.class; } UTF8.writeString(out, declaredClass.getName()); // always write declared if (declaredClass.isArray()) { // array int length = Array.getLength(instance); out.writeInt(length); for (int i = 0; i < length; i++) { writeObject(out, Array.get(instance, i), declaredClass.getComponentType(), conf); } } else if (declaredClass == String.class) { // String UTF8.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 UTF8.writeString(out, ((Enum) instance).name()); } else if (Writable.class.isAssignableFrom(declaredClass)) { // Writable UTF8.writeString(out, instance.getClass().getName()); ((Writable) instance).write(out); } else { throw new IOException("Can't write: " + instance + " as " + declaredClass); } }
/** * Make a copy of a writable object using serialization to a buffer. * * @param orig The object to copy * @return The copied object */ public static Writable clone(Writable orig, JobConf conf) { try { Writable newInst = (Writable) conf.newInstance(orig.getClass()); CopyInCopyOutBuffer buffer = (CopyInCopyOutBuffer) cloneBuffers.get(); buffer.outBuffer.reset(); orig.write(buffer.outBuffer); buffer.moveData(); newInst.readFields(buffer.inBuffer); return newInst; } catch (IOException e) { throw new RuntimeException("Error writing/reading clone buffer", e); } }
/** Utility method for testing writables. */ public static Writable testWritable(Writable before, Configuration conf) throws Exception { DataOutputBuffer dob = new DataOutputBuffer(); before.write(dob); DataInputBuffer dib = new DataInputBuffer(); dib.reset(dob.getData(), dob.getLength()); Writable after = (Writable) ReflectionUtils.newInstance(before.getClass(), conf); after.readFields(dib); assertEquals(before, after); return after; }
/** Used by child copy constructors. */ protected synchronized void copy(Writable other) { if (other != null) { try { DataOutputBuffer out = new DataOutputBuffer(); other.write(out); DataInputBuffer in = new DataInputBuffer(); in.reset(out.getData(), out.getLength()); readFields(in); } catch (IOException e) { throw new IllegalArgumentException("map cannot be copied: " + e.getMessage()); } } else { throw new IllegalArgumentException("source map cannot be null"); } }