@Override @SuppressWarnings("unchecked") public void readFields(DataInput in) throws IOException { this.fst = null; this.snd = null; final ClassIndex index = new ClassIndex(); index.readFields(in); int flags = in.readByte(); try { if ((flags & FST_NULL) == NOT_NULL) { fst = (A) index.getClass(in.readByte()).newInstance(); fst.readFields(in); } if ((flags & SND_NULL) == NOT_NULL) { snd = (B) index.getClass(in.readByte()).newInstance(); snd.readFields(in); } } catch (Exception e) { throw new RuntimeException(e); } }
@Override public void write(DataOutput out) throws IOException { // Serialize class names of each non-null field final ClassIndex index = new ClassIndex(); if (fst != null) index.addClass(fst.getClass()); if (snd != null) index.addClass(snd.getClass()); index.write(out); // Indicate which fields are null out.writeByte((fst == null ? FST_NULL : NOT_NULL) | (snd == null ? SND_NULL : NOT_NULL)); // Serialize each non-null field if (fst != null) { out.writeByte(index.getId(fst.getClass())); fst.write(out); } if (snd != null) { out.writeByte(index.getId(snd.getClass())); snd.write(out); } }