您好,登錄后才能下訂單哦!
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.ShortWritable;
import org.junit.Test;
public class TestWritable {
/*
* test writable
*/
@Test
public void test1() throws Exception {
IntWritable age = new IntWritable();
age.set(163);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dataOut = new DataOutputStream(baos);
// 序列化
age.write(dataOut);
dataOut.close();
byte[] bts = baos.toByteArray();
for (byte by : bts) {
System.out.println(by);
}
System.out.println(bts.toString());
// 反序列化
IntWritable age2 = new IntWritable();
age2.readFields(new DataInputStream(new ByteArrayInputStream(baos
.toByteArray())));
System.out.println(age2.get());
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
DataInputStream dis = new DataInputStream(bais);
System.out.println(dis.readInt());
// //測試用short類型方式的讀取
// ShortWritable short1 = new ShortWritable();
// short1.readFields(dis);
// System.out.println(short1.get());
// ShortWritable short2 = new ShortWritable();
// short2.readFields(dis);
// System.out.println(short2.get());
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。