1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2001 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 * 26 * ident "%Z%%M% %I% %E% SMI" 27 * 28 */ 29 30 /* 31 * @(#) BeanSerialization.java 1.5 - last change made 08/06/97 32 */ 33 34 package sunsoft.jws.visual.rt.base; 35 36 import java.io.*; 37 38 import sunsoft.jws.visual.rt.encoding.*; 39 40 public class BeanSerialization { 41 42 final static private boolean debug = false; 43 final static private boolean test = false; 44 serializeObject(Object obj)45 static public String serializeObject(Object obj) { 46 if (debug) System.out.println(/* NOI18N */"\nserializeObject: entered"); 47 48 /* 49 ** serialize the bean 50 */ 51 52 ByteArrayOutputStream byteOutStrm = new ByteArrayOutputStream(); 53 try { 54 ObjectOutputStream objOutStrm = new ObjectOutputStream(byteOutStrm); 55 objOutStrm.writeObject(obj); 56 objOutStrm.flush(); 57 } catch (NotSerializableException ex) { 58 // skip it since it is not serializable - not an err 59 if (debug) System.out.println(ex.toString()); 60 return null; 61 } catch (Exception ex) { 62 throw new VJException(Global.fmtMsg( 63 "sunsoft.jws.visual.rt.base.BeanSerialization.serExcpt", 64 obj, ex.toString())); 65 } 66 byte[] buf = byteOutStrm.toByteArray(); 67 68 if (debug) System.out.println(/* NOI18N */ 69 "serializeObject: serialized"); 70 71 /* 72 ** encode the string 73 */ 74 75 UCEncoder encoder = new UCEncoder(); 76 String str = encoder.encodeBuffer(buf); 77 78 if (debug) System.out.println(/* NOI18N */"serializeObject: encoded"); 79 80 return str; 81 } 82 deserializeObject(String value, String objName)83 static public Object deserializeObject(String value, String objName) { 84 if (debug) System.out.println(/* NOI18N */ 85 "\ndeserializeObject: entered"); 86 87 if (value == null || value.length() == 0) { 88 if (debug) { 89 /* JSTYLED */ 90 System.out.println(/* NOI18N */"deserializeObject: value is null"); 91 } 92 return null; 93 } 94 95 /* 96 ** decode the string first 97 */ 98 99 byte buf[] = null; 100 try { 101 UCDecoder decoder = new UCDecoder(); 102 buf = decoder.decodeBuffer(value); 103 } catch (Exception ex) { 104 if (debug) System.out.println(ex.toString()); 105 throw new VJException(Global.fmtMsg( 106 "sunsoft.jws.visual.rt.base.BeanSerialization.decoderExcpt", 107 objName)); 108 } 109 if (debug) System.out.println(/* NOI18N */"deserializeObject: decoded"); 110 111 /* 112 ** deserialize the object 113 */ 114 115 Object newBody = null; 116 try { 117 ByteArrayInputStream byteInStrm = new ByteArrayInputStream(buf); 118 ObjectInputStream objInStrm = new ObjectInputStream(byteInStrm); 119 newBody = objInStrm.readObject(); 120 } catch (Exception ex) { 121 // ClassNotFoundException from readObject 122 // OptionalDataException from readObject 123 // StreamCorruptedException from ObjectInputStream 124 // IOException from ObjectInputStream 125 String errMsg = Global.fmtMsg( 126 "sunsoft.jws.visual.rt.base.BeanSerialization.deserExcpt", 127 objName, ex.toString()); 128 if (java.beans.Beans.isDesignTime()) { 129 DesignerAccess.reportInstantiationError(errMsg); 130 } 131 else 132 { 133 throw new VJException(errMsg); 134 } 135 } 136 if (debug) { 137 System.out.println(/* NOI18N */"deserializeObject: deserialized"); 138 } 139 return newBody; 140 } 141 } 142