001/* Demonstrate using Serializable classes 002 003 Copyright (c) 1998-2014 The Regents of the University of California. 004 All rights reserved. 005 Permission is hereby granted, without written agreement and without 006 license or royalty fees, to use, copy, modify, and distribute this 007 software and its documentation for any purpose, provided that the above 008 copyright notice and the following two paragraphs appear in all copies 009 of this software. 010 011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 015 SUCH DAMAGE. 016 017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 022 ENHANCEMENTS, OR MODIFICATIONS. 023 024 PT_COPYRIGHT_VERSION_2 025 COPYRIGHTENDKEY 026 */ 027package ptolemy.kernel.test; 028 029import java.io.FileInputStream; 030import java.io.FileOutputStream; 031import java.io.IOException; 032import java.io.ObjectInputStream; 033import java.io.ObjectOutput; 034import java.io.ObjectOutputStream; 035 036import ptolemy.kernel.util.IllegalActionException; 037import ptolemy.kernel.util.NameDuplicationException; 038 039/////////////////////////////////////////////////////////////////// 040//// TestSerializable 041 042/** 043 This class constructs a system from the Ptolemy II design document, Figure 8, 044 saves it to a file and then reloads it. 045 @author Christopher Hylands 046 @version $Id$ 047 @since Ptolemy II 0.2 048 @Pt.ProposedRating Red 049 @Pt.AcceptedRating Red 050 */ 051public class TestSerializable { 052 /////////////////////////////////////////////////////////////////// 053 //// public methods //// 054 055 /** Create an Example System, then print it out. 056 * @exception NameDuplicationException if the example system cannot 057 * be built because of a duplicate name 058 * @exception IllegalActionException if the example system cannot 059 * be built. 060 */ 061 public static void main(String[] args) 062 throws NameDuplicationException, IllegalActionException { 063 ExampleSystem exampleSystem = new ExampleSystem(); 064 String filename = "TestSerializable.data"; 065 066 if (args.length > 0 && args[0].equals("write")) { 067 FileOutputStream f = null; 068 ObjectOutput s = null; 069 070 try { 071 // Write the system out. 072 f = new FileOutputStream(filename); 073 s = new ObjectOutputStream(f); 074 s.writeObject(exampleSystem); 075 } catch (IOException e) { 076 System.err.println("Exception while writing: " + e); 077 e.printStackTrace(); 078 } finally { 079 if (f != null) { 080 try { 081 f.close(); 082 } catch (Throwable throwable) { 083 System.out.println("Ignoring failure to close stream " 084 + "on '" + filename + "'"); 085 throwable.printStackTrace(); 086 } 087 } 088 089 if (s != null) { 090 try { 091 s.close(); 092 } catch (Throwable throwable) { 093 System.out.println("Ignoring failure to close stream " 094 + "on '" + filename + "'"); 095 throwable.printStackTrace(); 096 } 097 } 098 } 099 100 System.out.println("Wrote to " + filename); 101 } else { 102 FileInputStream f = null; 103 ObjectInputStream s = null; 104 105 try { 106 // Read the system in 107 f = new FileInputStream(filename); 108 s = new ObjectInputStream(f); 109 110 ExampleSystem newExampleSystem = (ExampleSystem) s.readObject(); 111 String newDescription = newExampleSystem.toString(); 112 String oldDescription = exampleSystem.toString(); 113 114 if (oldDescription.equals(newDescription)) { 115 System.out.println("OK: Description read in from " 116 + filename + " is the same as the original\n"); 117 } else { 118 System.out.println("ERROR\nDescription read in from " 119 + filename + "\n" + newDescription + "\n" 120 + "is NOT the same as the original:\n" 121 + oldDescription); 122 System.exit(1); 123 } 124 } catch (IOException e) { 125 System.err.println("IOException while reading: " + e); 126 e.printStackTrace(); 127 } catch (ClassNotFoundException e) { 128 System.err 129 .println("ClassNotFoundException while reading: " + e); 130 } finally { 131 if (f != null) { 132 try { 133 f.close(); 134 } catch (Throwable throwable) { 135 System.out.println("Ignoring failure to close stream " 136 + "on '" + filename + "'"); 137 throwable.printStackTrace(); 138 } 139 } 140 141 if (s != null) { 142 try { 143 s.close(); 144 } catch (Throwable throwable) { 145 System.out.println("Ignoring failure to close stream " 146 + "on '" + filename + "'"); 147 throwable.printStackTrace(); 148 } 149 } 150 } 151 } 152 } 153}