EncapsulationUtility.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 2000, 2013, 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.corba.se.impl.ior;
27
28import java.util.List;
29import java.util.LinkedList;
30import java.util.Iterator;
31
32import org.omg.IOP.TAG_INTERNET_IOP ;
33
34import org.omg.CORBA_2_3.portable.OutputStream ;
35import org.omg.CORBA_2_3.portable.InputStream ;
36
37import com.sun.corba.se.spi.ior.TaggedComponent ;
38import com.sun.corba.se.spi.ior.Identifiable ;
39import com.sun.corba.se.spi.ior.IdentifiableFactoryFinder ;
40import com.sun.corba.se.spi.ior.WriteContents ;
41
42import com.sun.corba.se.spi.orb.ORB ;
43
44import com.sun.corba.se.impl.ior.FreezableList ;
45
46import com.sun.corba.se.impl.encoding.CDROutputStream ;
47import com.sun.corba.se.impl.encoding.EncapsOutputStream ;
48import com.sun.corba.se.impl.encoding.EncapsInputStream ;
49
50import sun.corba.EncapsInputStreamFactory;
51
52/**
53 * This static utility class contains various utility methods for reading and
54 * writing CDR encapsulations.
55 *
56 * @author Ken Cavanaugh
57 */
58public class EncapsulationUtility
59{
60    private EncapsulationUtility()
61    {
62    }
63
64    /** Read the count from is, then read count Identifiables from
65     * is using the factory.  Add each constructed Identifiable to container.
66     */
67    public static void readIdentifiableSequence( List container,
68        IdentifiableFactoryFinder finder, InputStream istr)
69    {
70        int count = istr.read_long() ;
71        for (int ctr = 0; ctr<count; ctr++) {
72            int id = istr.read_long() ;
73            Identifiable obj = finder.create( id, istr ) ;
74            container.add( obj ) ;
75        }
76    }
77
78    /** Write all Identifiables that we contain to os.  The total
79     * length must be written before this method is called.
80     */
81    public static  void writeIdentifiableSequence( List container, OutputStream os)
82    {
83        os.write_long( container.size() ) ;
84        Iterator iter = container.iterator() ;
85        while (iter.hasNext()) {
86            Identifiable obj = (Identifiable)( iter.next() ) ;
87            os.write_long( obj.getId() ) ;
88            obj.write( os ) ;
89        }
90    }
91
92    /** Helper method that is used to extract data from an output
93    * stream and write the data to another output stream.  Defined
94    * as static so that it can be used in another class.
95    */
96    static public void writeOutputStream( OutputStream dataStream,
97        OutputStream os )
98    {
99        byte[] data = ((CDROutputStream)dataStream).toByteArray() ;
100        os.write_long( data.length ) ;
101        os.write_octet_array( data, 0, data.length ) ;
102    }
103
104    /** Helper method to read the octet array from is, deencapsulate it,
105    * and return
106    * as another InputStream.  This must be called inside the
107    * constructor of a derived class to obtain the correct stream
108    * for unmarshalling data.
109    */
110    static public InputStream getEncapsulationStream( InputStream is )
111    {
112        byte[] data = readOctets( is ) ;
113        EncapsInputStream result = EncapsInputStreamFactory.newEncapsInputStream( is.orb(), data,
114                data.length ) ;
115        result.consumeEndian() ;
116        return result ;
117    }
118
119    /** Helper method that reads an octet array from an input stream.
120    * Defined as static here so that it can be used in another class.
121    */
122    static public byte[] readOctets( InputStream is )
123    {
124        int len = is.read_ulong() ;
125        byte[] data = new byte[len] ;
126        is.read_octet_array( data, 0, len ) ;
127        return data ;
128    }
129
130    static public void writeEncapsulation( WriteContents obj,
131        OutputStream os )
132    {
133        EncapsOutputStream out =
134            sun.corba.OutputStreamFactory.newEncapsOutputStream((ORB)os.orb());
135
136        out.putEndian() ;
137
138        obj.writeContents( out ) ;
139
140        writeOutputStream( out, os ) ;
141    }
142}
143