IMDG (In Memory Data Grid)로 작업 중이며 마이그레이션 도구가 있습니다. 모든 객체가 성공적으로 마이그레이션되었는지 확인하기 위해 직렬화 된 버전에서 객체의 척을 계산합니다.HashMap 직렬화 및 직렬화 해제 변경
우리가 직렬화하는 HashMap에 문제가있는 것을 볼 수 있습니다.하지만 우리가 직렬화를 해제하면 체크섬이 변경됩니다. 다음은 간단한 테스트 케이스이다 :
@Test
public void testMapSerialization() throws IOException, ClassNotFoundException {
TestClass tc1 = new TestClass();
tc1.init();
String checksum1 = SpaceObjectUtils.calculateChecksum(tc1);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
byte[] objBytes = null;
out = new ObjectOutputStream(bos);
out.writeObject(tc1);
objBytes = bos.toByteArray();
out.close();
ByteArrayInputStream bis = new ByteArrayInputStream(objBytes);
ObjectInputStream in = new ObjectInputStream(bis);
TestClass tc2 = (TestClass) in.readObject();
String checksum2 = SpaceObjectUtils.calculateChecksum(tc2);
assertEquals(checksum1, checksum2);
}
의 TestClass는 다음과 같습니다
class TestClass implements Serializable {
private static final long serialVersionUID = 5528034467300853270L;
private Map<String, Object> map;
public TestClass() {
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public void init() {
map = new HashMap<String, Object>();
map.put("name", Integer.valueOf(4));
map.put("type", Integer.valueOf(4));
map.put("emails", new BigDecimal("43.3"));
map.put("theme", "sdfsd");
map.put("notes", Integer.valueOf(4));
map.put("addresses", Integer.valueOf(4));
map.put("additionalInformation", new BigDecimal("43.3"));
map.put("accessKey", "sdfsd");
map.put("accountId", Integer.valueOf(4));
map.put("password", Integer.valueOf(4));
map.put("domain", new BigDecimal("43.3"));
}
}
을 그리고 이것은 체크섬 계산하는 방법이다 : 당신의지도를 인쇄 할 경우
public static String calculateChecksum(Serializable obj) {
if (obj == null) {
throw new IllegalArgumentException("The object cannot be null");
}
MessageDigest digest = null;
try {
digest = MessageDigest.getInstance("MD5");
} catch (java.security.NoSuchAlgorithmException nsae) {
throw new IllegalStateException("Algorithm MD5 is not present", nsae);
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
byte[] objBytes = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(obj);
objBytes = bos.toByteArray();
out.close();
} catch (IOException e) {
throw new IllegalStateException(
"There was a problem trying to get the byte stream of this object: " + obj.toString());
}
digest.update(objBytes);
byte[] hash = digest.digest();
StringBuilder hexString = new StringBuilder();
for (int i = 0; i < hash.length; i++) {
String hex = Integer.toHexString(0xFF & hash[i]);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
을 tc1 및 tc2를 사용하면 요소가 같은 위치에 있지 않음을 알 수 있습니다.
{accessKey=sdfsd, accountId=4, theme=sdfsd, name=4, domain=43.3, additionalInformation=43.3, emails=43.3, addresses=4, notes=4, type=4, password=4}
{accessKey=sdfsd, accountId=4, name=4, theme=sdfsd, domain=43.3, emails=43.3, additionalInformation=43.3, type=4, notes=4, addresses=4, password=4}
나는 그것을 역 직렬화 할 때 HashMap을 직렬화하고 동일한 체크섬을 얻고 싶습니다. 해결책이 있는지 아니면 내가 뭔가 잘못하고 있는지 아십니까?
감사합니다.
디에고는 당신이 잘못 아무것도하지 않고있다
감사합니다. 다른 구현으로 변경하는 것을 피하고 싶지만 IMDG에서 관계형 데이터베이스로 지속될 때 우리가 가지고 있던 다른 문제를 해결할 때 더 나은 솔루션이되었습니다. – dgaviola