KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > ior > EncapsulationUtility


1 /*
2  * @(#)EncapsulationUtility.java 1.23 03/12/19
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.ior;
9
10 import java.util.List JavaDoc;
11 import java.util.LinkedList JavaDoc;
12 import java.util.Iterator JavaDoc;
13
14 import org.omg.IOP.TAG_INTERNET_IOP JavaDoc ;
15
16 import org.omg.CORBA_2_3.portable.OutputStream JavaDoc ;
17 import org.omg.CORBA_2_3.portable.InputStream JavaDoc ;
18
19 import com.sun.corba.se.spi.ior.TaggedComponent ;
20 import com.sun.corba.se.spi.ior.Identifiable ;
21 import com.sun.corba.se.spi.ior.IdentifiableFactoryFinder ;
22 import com.sun.corba.se.spi.ior.WriteContents ;
23
24 import com.sun.corba.se.spi.orb.ORB ;
25
26 import com.sun.corba.se.impl.ior.FreezableList ;
27
28 import com.sun.corba.se.impl.encoding.CDROutputStream ;
29 import com.sun.corba.se.impl.encoding.EncapsOutputStream ;
30 import com.sun.corba.se.impl.encoding.EncapsInputStream ;
31
32 /**
33  * This static utility class contains various utility methods for reading and
34  * writing CDR encapsulations.
35  *
36  * @author Ken Cavanaugh
37  */

38 public class EncapsulationUtility
39 {
40     private EncapsulationUtility()
41     {
42     }
43
44     /** Read the count from is, then read count Identifiables from
45      * is using the factory. Add each constructed Identifiable to container.
46      */

47     public static void readIdentifiableSequence( List JavaDoc container,
48     IdentifiableFactoryFinder finder, InputStream JavaDoc istr)
49     {
50     int count = istr.read_long() ;
51     for (int ctr = 0; ctr<count; ctr++) {
52         int id = istr.read_long() ;
53         Identifiable obj = finder.create( id, istr ) ;
54         container.add( obj ) ;
55     }
56     }
57
58     /** Write all Identifiables that we contain to os. The total
59      * length must be written before this method is called.
60      */

61     public static void writeIdentifiableSequence( List JavaDoc container, OutputStream JavaDoc os)
62     {
63     os.write_long( container.size() ) ;
64     Iterator JavaDoc iter = container.iterator() ;
65     while (iter.hasNext()) {
66         Identifiable obj = (Identifiable)( iter.next() ) ;
67         os.write_long( obj.getId() ) ;
68         obj.write( os ) ;
69     }
70     }
71
72     /** Helper method that is used to extract data from an output
73     * stream and write the data to another output stream. Defined
74     * as static so that it can be used in another class.
75     */

76     static public void writeOutputStream( OutputStream JavaDoc dataStream,
77     OutputStream JavaDoc os )
78     {
79     byte[] data = ((CDROutputStream)dataStream).toByteArray() ;
80     os.write_long( data.length ) ;
81     os.write_octet_array( data, 0, data.length ) ;
82     }
83
84     /** Helper method to read the octet array from is, deencapsulate it,
85     * and return
86     * as another InputStream. This must be called inside the
87     * constructor of a derived class to obtain the correct stream
88     * for unmarshalling data.
89     */

90     static public InputStream JavaDoc getEncapsulationStream( InputStream JavaDoc is )
91     {
92     byte[] data = readOctets( is ) ;
93     EncapsInputStream result = new EncapsInputStream( is.orb(), data,
94         data.length ) ;
95     result.consumeEndian() ;
96     return result ;
97     }
98
99     /** Helper method that reads an octet array from an input stream.
100     * Defined as static here so that it can be used in another class.
101     */

102     static public byte[] readOctets( InputStream JavaDoc is )
103     {
104     int len = is.read_ulong() ;
105     byte[] data = new byte[len] ;
106     is.read_octet_array( data, 0, len ) ;
107     return data ;
108     }
109
110     static public void writeEncapsulation( WriteContents obj,
111     OutputStream JavaDoc os )
112     {
113     EncapsOutputStream out = new EncapsOutputStream( (ORB)os.orb() ) ;
114
115     out.putEndian() ;
116
117     obj.writeContents( out ) ;
118
119     writeOutputStream( out, os ) ;
120     }
121 }
122
Popular Tags