cdroutputstream_1_0.java
来自「java jdk 1.4的源码」· Java 代码 · 共 2,001 行 · 第 1/4 页
JAVA
2,001 行
/* * @(#)CDROutputStream_1_0.java 1.107 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. *//* * Licensed Materials - Property of IBM * RMI-IIOP v1.0 * Copyright IBM Corp. 1998 1999 All Rights Reserved * * US Government Users Restricted Rights - Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */package com.sun.corba.se.internal.iiop;import java.io.ByteArrayOutputStream;import java.io.IOException;import org.omg.CORBA.SystemException;import org.omg.CORBA.CompletionStatus;import org.omg.CORBA.INTERNAL;import org.omg.CORBA.MARSHAL;import org.omg.CORBA.DATA_CONVERSION;import org.omg.CORBA.BAD_INV_ORDER;import org.omg.CORBA.BAD_OPERATION;import org.omg.CORBA.BAD_PARAM;import org.omg.CORBA.Object;import org.omg.CORBA.Principal;import org.omg.CORBA.TypeCode;import org.omg.CORBA.Any;import org.omg.CORBA.portable.ObjectImpl;import org.omg.CORBA.portable.OutputStream;import org.omg.CORBA.portable.ValueBase;import org.omg.CORBA.NO_IMPLEMENT;import org.omg.CORBA.CompletionStatus;import org.omg.CORBA.portable.IDLEntity;import org.omg.CORBA.VM_CUSTOM;import org.omg.CORBA.VM_TRUNCATABLE;import org.omg.CORBA.VM_NONE;import java.rmi.Remote;import java.io.Serializable;import java.lang.reflect.Method;import java.lang.reflect.InvocationTargetException;import java.io.ByteArrayOutputStream;import java.io.ObjectOutputStream;import com.sun.corba.se.internal.orbutil.MinorCodes;import com.sun.corba.se.internal.core.MarshalOutputStream;import com.sun.corba.se.internal.core.IOR;import com.sun.corba.se.internal.core.ClientSubcontract;import com.sun.corba.se.internal.corba.TypeCodeImpl;import com.sun.org.omg.CORBA.portable.ValueHelper;import org.omg.CORBA.portable.CustomValue;import org.omg.CORBA.portable.StreamableValue;import org.omg.CORBA.CustomMarshal;import org.omg.CORBA.DataOutputStream;import org.omg.CORBA.portable.BoxedValueHelper;import org.omg.CORBA.TypeCodePackage.BadKind;import java.io.IOException;import java.util.Hashtable;import java.util.Stack;import com.sun.corba.se.internal.orbutil.CacheTable;import com.sun.corba.se.internal.orbutil.ORBUtility;import com.sun.corba.se.internal.orbutil.RepositoryIdStrings;import com.sun.corba.se.internal.orbutil.RepositoryIdUtility;import com.sun.corba.se.internal.orbutil.RepositoryIdFactory;import com.sun.corba.se.internal.core.ORBVersionImpl;import com.sun.corba.se.internal.core.ORBVersion;import com.sun.corba.se.internal.core.GIOPVersion;import com.sun.corba.se.internal.util.Utility;import com.sun.corba.se.internal.iiop.ByteBufferWithInfo;import javax.rmi.CORBA.Util;import java.math.BigDecimal;import javax.rmi.CORBA.ValueHandler;import com.sun.corba.se.internal.core.CodeSetConversion;import java.security.AccessController;import java.security.PrivilegedExceptionAction;import java.security.PrivilegedActionException;public class CDROutputStream_1_0 extends CDROutputStreamBase{ private static final int INDIRECTION_TAG = 0xffffffff; protected boolean littleEndian; protected BufferManagerWrite bufferManagerWrite; ByteBufferWithInfo bbwi; protected com.sun.corba.se.internal.corba.ORB orb; protected boolean debug = false; protected int blockSizeIndex = -1; protected int blockSizePosition = 0; private static final int DEFAULT_BUFFER_SIZE = 1024; private static final String kWriteMethod = "write"; // Codebase cache private CacheTable codebaseCache = null; // Value cache private CacheTable valueCache = null; // Repository ID cache private CacheTable repositoryIdCache = null; // Write end flag private int end_flag = 0; // Beginning with the resolution to interop issue 4328, // only enclosing chunked valuetypes are taken into account // when computing the nesting level. However, we still need // the old computation around for interoperability with our // older ORBs. private int chunkedValueNestingLevel = 0; // Nesting custom marshaling - used to tell whether we can skip chunking private boolean mustChunk = false; // In block marker protected boolean inBlock = false; // Last end tag position private int end_flag_position = 0; private int end_flag_index = 0; // ValueHandler private ValueHandler valueHandler = null; // Repository ID handlers private RepositoryIdUtility repIdUtil; private RepositoryIdStrings repIdStrs; // Code set converters (created when first needed) private CodeSetConversion.CTBConverter charConverter; private CodeSetConversion.CTBConverter wcharConverter; protected void init(org.omg.CORBA.ORB orb, boolean littleEndian, int size, BufferManagerWrite bufferManager) { this.orb = (com.sun.corba.se.internal.corba.ORB)orb; if (this.orb != null) debug = this.orb.transportDebugFlag; this.littleEndian = littleEndian; this.bufferManagerWrite = bufferManager; this.bbwi = bufferManagerWrite.getInitialBuffer(size); createRepositoryIdHandlers(); } private final void createRepositoryIdHandlers() { if (orb != null) { // Get the appropriate versions based on the ORB version. The // ORB versioning info is only in the corba ORB. repIdUtil = RepositoryIdFactory.getRepIdUtility(orb); repIdStrs = RepositoryIdFactory.getRepIdStringsFactory(orb); } else { // Get the latest versions repIdUtil = RepositoryIdFactory.getRepIdUtility(); repIdStrs = RepositoryIdFactory.getRepIdStringsFactory(); } } public BufferManagerWrite getBufferManager() { return bufferManagerWrite; } public byte[] toByteArray() { byte[] it; it = new byte[bbwi.index]; System.arraycopy(bbwi.buf, 0, it, 0, bbwi.index); return it; } public GIOPVersion getGIOPVersion() { return GIOPVersion.V1_0; } protected void handleSpecialChunkBegin(int requiredSize) { // No-op for GIOP 1.0 } protected void handleSpecialChunkEnd() { // No-op for GIOP 1.0 } protected final int computeAlignment(int align) { if (align > 1) { int incr = bbwi.index & (align - 1); if (incr != 0) return align - incr; } return 0; } protected void alignAndReserve(int align, int n) { bbwi.index += computeAlignment(align); if (bbwi.index + n > bbwi.buflen) grow(align, n); } // // Default implementation of grow. Subclassers may override this. // Always grow the single buffer. This needs to delegate // fragmentation policy for IIOP 1.1. // protected void grow(int align, int n) { bbwi.needed = n; bufferManagerWrite.overflow(bbwi); } public final void putEndian() throws SystemException { write_boolean(littleEndian); } public final boolean littleEndian() { return littleEndian; } void freeInternalCaches() { if (codebaseCache != null) codebaseCache.done(); if (valueCache != null) valueCache.done(); if (repositoryIdCache != null) repositoryIdCache.done(); } // No such type in java public final void write_longdouble(double x) { throw new NO_IMPLEMENT(MinorCodes.SEND_DEFERRED_NOTIMPLEMENTED, CompletionStatus.COMPLETED_MAYBE); } public void write_octet(byte x) { if (bbwi.index + 1 > bbwi.buflen) alignAndReserve(1, 1); bbwi.buf[bbwi.index++] = x; } public final void write_boolean(boolean x) { write_octet(x? (byte)1:(byte)0); } public void write_char(char x) { CodeSetConversion.CTBConverter converter = getCharConverter(); converter.convert(x); // CORBA formal 99-10-07 15.3.1.6: "In the case of multi-byte encodings // of characters, a single instance of the char type may only // hold one octet of any multi-byte character encoding." if (converter.getNumBytes() > 1) throw new DATA_CONVERSION(MinorCodes.INVALID_SINGLE_CHAR_CTB, CompletionStatus.COMPLETED_MAYBE); write_octet(converter.getBytes()[0]); } // These wchar methods are only used when talking to // legacy ORBs, now. private final void writeLittleEndianWchar(char x) { bbwi.buf[bbwi.index++] = (byte)(x & 0xFF); bbwi.buf[bbwi.index++] = (byte)((x >>> 8) & 0xFF); } private final void writeBigEndianWchar(char x) { bbwi.buf[bbwi.index++] = (byte)((x >>> 8) & 0xFF); bbwi.buf[bbwi.index++] = (byte)(x & 0xFF); } private final void writeLittleEndianShort(short x) { bbwi.buf[bbwi.index++] = (byte)(x & 0xFF); bbwi.buf[bbwi.index++] = (byte)((x >>> 8) & 0xFF); } private final void writeBigEndianShort(short x) { bbwi.buf[bbwi.index++] = (byte)((x >>> 8) & 0xFF); bbwi.buf[bbwi.index++] = (byte)(x & 0xFF); } private final void writeLittleEndianLong(int x) { bbwi.buf[bbwi.index++] = (byte)(x & 0xFF); bbwi.buf[bbwi.index++] = (byte)((x >>> 8) & 0xFF); bbwi.buf[bbwi.index++] = (byte)((x >>> 16) & 0xFF); bbwi.buf[bbwi.index++] = (byte)((x >>> 24) & 0xFF); } private final void writeBigEndianLong(int x) { bbwi.buf[bbwi.index++] = (byte)((x >>> 24) & 0xFF); bbwi.buf[bbwi.index++] = (byte)((x >>> 16) & 0xFF); bbwi.buf[bbwi.index++] = (byte)((x >>> 8) & 0xFF); bbwi.buf[bbwi.index++] = (byte)(x & 0xFF); } private final void writeLittleEndianLongLong(long x) { bbwi.buf[bbwi.index++] = (byte)(x & 0xFF); bbwi.buf[bbwi.index++] = (byte)((x >>> 8) & 0xFF); bbwi.buf[bbwi.index++] = (byte)((x >>> 16) & 0xFF); bbwi.buf[bbwi.index++] = (byte)((x >>> 24) & 0xFF); bbwi.buf[bbwi.index++] = (byte)((x >>> 32) & 0xFF); bbwi.buf[bbwi.index++] = (byte)((x >>> 40) & 0xFF); bbwi.buf[bbwi.index++] = (byte)((x >>> 48) & 0xFF); bbwi.buf[bbwi.index++] = (byte)((x >>> 56) & 0xFF); } private final void writeBigEndianLongLong(long x) { bbwi.buf[bbwi.index++] = (byte)((x >>> 56) & 0xFF); bbwi.buf[bbwi.index++] = (byte)((x >>> 48) & 0xFF); bbwi.buf[bbwi.index++] = (byte)((x >>> 40) & 0xFF); bbwi.buf[bbwi.index++] = (byte)((x >>> 32) & 0xFF); bbwi.buf[bbwi.index++] = (byte)((x >>> 24) & 0xFF); bbwi.buf[bbwi.index++] = (byte)((x >>> 16) & 0xFF); bbwi.buf[bbwi.index++] = (byte)((x >>> 8) & 0xFF); bbwi.buf[bbwi.index++] = (byte)(x & 0xFF); } public void write_wchar(char x) { // Don't allow transmission of wchar/wstring data with // foreign ORBs since it's against the spec. if (ORBUtility.isForeignORB(orb)) { throw new MARSHAL(MinorCodes.WCHAR_DATA_IN_GIOP_1_0, CompletionStatus.COMPLETED_MAYBE); } // If it's one of our legacy ORBs, do what they did: alignAndReserve(2, 2); if (littleEndian) { writeLittleEndianWchar(x); } else { writeBigEndianWchar(x); } } public void write_short(short x) { alignAndReserve(2, 2); if (littleEndian) { writeLittleEndianShort(x); } else { writeBigEndianShort(x); } } public final void write_ushort(short x) { write_short(x); } public void write_long(int x) { alignAndReserve(4, 4); if (littleEndian) { writeLittleEndianLong(x); } else { writeBigEndianLong(x); } } public final void write_ulong(int x) { write_long(x); } public void write_longlong(long x) { alignAndReserve(8, 8); if (littleEndian) { writeLittleEndianLongLong(x); } else { writeBigEndianLongLong(x); } } public final void write_ulonglong(long x) { write_longlong(x); } public final void write_float(float x) { write_long(Float.floatToIntBits(x)); } public final void write_double(double x) { write_longlong(Double.doubleToLongBits(x)); } public void write_string(String value) { if (value == null) { throw new BAD_PARAM(MinorCodes.NULL_PARAM, CompletionStatus.COMPLETED_MAYBE); } CodeSetConversion.CTBConverter converter = getCharConverter(); converter.convert(value); // A string is encoded as an unsigned CORBA long for the // number of bytes to follow (including a terminating null). // There is only one octet per character in the string. int len = converter.getNumBytes() + 1; handleSpecialChunkBegin(computeAlignment(4) + 4 + len); write_long(len); internalWriteOctetArray(converter.getBytes(), 0, converter.getNumBytes()); // Write the null ending write_octet((byte)0); handleSpecialChunkEnd(); } public void write_wstring(String value) { if (value == null) throw new BAD_PARAM(MinorCodes.NULL_PARAM, CompletionStatus.COMPLETED_MAYBE); // Don't allow transmission of wchar/wstring data with // foreign ORBs since it's against the spec. if (ORBUtility.isForeignORB(orb)) { throw new MARSHAL(MinorCodes.WCHAR_DATA_IN_GIOP_1_0, CompletionStatus.COMPLETED_MAYBE); } // When talking to our legacy ORBs, do what they did: int len = value.length() + 1; // This will only have an effect if we're already chunking handleSpecialChunkBegin(4 + (len * 2) + computeAlignment(4)); write_long(len); for (int i = 0; i < len - 1; i++) write_wchar(value.charAt(i)); // Write the null ending write_short((short)0); // This will only have an effect if we're already chunking handleSpecialChunkEnd(); } // Performs no checks and doesn't tamper with chunking void internalWriteOctetArray(byte[] value, int offset, int length) { int n = offset; while (n < length+offset) { int avail; int bytes; int wanted; if (bbwi.index + 1 > bbwi.buflen) alignAndReserve(1, 1); avail = bbwi.buf.length - bbwi.index; wanted = (length + offset) - n; bytes = (wanted < avail) ? wanted : avail; System.arraycopy(value, n, bbwi.buf, bbwi.index, bytes);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?