typecodeimpl.java
来自「java jdk 1.4的源码」· Java 代码 · 共 2,106 行 · 第 1/5 页
JAVA
2,106 行
/* * @(#)TypeCodeImpl.java 1.91 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.corba.se.internal.corba;import org.omg.CORBA.*;import org.omg.CORBA.TypeCodePackage.*;import org.omg.CORBA_2_3.portable.InputStream;import org.omg.CORBA_2_3.portable.OutputStream;import com.sun.corba.se.internal.core.*;import com.sun.corba.se.internal.iiop.CDRInputStream;import com.sun.corba.se.internal.iiop.CDROutputStream;import java.util.HashMap;import java.util.Map;import java.util.Iterator;import java.util.List;import java.util.Collections;import java.util.ArrayList;import java.io.IOException;import java.io.PrintStream;import java.io.ByteArrayOutputStream;import java.math.BigDecimal;import java.math.BigInteger;class WrapperInputStream extends org.omg.CORBA_2_3.portable.InputStream implements TypeCodeReader{ private CDRInputStream stream; private Map typeMap = null; private int startPos = 0; public WrapperInputStream(CDRInputStream s) { super(); stream = s; startPos = stream.getPosition(); } public int read() throws IOException { return stream.read(); } public int read(byte b[]) throws IOException { return stream.read(b); } public int read(byte b[], int off, int len) throws IOException { return stream.read(b, off, len); } public long skip(long n) throws IOException { return stream.skip(n); } public int available() throws IOException { return stream.available(); } public void close() throws IOException { stream.close(); } public void mark(int readlimit) { stream.mark(readlimit); } public void reset() { stream.reset(); } public boolean markSupported() { return stream.markSupported(); } public int getPosition() { return stream.getPosition(); } public void consumeEndian() { stream.consumeEndian(); } public boolean read_boolean() { return stream.read_boolean(); } public char read_char() { return stream.read_char(); } public char read_wchar() { return stream.read_wchar(); } public byte read_octet() { return stream.read_octet(); } public short read_short() { return stream.read_short(); } public short read_ushort() { return stream.read_ushort(); } public int read_long() { return stream.read_long(); } public int read_ulong() { return stream.read_ulong(); } public long read_longlong() { return stream.read_longlong(); } public long read_ulonglong() { return stream.read_ulonglong(); } public float read_float() { return stream.read_float(); } public double read_double() { return stream.read_double(); } public String read_string() { return stream.read_string(); } public String read_wstring() { return stream.read_wstring(); } public void read_boolean_array(boolean[] value, int offset, int length) { stream.read_boolean_array(value, offset, length); } public void read_char_array(char[] value, int offset, int length) { stream.read_char_array(value, offset, length); } public void read_wchar_array(char[] value, int offset, int length) { stream.read_wchar_array(value, offset, length); } public void read_octet_array(byte[] value, int offset, int length) { stream.read_octet_array(value, offset, length); } public void read_short_array(short[] value, int offset, int length) { stream.read_short_array(value, offset, length); } public void read_ushort_array(short[] value, int offset, int length) { stream.read_ushort_array(value, offset, length); } public void read_long_array(int[] value, int offset, int length) { stream.read_long_array(value, offset, length); } public void read_ulong_array(int[] value, int offset, int length) { stream.read_ulong_array(value, offset, length); } public void read_longlong_array(long[] value, int offset, int length) { stream.read_longlong_array(value, offset, length); } public void read_ulonglong_array(long[] value, int offset, int length) { stream.read_ulonglong_array(value, offset, length); } public void read_float_array(float[] value, int offset, int length) { stream.read_float_array(value, offset, length); } public void read_double_array(double[] value, int offset, int length) { stream.read_double_array(value, offset, length); } public org.omg.CORBA.Object read_Object() { return stream.read_Object(); } public java.io.Serializable read_value() {return stream.read_value();} public TypeCode read_TypeCode() { return stream.read_TypeCode(); } public Any read_any() { return stream.read_any(); } public Principal read_Principal() { return stream.read_Principal(); } public java.math.BigDecimal read_fixed() { return stream.read_fixed(); } public org.omg.CORBA.Context read_Context() { return stream.read_Context(); } public org.omg.CORBA.ORB orb() { return stream.orb(); } public void addTypeCodeAtPosition(TypeCodeImpl tc, int position) { if (typeMap == null) { //if (TypeCodeImpl.debug) System.out.println("Creating typeMap"); typeMap = new HashMap(16); } //if (TypeCodeImpl.debug) System.out.println(this + " adding tc " + tc + " at position " + position); typeMap.put(new Integer(position), tc); } public TypeCodeImpl getTypeCodeAtPosition(int position) { if (typeMap == null) return null; //if (TypeCodeImpl.debug) System.out.println("Getting tc " + (TypeCodeImpl)typeMap.get(new Integer(position)) + //" at position " + position); return (TypeCodeImpl)typeMap.get(new Integer(position)); } public void setEnclosingInputStream(InputStream enclosure) { // WrapperInputStream has no enclosure } public TypeCodeReader getTopLevelStream() { // WrapperInputStream has no enclosure return this; } public int getTopLevelPosition() { //if (TypeCodeImpl.debug) System.out.println("WrapperInputStream.getTopLevelPosition " + //"returning getPosition " + getPosition() + " - startPos " + startPos + //" = " + (getPosition() - startPos)); return getPosition() - startPos; } public void performORBVersionSpecificInit() { // This is never actually called on a WrapperInputStream, but // exists to satisfy the interface requirement. stream.performORBVersionSpecificInit(); } public void resetCodeSetConverters() { stream.resetCodeSetConverters(); } //public void printBuffer() { stream.printBuffer(); } public void printTypeMap() { System.out.println("typeMap = {"); List sortedKeys = new ArrayList(typeMap.keySet()); Collections.sort(sortedKeys); Iterator i = sortedKeys.iterator(); while (i.hasNext()) { Integer pos = (Integer)i.next(); TypeCodeImpl tci = (TypeCodeImpl)typeMap.get(pos); System.out.println(" key = " + pos.intValue() + ", value = " + tci.description()); } System.out.println("}"); }}interface TypeCodeReader extends MarshalInputStream { public void addTypeCodeAtPosition(TypeCodeImpl tc, int position); public TypeCodeImpl getTypeCodeAtPosition(int position); public void setEnclosingInputStream(InputStream enclosure); public TypeCodeReader getTopLevelStream(); public int getTopLevelPosition(); // for debugging //public void printBuffer(); public int getPosition(); public void printTypeMap();}class TypeCodeInputStream extends EncapsInputStream implements TypeCodeReader{ private Map typeMap = null; private InputStream enclosure = null; private boolean isEncapsulation = false; public TypeCodeInputStream(org.omg.CORBA.ORB orb, byte[] data, int size) { super(orb, data, size); } public TypeCodeInputStream(org.omg.CORBA.ORB orb, byte[] data, int size, boolean littleEndian) { super(orb, data, size, littleEndian); } public void addTypeCodeAtPosition(TypeCodeImpl tc, int position) { if (typeMap == null) { //if (TypeCodeImpl.debug) System.out.println("Creating typeMap"); typeMap = new HashMap(16); } //if (TypeCodeImpl.debug) System.out.println(this + " adding tc " + tc + " at position " + position); typeMap.put(new Integer(position), tc); } public TypeCodeImpl getTypeCodeAtPosition(int position) { if (typeMap == null) return null; //if (TypeCodeImpl.debug) { //System.out.println("Getting tc " + (TypeCode)typeMap.get(new Integer(position)) + //" at position " + position); //} return (TypeCodeImpl)typeMap.get(new Integer(position)); } public void setEnclosingInputStream(InputStream enclosure) { this.enclosure = enclosure; } public TypeCodeReader getTopLevelStream() { if (enclosure == null) return this; if (enclosure instanceof TypeCodeReader) return ((TypeCodeReader)enclosure).getTopLevelStream(); return this; } public int getTopLevelPosition() { if (enclosure != null && enclosure instanceof TypeCodeReader) { // The enclosed stream has to consider if the enclosing stream // had to read the enclosed stream completely when creating it. // This is why the size of the enclosed stream needs to be substracted. int topPos = ((TypeCodeReader)enclosure).getTopLevelPosition(); // Substract getBufferLength from the parents pos because it read this stream // from its own when creating it int pos = topPos - getBufferLength() + getPosition(); //if (TypeCodeImpl.debug) { //System.out.println("TypeCodeInputStream.getTopLevelPosition using getTopLevelPosition " + topPos + //(isEncapsulation ? " - encaps length 4" : "") + //" - getBufferLength() " + getBufferLength() + //" + getPosition() " + getPosition() + " = " + pos); //} return pos; } //if (TypeCodeImpl.debug) { //System.out.println("TypeCodeInputStream.getTopLevelPosition returning getPosition() = " + //getPosition() + " because enclosure is " + enclosure); //} return getPosition(); } public static TypeCodeInputStream readEncapsulation(InputStream is, org.omg.CORBA.ORB _orb) { // _REVISIT_ Would be nice if we didn't have to copy the buffer! TypeCodeInputStream encap; int encapLength = is.read_long(); // read off part of the buffer corresponding to the encapsulation byte[] encapBuffer = new byte[encapLength]; is.read_octet_array(encapBuffer, 0, encapBuffer.length); // create an encapsulation using the marshal buffer if (is instanceof CDRInputStream) { encap = new TypeCodeInputStream(_orb, encapBuffer, encapBuffer.length, ((CDRInputStream)is).isLittleEndian()); } else { encap = new TypeCodeInputStream(_orb, encapBuffer, encapBuffer.length); } encap.setEnclosingInputStream(is); encap.makeEncapsulation(); //if (TypeCodeImpl.debug) { //System.out.println("Created TypeCodeInputStream " + encap + " with parent " + is); //encap.printBuffer(); //} return encap; } protected void makeEncapsulation() { // first entry in an encapsulation is the endianess consumeEndian(); isEncapsulation = true; } public void printTypeMap() { System.out.println("typeMap = {"); Iterator i = typeMap.keySet().iterator(); while (i.hasNext()) { Integer pos = (Integer)i.next(); TypeCodeImpl tci = (TypeCodeImpl)typeMap.get(pos); System.out.println(" key = " + pos.intValue() + ", value = " + tci.description()); } System.out.println("}"); }}final class TypeCodeOutputStream extends EncapsOutputStream { private OutputStream enclosure = null; private Map typeMap = null; private boolean isEncapsulation = false; public TypeCodeOutputStream(org.omg.CORBA.ORB orb) { super(orb, false); } public TypeCodeOutputStream(org.omg.CORBA.ORB orb, boolean littleEndian) { super(orb, littleEndian); } public org.omg.CORBA.portable.InputStream create_input_stream() { //return new TypeCodeInputStream(orb(), getByteBuffer(), getIndex(), isLittleEndian()); TypeCodeInputStream tcis = new TypeCodeInputStream(orb(), getByteBuffer(), getIndex(), isLittleEndian()); //if (TypeCodeImpl.debug) { //System.out.println("Created TypeCodeInputStream " + tcis + " with no parent"); //tcis.printBuffer(); //} return tcis; } public void setEnclosingOutputStream(OutputStream enclosure) { this.enclosure = enclosure; } /* public boolean isEncapsulatedIn(TypeCodeOutputStream outerEnclosure) { if (outerEnclosure == this) return true; if (enclosure == null) return false; if (enclosure instanceof TypeCodeOutputStream) return ((TypeCodeOutputStream)enclosure).isEncapsulatedIn(outerEnclosure); // Last chance! Recursion ends with first non TypeCodeOutputStream. return (enclosure == outerEnclosure); } */ public TypeCodeOutputStream getTopLevelStream() { if (enclosure == null) return this; if (enclosure instanceof TypeCodeOutputStream) return ((TypeCodeOutputStream)enclosure).getTopLevelStream(); return this; } public int getTopLevelPosition() { if (enclosure != null && enclosure instanceof TypeCodeOutputStream) { int pos = ((TypeCodeOutputStream)enclosure).getTopLevelPosition() + getPosition(); // Add four bytes for the encaps length, not another 4 for the byte order // which is included in getPosition(). if (isEncapsulation) pos += 4; //if (TypeCodeImpl.debug) { //System.out.println("TypeCodeOutputStream.getTopLevelPosition using getTopLevelPosition " + //((TypeCodeOutputStream)enclosure).getTopLevelPosition() + //" + getPosition() " + getPosition() + //(isEncapsulation ? " + encaps length 4" : "") + //" = " + pos); //} return pos; } //if (TypeCodeImpl.debug) { //System.out.println("TypeCodeOutputStream.getTopLevelPosition returning getPosition() = " + //getPosition() + ", enclosure is " + enclosure); //} return getPosition(); } public void addIDAtPosition(String id, int position) { if (typeMap == null) typeMap = new HashMap(16); //if (TypeCodeImpl.debug) System.out.println(this + " adding id " + id + " at position " + position); typeMap.put(id, new Integer(position)); } public int getPositionForID(String id) { if (typeMap == null) throw new MARSHAL("Referenced type of indirect type not marshaled!"); //if (TypeCodeImpl.debug) System.out.println("Getting position " + ((Integer)typeMap.get(id)).intValue() + //" for id " + id); return ((Integer)typeMap.get(id)).intValue(); } public void writeRawBuffer(org.omg.CORBA.portable.OutputStream s, int firstLong) { // Writes this streams buffer to the given OutputStream // without byte order flag and length as is the case for encapsulations. // Make sure to align s to 4 byte boundaries. // Unfortunately we can't do just this: // s.alignAndReserve(4, 4); // So we have to take the first four bytes given in firstLong and write them // with a call to write_long which will trigger the alignment. // Then write the rest of the byte array. //if (TypeCodeImpl.debug) { //System.out.println(this + ".writeRawBuffer(" + s + ", " + firstLong + ")"); //if (s instanceof CDROutputStream) { //System.out.println("Parent position before writing kind = " + ((CDROutputStream)s).getIndex()); //} //} s.write_long(firstLong); //if (TypeCodeImpl.debug) { //if (s instanceof CDROutputStream) { //System.out.println("Parent position after writing kind = " + ((CDROutputStream)s).getIndex()); //} //} s.write_octet_array(getByteBuffer(), 4, getIndex() - 4); //if (TypeCodeImpl.debug) { //if (s instanceof CDROutputStream) { //System.out.println("Parent position after writing all " + getIndex() + " bytes = " + ((CDROutputStream)s).getIndex()); //} //} } public TypeCodeOutputStream createEncapsulation(org.omg.CORBA.ORB _orb) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?