Skip to content

Commit eca3107

Browse files
committed
Java 二进制序列化示例
1 parent d63e5a5 commit eca3107

File tree

7 files changed

+282
-0
lines changed

7 files changed

+282
-0
lines changed

javalib-io-binary/pom.xml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>io.github.dunwu</groupId>
8+
<artifactId>javalib-io-binary</artifactId>
9+
<version>1.0.0</version>
10+
11+
<name>javalib-io-binary</name>
12+
<description>Java binary serialize Lib Examples</description>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<java.version>1.8</java.version>
17+
<maven.compiler.source>${java.version}</maven.compiler.source>
18+
<maven.compiler.target>${java.version}</maven.compiler.target>
19+
<dunwu.version>0.4.8</dunwu.version>
20+
</properties>
21+
22+
<dependencies>
23+
<!--serialization-->
24+
<dependency>
25+
<groupId>de.ruedigermoeller</groupId>
26+
<artifactId>fst</artifactId>
27+
<version>2.56</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>com.esotericsoftware</groupId>
31+
<artifactId>kryo</artifactId>
32+
<version>5.0.0-RC4</version>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.projectlombok</groupId>
37+
<artifactId>lombok</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>io.github.dunwu</groupId>
41+
<artifactId>dunwu-common</artifactId>
42+
</dependency>
43+
44+
<!--test-->
45+
<dependency>
46+
<groupId>junit</groupId>
47+
<artifactId>junit</artifactId>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.assertj</groupId>
52+
<artifactId>assertj-core</artifactId>
53+
<scope>test</scope>
54+
</dependency>
55+
</dependencies>
56+
<dependencyManagement>
57+
<dependencies>
58+
<dependency>
59+
<groupId>io.github.dunwu</groupId>
60+
<artifactId>dunwu-dependencies</artifactId>
61+
<version>${dunwu.version}</version>
62+
<type>pom</type>
63+
<scope>import</scope>
64+
</dependency>
65+
</dependencies>
66+
</dependencyManagement>
67+
68+
<build>
69+
<resources>
70+
<resource>
71+
<directory>src/main/resources</directory>
72+
</resource>
73+
</resources>
74+
</build>
75+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.github.dunwu.javalib.io;
2+
3+
import org.nustaq.serialization.FSTConfiguration;
4+
5+
import java.io.IOException;
6+
7+
/**
8+
* fast-serialization lib Demo
9+
*
10+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
11+
* @see <a href="https://github.com/RuedigerMoeller/fast-serialization">FST</a>
12+
* @since 2019-11-22
13+
*/
14+
public class FstDemo {
15+
16+
private static FSTConfiguration DEFAULT_CONFIG = FSTConfiguration.createDefaultConfiguration();
17+
18+
public static <T> byte[] serialize(T obj) {
19+
return DEFAULT_CONFIG.asByteArray(obj);
20+
}
21+
22+
public static <T> T deserialize(byte[] bytes, Class<T> clazz) throws IOException {
23+
Object obj = DEFAULT_CONFIG.asObject(bytes);
24+
if (clazz.isInstance(obj)) {
25+
return (T) obj;
26+
} else {
27+
throw new IOException("derialize failed");
28+
}
29+
}
30+
31+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.github.dunwu.javalib.io;
2+
3+
import java.io.*;
4+
5+
/**
6+
* JDK 默认序列化、反序列化机制示例
7+
*
8+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
9+
* @since 2019-11-22
10+
*/
11+
public class JdkSerializeDemo {
12+
13+
public static <T> byte[] serialize(T obj) throws IOException {
14+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
15+
ObjectOutputStream oos = new ObjectOutputStream(baos);
16+
oos.writeObject(obj);
17+
byte[] bytes = baos.toByteArray();
18+
baos.close();
19+
oos.close();
20+
return bytes;
21+
}
22+
23+
public static <T> T deserialize(byte[] bytes, Class<T> clazz) throws IOException, ClassNotFoundException {
24+
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
25+
ObjectInputStream ois = new ObjectInputStream(bais);
26+
Object obj = ois.readObject();
27+
bais.close();
28+
ois.close();
29+
if (clazz.isInstance(obj)) {
30+
return (T) obj;
31+
} else {
32+
throw new IOException("derialize failed");
33+
}
34+
}
35+
36+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package io.github.dunwu.javalib.io;
2+
3+
import io.github.dunwu.javalib.io.bean.BeanUtils;
4+
import io.github.dunwu.javalib.io.bean.TestBean;
5+
import org.junit.Test;
6+
7+
import java.io.IOException;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
/**
12+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
13+
* @since 2019-11-22
14+
*/
15+
public class SerializeTest {
16+
17+
private static final int BATCH_SIZE = 100000;
18+
19+
@Test
20+
public void testJdkSerialize() throws IOException, ClassNotFoundException {
21+
long begin = System.currentTimeMillis();
22+
for (int i = 0; i < BATCH_SIZE; i++) {
23+
TestBean oldBean = BeanUtils.initJdk8Bean();
24+
byte[] bytes = JdkSerializeDemo.serialize(oldBean);
25+
assertThat(bytes).isNotEmpty();
26+
TestBean newBean = JdkSerializeDemo.deserialize(bytes, TestBean.class);
27+
assertThat(newBean).isNotNull();
28+
}
29+
long end = System.currentTimeMillis();
30+
System.out.printf("JDK 默认序列化/反序列化耗时:%s", (end - begin));
31+
}
32+
33+
@Test
34+
public void testFst() throws IOException {
35+
long begin = System.currentTimeMillis();
36+
for (int i = 0; i < BATCH_SIZE; i++) {
37+
TestBean oldBean = BeanUtils.initJdk8Bean();
38+
byte[] bytes = FstDemo.serialize(oldBean);
39+
assertThat(bytes).isNotEmpty();
40+
TestBean newBean = FstDemo.deserialize(bytes, TestBean.class);
41+
assertThat(newBean).isNotNull();
42+
}
43+
long end = System.currentTimeMillis();
44+
System.out.printf("FST 序列化/反序列化耗时:%s", (end - begin));
45+
}
46+
47+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.github.dunwu.javalib.io.bean;
2+
3+
import java.sql.Date;
4+
import java.time.LocalDate;
5+
import java.time.LocalDateTime;
6+
import java.util.*;
7+
8+
/**
9+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
10+
* @since 2019-11-22
11+
*/
12+
public class BeanUtils {
13+
14+
public static TestBean initJdk8Bean() {
15+
String[] strArray = { "a", "b", "c" };
16+
Integer[] intArray = { 1, 2, 3, 4, 5 };
17+
List<Integer> intList = new ArrayList<>();
18+
intList.addAll(Arrays.asList(intArray));
19+
Map<String, Object> map = new HashMap<>();
20+
map.put("name", "jack");
21+
map.put("age", 18);
22+
map.put("length", 175.3f);
23+
TestBean bean = new TestBean();
24+
Date date = Date.valueOf("2019-11-22");
25+
LocalDateTime localDateTime = LocalDateTime.of(2000, 1, 1, 12, 0, 0);
26+
LocalDate localDate = LocalDate.of(1949, 10, 1);
27+
bean.setI1(10).setI2(1024).setF1(0.5f).setD1(100.0)
28+
.setDate1(date).setDate2(localDateTime).setDate3(localDate)
29+
.setColor(TestBean.Color.BLUE).setStrArray(strArray).setIntList(intList).setMap(map);
30+
return bean;
31+
}
32+
33+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package io.github.dunwu.javalib.io.bean;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
import lombok.ToString;
7+
import lombok.experimental.Accessors;
8+
9+
import java.io.Serializable;
10+
import java.time.LocalDate;
11+
import java.time.LocalDateTime;
12+
import java.util.Date;
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
/**
17+
* 定义一个满足大多数情况的 Bean 结构(含 JDK8 数据类型),使得各种 Json 库测试性能时能相对公平
18+
*
19+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
20+
* @since 2019-11-22
21+
*/
22+
@Data
23+
@ToString
24+
@Accessors(chain = true)
25+
@NoArgsConstructor
26+
@AllArgsConstructor
27+
public class TestBean implements Serializable {
28+
29+
private static final long serialVersionUID = -6473181683996762084L;
30+
31+
private int i1;
32+
33+
private Integer i2;
34+
35+
private float f1;
36+
37+
private Double d1;
38+
39+
private Date date1;
40+
41+
private LocalDateTime date2;
42+
43+
private LocalDate date3;
44+
45+
private Color color;
46+
47+
private String[] strArray;
48+
49+
private List<Integer> intList;
50+
51+
private Map<String, Object> map;
52+
53+
public static enum Color {
54+
RED,
55+
YELLOW,
56+
BLUE
57+
}
58+
59+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<module>javalib-bean</module>
1717
<module>javalib-cli</module>
1818
<module>javalib-io</module>
19+
<module>javalib-io-binary</module>
1920
<module>javalib-io-json</module>
2021
<module>javalib-log</module>
2122
<module>javalib-mvel</module>

0 commit comments

Comments
 (0)