public void readFields(DataInput in) throws IOException { values = new Writable[in.readInt()]; // construct values for (int i = 0; i < values.length; i++) { Writable value = WritableFactories.newInstance(valueClass); value.readFields(in); // read a value values[i] = value; // store it in values } }
/** * 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; }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { id = in.readInt(); String clsName = in.readUTF(); if (clsName == null) innerSplit = in.readObject(); else { // Split wrapper only used when classes available in our classpath, so Class.forName is ok // here. Class<Writable> cls = (Class<Writable>) Class.forName(clsName); try { innerSplit = U.newInstance(cls); } catch (GridException e) { throw new IOException(e); } ((Writable) innerSplit).readFields(in); } }
@Override public void readFields(DataInput in) throws IOException { // construct matrix values = new Writable[in.readInt()][]; for (int i = 0; i < values.length; i++) { values[i] = new Writable[in.readInt()]; } // construct values for (int i = 0; i < values.length; i++) { for (int j = 0; j < values[i].length; j++) { Writable value; // construct value try { value = (Writable) valueClass.newInstance(); } catch (InstantiationException e) { throw new RuntimeException(e.toString()); } catch (IllegalAccessException e) { throw new RuntimeException(e.toString()); } value.readFields(in); // read a value values[i][j] = value; // store it in values } } }
/** Read a {@link Writable}, {@link String}, primitive type, or an array of the preceding. */ @SuppressWarnings("unchecked") public static Object readObject(DataInput in, ObjectWritable objectWritable, Configuration conf) throws IOException { String className = UTF8.readString(in); Class<?> declaredClass = PRIMITIVE_NAMES.get(className); if (declaredClass == null) { declaredClass = loadClass(conf, className); } Object instance; if (declaredClass.isPrimitive()) { // primitive types if (declaredClass == Boolean.TYPE) { // boolean instance = Boolean.valueOf(in.readBoolean()); } else if (declaredClass == Character.TYPE) { // char instance = Character.valueOf(in.readChar()); } else if (declaredClass == Byte.TYPE) { // byte instance = Byte.valueOf(in.readByte()); } else if (declaredClass == Short.TYPE) { // short instance = Short.valueOf(in.readShort()); } else if (declaredClass == Integer.TYPE) { // int instance = Integer.valueOf(in.readInt()); } else if (declaredClass == Long.TYPE) { // long instance = Long.valueOf(in.readLong()); } else if (declaredClass == Float.TYPE) { // float instance = Float.valueOf(in.readFloat()); } else if (declaredClass == Double.TYPE) { // double instance = Double.valueOf(in.readDouble()); } else if (declaredClass == Void.TYPE) { // void instance = null; } else { throw new IllegalArgumentException("Not a primitive: " + declaredClass); } } else if (declaredClass.isArray()) { // array int length = in.readInt(); instance = Array.newInstance(declaredClass.getComponentType(), length); for (int i = 0; i < length; i++) { Array.set(instance, i, readObject(in, conf)); } } else if (declaredClass == String.class) { // String instance = UTF8.readString(in); } else if (declaredClass.isEnum()) { // enum instance = Enum.valueOf((Class<? extends Enum>) declaredClass, UTF8.readString(in)); } else { // Writable Class instanceClass = null; String str = UTF8.readString(in); instanceClass = loadClass(conf, str); Writable writable = WritableFactories.newInstance(instanceClass, conf); writable.readFields(in); instance = writable; if (instanceClass == NullInstance.class) { // null declaredClass = ((NullInstance) instance).declaredClass; instance = null; } } if (objectWritable != null) { // store values objectWritable.declaredClass = declaredClass; objectWritable.instance = instance; } return instance; }