equal
deleted
inserted
replaced
1 /* |
1 /* |
2 * Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
31 import java.io.*; |
31 import java.io.*; |
32 import java.util.HashSet; |
32 import java.util.HashSet; |
33 import java.util.Iterator; |
33 import java.util.Iterator; |
34 |
34 |
35 class A implements Serializable { |
35 class A implements Serializable { |
36 static HashSet writeObjectExtent = new HashSet(); |
36 private static final long serialVersionUID = 1L; |
|
37 |
|
38 static HashSet<A>writeObjectExtent = new HashSet<>(); |
37 |
39 |
38 private void writeObject(ObjectOutputStream out) throws IOException { |
40 private void writeObject(ObjectOutputStream out) throws IOException { |
39 if (writeObjectExtent.contains(this)) { |
41 if (writeObjectExtent.contains(this)) { |
40 throw new InvalidObjectException("writeObject: object " + |
42 throw new InvalidObjectException("writeObject: object " + |
41 this.toString() + " has already " |
43 this.toString() + " has already " |
51 } |
53 } |
52 } |
54 } |
53 |
55 |
54 public class WriteObjectMemory { |
56 public class WriteObjectMemory { |
55 public static void main(String args[]) |
57 public static void main(String args[]) |
56 throws IOException, ClassNotFoundException |
58 throws IOException |
57 { |
59 { |
58 ObjectOutputStream out = |
60 ObjectOutputStream out = |
59 new ObjectOutputStream(new ByteArrayOutputStream(3000)); |
61 new ObjectOutputStream(new ByteArrayOutputStream(3000)); |
60 for (int i = 0; i < 1000; i++) { |
62 for (int i = 0; i < 1000; i++) { |
61 out.writeObject(new A()); |
63 out.writeObject(new A()); |
63 |
65 |
64 // Make sure that serialization subsystem does not |
66 // Make sure that serialization subsystem does not |
65 // allow writeObject to be called on any objects that |
67 // allow writeObject to be called on any objects that |
66 // have already been serialized. These objects should be |
68 // have already been serialized. These objects should be |
67 // written out by reference. |
69 // written out by reference. |
68 Iterator iter = A.writeObjectExtent.iterator(); |
70 Iterator<A> iter = A.writeObjectExtent.iterator(); |
69 while (iter.hasNext()) { |
71 while (iter.hasNext()) { |
70 out.writeObject(iter.next()); |
72 out.writeObject(iter.next()); |
71 } |
73 } |
72 |
74 |
73 out.close(); |
75 out.close(); |