1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
public
class
Person
implements
Serializable {
private
String name =
null
;
private
Integer age =
null
;
private
Gender gender =
null
;
public
Person() {
System.out.println(
"none-arg constructor"
);
}
public
Person(String name, Integer age, Gender gender) {
System.out.println(
"arg constructor"
);
this
.name = name;
this
.age = age;
this
.gender = gender;
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
Integer getAge() {
return
age;
}
public
void
setAge(Integer age) {
this
.age = age;
}
public
Gender getGender() {
return
gender;
}
public
void
setGender(Gender gender) {
this
.gender = gender;
}
@Override
public
String toString() {
return
"["
+ name +
", "
+ age +
", "
+ gender +
"]"
;
}
}
|
SimpleSerial,是一个简单的序列化程序,它先将一个Person对象保存到文件person.out中,然后再从该文件中读出被存储的Person对象,并打印该对象。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public
class
SimpleSerial {
public
static
void
main(String[] args)
throws
Exception {
File file =
new
File(
"person.out"
);
ObjectOutputStream oout =
new
ObjectOutputStream(
new
FileOutputStream(file));
Person person =
new
Person(
"John"
,
101
, Gender.MALE);
oout.writeObject(person);
oout.close();
ObjectInputStream oin =
new
ObjectInputStream(
new
FileInputStream(file));
Object newPerson = oin.readObject();
// 没有强制转换到Person类型
oin.close();
System.out.println(newPerson);
}
}
|
上述程序的输出的结果为:
arg constructor
[John,
31
, MALE]
1
2
3
4
5
|
package
org.apache.hadoop.io;
public
interface
Writable {
void
write(DataOutput out)
throws
IOException;
void
readFields(DataInput in)
throws
IOException;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
public
class
TestComparator {
RawComparator<IntWritable> comparator;
IntWritable w1;
IntWritable w2;
/**
* 获得IntWritable的comparator,并初始化两个IntWritable
*/
@Before
public
void
init() {
comparator = WritableComparator.get(IntWritable.
class
);
w1 =
new
IntWritable(
163
);
w2 =
new
IntWritable(
76
);
}
/**
* 比较两个对象大小
*/
@Test
public
void
testComparator() {
Assert.assertEquals(comparator.compare(w1, w2) >
0
,
true
);
}
/**
* 序列号后进行直接比较
*
* @throws IOException
*/
@Test
public
void
testcompare()
throws
IOException {
byte
[] b1 = serialize(w1);
byte
[] b2 = serialize(w2);
Assert.assertTrue(comparator.compare(b1,
0
, b1.length, b2,
0
, b2.length) >
0
);
}
/**
* 将一个实现了Writable接口的对象序列化成字节流
*
* @param writable
* @return
* @throws java.io.IOException
*/
public
static
byte
[] serialize(Writable writable)
throws
IOException {
ByteArrayOutputStream out =
new
ByteArrayOutputStream();
DataOutputStream dataOut =
new
DataOutputStream(out);
writable.write(dataOut);
dataOut.close();
return
out.toByteArray();
}
}
|
1
2
3
4
5
6
7
|
public
static
byte
[] serialize(Writable writable)
throws
IOException {
ByteArrayOutputStream out =
new
ByteArrayOutputStream();
DataOutputStream dataOut =
new
DataOutputStream(out);
writable.write(dataOut);
dataOut.close();
return
out.toByteArray();
}
|
1
2
3
4
5
6
7
8
|
public
static
byte
[] deserialize(Writable writable,
byte
[] bytes)
throws
IOException {
ByteArrayInputStream in =
new
ByteArrayInputStream(bytes);
DataInputStream dataIn =
new
DataInputStream(in);
writable.readFields(dataIn);
dataIn.close();
return
bytes;
}
|