-
Notifications
You must be signed in to change notification settings - Fork 587
/
SerializeTestHelper.java
87 lines (77 loc) · 2.62 KB
/
SerializeTestHelper.java
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Copyright (c) 2022 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
public final class SerializeTestHelper
{
private SerializeTestHelper()
{
throw new AssertionError("Suppress default constructor for noninstantiability");
}
public static <T> T serializeDeserialize(T sourceObject)
{
byte[] pileOfBytes = serialize(sourceObject);
return (T) deserialize(pileOfBytes);
}
public static <T> byte[] serialize(T sourceObject)
{
ByteArrayOutputStream baos = SerializeTestHelper.getByteArrayOutputStream(sourceObject);
return baos.toByteArray();
}
public static <T> ByteArrayOutputStream getByteArrayOutputStream(T sourceObject)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try
{
writeObjectToStream(sourceObject, baos);
}
catch (IOException e)
{
Verify.fail("Failed to marshal an object", e);
}
return baos;
}
private static <T> void writeObjectToStream(Object sourceObject, ByteArrayOutputStream baos) throws IOException
{
try (ObjectOutput objectOutputStream = new ObjectOutputStream(baos))
{
objectOutputStream.writeObject(sourceObject);
objectOutputStream.flush();
objectOutputStream.close();
}
}
private static Object readOneObject(ByteArrayInputStream bais)
throws IOException, ClassNotFoundException
{
try (ObjectInput objectStream = new ObjectInputStream(bais))
{
return objectStream.readObject();
}
}
public static Object deserialize(byte[] pileOfBytes)
{
ByteArrayInputStream bais = new ByteArrayInputStream(pileOfBytes);
try
{
return readOneObject(bais);
}
catch (ClassNotFoundException | IOException e)
{
Verify.fail("Failed to unmarshal an object", e);
}
return null;
}
}