iTE*_*Egg 275 java serialization object
假设我有一个可序列化的类AppMessage.
我想将它作为byte[]套接字传输到另一台机器,在那里从接收的字节重建它.
我怎么能实现这个目标?
Tay*_*ese 399
准备要发送的字节:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(yourObject);
out.flush();
byte[] yourBytes = bos.toByteArray();
...
} finally {
try {
bos.close();
} catch (IOException ex) {
// ignore close exception
}
}
Run Code Online (Sandbox Code Playgroud)
从字节创建对象:
ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes);
ObjectInput in = null;
try {
in = new ObjectInputStream(bis);
Object o = in.readObject();
...
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
// ignore close exception
}
}
Run Code Online (Sandbox Code Playgroud)
uri*_*ris 288
最好的方法是使用SerializationUtilsApache Commons Lang.
要序列化:
byte[] data = SerializationUtils.serialize(yourObject);
Run Code Online (Sandbox Code Playgroud)
要反序列化:
YourObject yourObject = SerializationUtils.deserialize(data)
Run Code Online (Sandbox Code Playgroud)
如上所述,这需要Commons Lang库.它可以使用Gradle导入:
compile 'org.apache.commons:commons-lang3:3.5'
Run Code Online (Sandbox Code Playgroud)
Maven的:
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
或者,可以导入整个集合.请参阅此链接
Víc*_*ero 84
如果使用Java> = 7,则可以使用try with resources改进已接受的解决方案:
private byte[] convertToBytes(Object object) throws IOException {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos)) {
out.writeObject(object);
return bos.toByteArray();
}
}
Run Code Online (Sandbox Code Playgroud)
反过来说:
private Object convertFromBytes(byte[] bytes) throws IOException, ClassNotFoundException {
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInput in = new ObjectInputStream(bis)) {
return in.readObject();
}
}
Run Code Online (Sandbox Code Playgroud)
另一个有趣的方法来自com.fasterxml.jackson.databind.ObjectMapper
byte[] data = new ObjectMapper().writeValueAsBytes(JAVA_OBJECT_HERE)
Maven依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
可以通过SerializationUtils完成,通过ApacheUtils的序列化和反序列化方法将对象转换为byte [],反之亦然,如@uris回答中所述。
通过序列化将对象转换为byte []:
byte[] data = SerializationUtils.serialize(object);
Run Code Online (Sandbox Code Playgroud)
通过反序列化将byte []转换为对象:
Object object = (Object) SerializationUtils.deserialize(byte[] data)
Run Code Online (Sandbox Code Playgroud)
单击链接下载org-apache-commons-lang.jar
通过单击以下命令来集成.jar文件:
FileName- > Open Medule Settings- > 选择您的模块 -> Dependencies- > Add Jar文件,您完成了。
希望这会有所帮助。
小智 5
我还建议使用 SerializationUtils 工具。我想对@Abilash 的错误评论进行调整。该SerializationUtils.serialize()方法不局限于1024个字节,违背了这里的另一个答案。
public static byte[] serialize(Object object) {
if (object == null) {
return null;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
try {
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(object);
oos.flush();
}
catch (IOException ex) {
throw new IllegalArgumentException("Failed to serialize object of type: " + object.getClass(), ex);
}
return baos.toByteArray();
}
Run Code Online (Sandbox Code Playgroud)
第一眼?您可能认为new ByteArrayOutputStream(1024)这只允许固定大小。但是,如果您仔细查看ByteArrayOutputStream,您会发现如果有必要,流会增长:
这个类实现了一个输出流,其中数据被写入一个字节数组。缓冲区会随着数据写入而自动增长。可以使用
toByteArray()和 检索数据toString()。
如果您使用 spring,则 spring-core 中有一个可用的 util 类。你可以简单地做
import org.springframework.util.SerializationUtils;
byte[] bytes = SerializationUtils.serialize(anyObject);
Object object = SerializationUtils.deserialize(bytes);
Run Code Online (Sandbox Code Playgroud)