KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > copyobject > JavaStreamObjectCopierImpl


1 /*
2  * @(#)JavaStreamObjectCopierImpl.java 1.2 04/07/27
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.copyobject ;
9
10 import java.util.logging.Level JavaDoc;
11 import java.util.logging.Logger JavaDoc;
12
13 import java.io.Serializable JavaDoc;
14 import java.rmi.Remote JavaDoc;
15 import java.rmi.MarshalException JavaDoc;
16
17 import java.io.InputStream JavaDoc ;
18 import java.io.OutputStream JavaDoc ;
19 import java.io.ByteArrayInputStream JavaDoc ;
20 import java.io.ByteArrayOutputStream JavaDoc ;
21 import java.io.ObjectInputStream JavaDoc ;
22 import java.io.ObjectOutputStream JavaDoc ;
23
24 import org.omg.CORBA.ORB JavaDoc ;
25
26 import com.sun.corba.se.spi.copyobject.ObjectCopier ;
27 import com.sun.corba.se.impl.util.Utility;
28
29 public class JavaStreamObjectCopierImpl implements ObjectCopier {
30
31     public JavaStreamObjectCopierImpl( ORB JavaDoc orb )
32     {
33     this.orb = orb ;
34     }
35
36     public Object JavaDoc copy(Object JavaDoc obj) {
37     if (obj instanceof Remote JavaDoc) {
38             // Yes, so make sure it is connected and converted
39
// to a stub (if needed)...
40
return Utility.autoConnect(obj,orb,true);
41         }
42
43     try {
44         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc( 10000 ) ;
45         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc( os ) ;
46         oos.writeObject( obj ) ;
47
48         byte[] arr = os.toByteArray() ;
49         InputStream JavaDoc is = new ByteArrayInputStream JavaDoc( arr ) ;
50         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc( is ) ;
51
52         return ois.readObject();
53     } catch (Exception JavaDoc exc) {
54         System.out.println( "Failed with exception:" + exc ) ;
55         return null ;
56     }
57     }
58
59     private ORB JavaDoc orb;
60 }
61
Popular Tags