@Test
public void testWritableIO() throws IOException {
Writable[] vectorValues = new Writable[] {
new Text("test1"), new Text("test2"), new Text("test3")
};
ArrayWritable vector = new ArrayWritable(Text.class, vectorValues);
MapWritable map = new MapWritable();
map.put(new Text("one"), new VIntWritable(1));
map.put(new Text("two"), new VLongWritable(2));
Writable[] writables = new Writable[] {
new BytesWritable(new byte[] { 1, 2, 3, 4 }),
new ByteWritable((byte) 123), new BooleanWritable(true),
new VIntWritable(12345), new VLongWritable(123456789L),
new FloatWritable((float) 1.2), new DoubleWritable(1.234),
new Text("random string")
};
TypedBytesWritable tbw = new TypedBytesWritable();
tbw.setValue("typed bytes text");
RecRecord1 r1 = new RecRecord1();
r1.setBoolVal(true);
r1.setByteVal((byte) 0x66);
r1.setFloatVal(3.145F);
r1.setDoubleVal(1.5234);
r1.setIntVal(-4567);
r1.setLongVal(-2367L);
r1.setStringVal("random text");
r1.setBufferVal(new Buffer());
r1.setVectorVal(new ArrayList<String>());
r1.setMapVal(new TreeMap<String, String>());
RecRecord0 r0 = new RecRecord0();
r0.setStringVal("other random text");
r1.setRecordVal(r0);
FileOutputStream ostream = new FileOutputStream(tmpfile);
DataOutputStream dostream = new DataOutputStream(ostream);
TypedBytesWritableOutput out = new TypedBytesWritableOutput(dostream);
for (Writable w : writables) {
out.write(w);
}
out.write(tbw);
out.write(vector);
out.write(map);
out.write(r1);
dostream.close();
ostream.close();
FileInputStream istream = new FileInputStream(tmpfile);
DataInputStream distream = new DataInputStream(istream);
TypedBytesWritableInput in = new TypedBytesWritableInput(distream);
for (Writable w : writables) {
assertEquals(w, in.read());
}
assertEquals(tbw.getValue().toString(), in.read().toString());
assertEquals(ArrayWritable.class, in.readType());
ArrayWritable aw = in.readArray();
Writable[] writables1 = vector.get(), writables2 = aw.get();
assertEquals(writables1.length, writables2.length);
for (int i = 0; i < writables1.length; i++) {
assertEquals(((Text) writables1[i]).toString(),
((TypedBytesWritable) writables2[i]).getValue());
}