3
주어진 개체를 byteArray로 변환하고 나중에 사용하기 위해 데이터 저장소 나 큐에 저장하려고 Kryo 라이브러리를 사용하려고합니다. 그러나 주어진 객체를 직렬화하거나 직렬화 가능 인터페이스를 구현하는 객체 만 변환 할 수 있습니까?직렬화가 불가능한 클래스와 클래스에서 kryo 직렬화 작업이 직렬화 할 수없는 속성을 가지고 있습니까?
주어진 개체를 byteArray로 변환하고 나중에 사용하기 위해 데이터 저장소 나 큐에 저장하려고 Kryo 라이브러리를 사용하려고합니다. 그러나 주어진 객체를 직렬화하거나 직렬화 가능 인터페이스를 구현하는 객체 만 변환 할 수 있습니까?직렬화가 불가능한 클래스와 클래스에서 kryo 직렬화 작업이 직렬화 할 수없는 속성을 가지고 있습니까?
java.io.Serialiable을 구현하지 않은 경우 및/또는 해당 속성이 Serializable을 구현하지 않는 경우에도 Kryo를 사용하여 빈을 직렬화/비 직렬화 할 수 있습니다 (Kryo 2.10을 사용하여 예제를 실행합니다. 비 기본 생성자가 존재하기 때문에 인수를 생성자를 명시 적으로) 여기에 정의되지했다는 것을 제한 :
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
public class KryoSerializerExample {
public static void main(String[] args) {
KryoHelper kryoHelper = new KryoHelper();
ClassNotImplementingSerializable obj1 = new ClassNotImplementingSerializable(123, 456, "789");
ClassNotImplementingSerializable obj2 = (ClassNotImplementingSerializable) kryoHelper.fromBytes(kryoHelper.toBytes(obj1));
if (obj1.equals(obj2)) {
System.out.println("the object and its clone are equal as expected");
} else {
System.out.println("the object and its clone are not equal, something went wrong");
}
}
public static class KryoHelper {
Kryo kryo;
public KryoHelper() {
super();
kryo=new Kryo();
}
public byte[] toBytes(Object obj){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (Output output = new Output(baos)) {
kryo.writeClassAndObject(output, obj);
}
byte[] bytes = baos.toByteArray();
return bytes;
}
public Object fromBytes(byte[] bytes){
Object retrievedObject;
try (Input input = new Input(new ByteArrayInputStream(bytes))){
retrievedObject = kryo.readClassAndObject(input);
}
return retrievedObject;
}
}
public static class ClassNotImplementingSerializable {
private int a;
private ClassNotImplementingSerializable2 b;
/**
* no-arg constructor is necessary
*/
public ClassNotImplementingSerializable() {
}
public ClassNotImplementingSerializable(int a, int b, String c) {
this.a = a;
this.b = new ClassNotImplementingSerializable2(b,c);
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public ClassNotImplementingSerializable2 getB() {
return b;
}
public void setB(ClassNotImplementingSerializable2 b) {
this.b = b;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + a;
result = prime * result + ((b == null) ? 0 : b.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ClassNotImplementingSerializable other = (ClassNotImplementingSerializable) obj;
if (a != other.a)
return false;
if (b == null) {
if (other.b != null)
return false;
} else if (!b.equals(other.b))
return false;
return true;
}
}
public static class ClassNotImplementingSerializable2 {
private int a;
private String b;
/**
* no-arg constructor is necessary
*/
public ClassNotImplementingSerializable2() {
}
public ClassNotImplementingSerializable2(int a, String b) {
this.a = a;
this.b = b;
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + a;
result = prime * result + ((b == null) ? 0 : b.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ClassNotImplementingSerializable2 other = (ClassNotImplementingSerializable2) obj;
if (a != other.a)
return false;
if (b == null) {
if (other.b != null)
return false;
} else if (!b.equals(other.b))
return false;
return true;
}
}
}
예를 들어 java.sql.Timestamp
같은 일부 특정 필드 유형에 문제가있을 수는 (비록이 하나 주위에 방법이 ), 물론 java.lang.Thread
등이 있습니다.
java.io.Serialiable을 구현하지 않은 경우에도 Bean을 직렬화 할 수 있어야합니다 (예 : 테스트 클래스 BeanSerializerTest 참조). –
@ John-Donn 답변을 작성하고 지정된 예제 링크를 제공하십시오. –
@MikePone이 답변을 추가했습니다. –