1 /* 2 * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 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 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * 26 * @clean Write Read Foo 27 * @compile Write.java 28 * @run main Write 29 * @clean Write Read Foo 30 * @compile Read.java 31 * @run main Read 32 * 33 * @summary Verify that the ObjectOutputStream.PutField API works as 34 * advertised. 35 */ 36 37 import java.io.*; 38 39 class Foo implements Serializable { 40 private static final long serialVersionUID = 0L; 41 private static final ObjectStreamField[] serialPersistentFields = { 42 new ObjectStreamField("z", boolean.class), 43 new ObjectStreamField("b", byte.class), 44 new ObjectStreamField("c", char.class), 45 new ObjectStreamField("s", short.class), 46 new ObjectStreamField("i", int.class), 47 new ObjectStreamField("j", long.class), 48 new ObjectStreamField("f", float.class), 49 new ObjectStreamField("d", double.class), 50 new ObjectStreamField("str", String.class), 51 }; 52 writeObject(ObjectOutputStream out)53 private void writeObject(ObjectOutputStream out) throws IOException { 54 ObjectOutputStream.PutField fields = out.putFields(); 55 fields.put("z", true); 56 fields.put("b", (byte) 5); 57 fields.put("c", '5'); 58 fields.put("s", (short) 5); 59 fields.put("i", 5); 60 fields.put("j", 5l); 61 fields.put("f", 5.0f); 62 fields.put("d", 5.0); 63 fields.put("str", "5"); 64 out.writeFields(); 65 } 66 } 67 68 public class Write { main(String[] args)69 public static void main(String[] args) throws Exception { 70 ObjectOutputStream oout = 71 new ObjectOutputStream(new FileOutputStream("tmp.ser")); 72 oout.writeObject(new Foo()); 73 oout.close(); 74 } 75 } 76