001: /*
002: * Copyright 2000-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.corba.se.impl.ior;
027:
028: import java.util.List;
029: import java.util.LinkedList;
030: import java.util.Iterator;
031:
032: import org.omg.IOP.TAG_INTERNET_IOP;
033:
034: import org.omg.CORBA_2_3.portable.OutputStream;
035: import org.omg.CORBA_2_3.portable.InputStream;
036:
037: import com.sun.corba.se.spi.ior.TaggedComponent;
038: import com.sun.corba.se.spi.ior.Identifiable;
039: import com.sun.corba.se.spi.ior.IdentifiableFactoryFinder;
040: import com.sun.corba.se.spi.ior.WriteContents;
041:
042: import com.sun.corba.se.spi.orb.ORB;
043:
044: import com.sun.corba.se.impl.ior.FreezableList;
045:
046: import com.sun.corba.se.impl.encoding.CDROutputStream;
047: import com.sun.corba.se.impl.encoding.EncapsOutputStream;
048: import com.sun.corba.se.impl.encoding.EncapsInputStream;
049:
050: /**
051: * This static utility class contains various utility methods for reading and
052: * writing CDR encapsulations.
053: *
054: * @author Ken Cavanaugh
055: */
056: public class EncapsulationUtility {
057: private EncapsulationUtility() {
058: }
059:
060: /** Read the count from is, then read count Identifiables from
061: * is using the factory. Add each constructed Identifiable to container.
062: */
063: public static void readIdentifiableSequence(List container,
064: IdentifiableFactoryFinder finder, InputStream istr) {
065: int count = istr.read_long();
066: for (int ctr = 0; ctr < count; ctr++) {
067: int id = istr.read_long();
068: Identifiable obj = finder.create(id, istr);
069: container.add(obj);
070: }
071: }
072:
073: /** Write all Identifiables that we contain to os. The total
074: * length must be written before this method is called.
075: */
076: public static void writeIdentifiableSequence(List container,
077: OutputStream os) {
078: os.write_long(container.size());
079: Iterator iter = container.iterator();
080: while (iter.hasNext()) {
081: Identifiable obj = (Identifiable) (iter.next());
082: os.write_long(obj.getId());
083: obj.write(os);
084: }
085: }
086:
087: /** Helper method that is used to extract data from an output
088: * stream and write the data to another output stream. Defined
089: * as static so that it can be used in another class.
090: */
091: static public void writeOutputStream(OutputStream dataStream,
092: OutputStream os) {
093: byte[] data = ((CDROutputStream) dataStream).toByteArray();
094: os.write_long(data.length);
095: os.write_octet_array(data, 0, data.length);
096: }
097:
098: /** Helper method to read the octet array from is, deencapsulate it,
099: * and return
100: * as another InputStream. This must be called inside the
101: * constructor of a derived class to obtain the correct stream
102: * for unmarshalling data.
103: */
104: static public InputStream getEncapsulationStream(InputStream is) {
105: byte[] data = readOctets(is);
106: EncapsInputStream result = new EncapsInputStream(is.orb(),
107: data, data.length);
108: result.consumeEndian();
109: return result;
110: }
111:
112: /** Helper method that reads an octet array from an input stream.
113: * Defined as static here so that it can be used in another class.
114: */
115: static public byte[] readOctets(InputStream is) {
116: int len = is.read_ulong();
117: byte[] data = new byte[len];
118: is.read_octet_array(data, 0, len);
119: return data;
120: }
121:
122: static public void writeEncapsulation(WriteContents obj,
123: OutputStream os) {
124: EncapsOutputStream out = new EncapsOutputStream((ORB) os.orb());
125:
126: out.putEndian();
127:
128: obj.writeContents(out);
129:
130: writeOutputStream(out, os);
131: }
132: }
|