-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSharedArrayExample.java
More file actions
47 lines (43 loc) · 1.54 KB
/
Copy pathSharedArrayExample.java
File metadata and controls
47 lines (43 loc) · 1.54 KB
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
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* Generates the fixture of issue #62: the same array is stored in two
* fields, so it is written once and referenced the second time with a
* TC_REFERENCE.
*
* Reading it required two fixes in v2: arrays were given a handle but were
* never stored, so the reference could not be resolved, and a reference
* found in an array field was read as a class description.
*
* The trailing 'marker' field detects a desynchronized stream: it is read
* right after the shared array.
*
* Run it with: java SharedArrayExample.java
*/
class SharedArrayHolder implements Serializable {
private static final long serialVersionUID = 1L;
private byte[] first;
/** Same array as 'first': written as a reference. */
private byte[] second;
private String[] strings;
/** Same array as 'strings': written as a reference. */
private String[] sameStrings;
/** Read after the references: wrong if the stream is desynchronized. */
private int marker = 443;
SharedArrayHolder() {
first = new byte[] {1, 2, 3};
second = first;
strings = new String[] {"a", "b"};
sameStrings = strings;
}
}
public class SharedArrayExample {
public static void main(String[] args) throws IOException {
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("testSharedArray.ser"))) {
oos.writeObject(new SharedArrayHolder());
}
}
}