1. /*
  2. * @(#)CDROutputStream.java 1.28 04/06/21
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.impl.encoding;
  8. import java.io.IOException;
  9. import java.io.Serializable;
  10. import java.math.BigDecimal;
  11. import java.nio.ByteBuffer;
  12. import org.omg.CORBA.TypeCode;
  13. import org.omg.CORBA.Principal;
  14. import org.omg.CORBA.Any;
  15. import com.sun.corba.se.pept.protocol.MessageMediator;
  16. import com.sun.corba.se.spi.orb.ORB;
  17. import com.sun.corba.se.spi.logging.CORBALogDomains;
  18. import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
  19. import com.sun.corba.se.spi.protocol.CorbaMessageMediator;
  20. import com.sun.corba.se.impl.encoding.CodeSetConversion;
  21. import com.sun.corba.se.impl.encoding.OSFCodeSetRegistry;
  22. import com.sun.corba.se.impl.orbutil.ORBConstants;
  23. import com.sun.corba.se.impl.logging.ORBUtilSystemException;
  24. import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
  25. /**
  26. * This is delegates to the real implementation.
  27. */
  28. public abstract class CDROutputStream
  29. extends org.omg.CORBA_2_3.portable.OutputStream
  30. implements com.sun.corba.se.impl.encoding.MarshalOutputStream,
  31. org.omg.CORBA.DataOutputStream, org.omg.CORBA.portable.ValueOutputStream
  32. {
  33. private CDROutputStreamBase impl;
  34. protected ORB orb ;
  35. protected ORBUtilSystemException wrapper ;
  36. protected CorbaMessageMediator corbaMessageMediator;
  37. // We can move this out somewhere later. For now, it serves its purpose
  38. // to create a concrete CDR delegate based on the GIOP version.
  39. private static class OutputStreamFactory {
  40. public static CDROutputStreamBase newOutputStream(
  41. ORB orb, GIOPVersion version, byte encodingVersion) {
  42. switch(version.intValue()) {
  43. case GIOPVersion.VERSION_1_0:
  44. return new CDROutputStream_1_0();
  45. case GIOPVersion.VERSION_1_1:
  46. return new CDROutputStream_1_1();
  47. case GIOPVersion.VERSION_1_2:
  48. if (encodingVersion != Message.CDR_ENC_VERSION) {
  49. return
  50. new IDLJavaSerializationOutputStream(encodingVersion);
  51. }
  52. return new CDROutputStream_1_2();
  53. default:
  54. ORBUtilSystemException wrapper = ORBUtilSystemException.get( orb,
  55. CORBALogDomains.RPC_ENCODING ) ;
  56. // REVISIT - what is appropriate? INTERNAL exceptions
  57. // are really hard to track later.
  58. throw wrapper.unsupportedGiopVersion( version ) ;
  59. }
  60. }
  61. }
  62. // REVISIT - These two constructors should be re-factored to better hide
  63. // the fact that someone extending this class 'can' construct a CDROutputStream
  64. // that does not use pooled ByteBuffers. Right now, only EncapsOutputStream
  65. // does _not_ use pooled ByteBuffers, see EncapsOutputStream.
  66. // NOTE: When a stream is constructed for non-channel-backed sockets
  67. // it notifies the constructor not to use pooled (i.e, direct)
  68. // ByteBuffers.
  69. public CDROutputStream(ORB orb,
  70. GIOPVersion version,
  71. byte encodingVersion,
  72. boolean littleEndian,
  73. BufferManagerWrite bufferManager,
  74. byte streamFormatVersion,
  75. boolean usePooledByteBuffers)
  76. {
  77. impl = OutputStreamFactory.newOutputStream(orb, version,
  78. encodingVersion);
  79. impl.init(orb, littleEndian, bufferManager,
  80. streamFormatVersion, usePooledByteBuffers);
  81. impl.setParent(this);
  82. this.orb = orb ;
  83. this.wrapper = ORBUtilSystemException.get( orb,
  84. CORBALogDomains.RPC_ENCODING ) ;
  85. }
  86. public CDROutputStream(ORB orb,
  87. GIOPVersion version,
  88. byte encodingVersion,
  89. boolean littleEndian,
  90. BufferManagerWrite bufferManager,
  91. byte streamFormatVersion)
  92. {
  93. this(orb, version, encodingVersion, littleEndian,
  94. bufferManager, streamFormatVersion, true);
  95. }
  96. // org.omg.CORBA.portable.OutputStream
  97. // Provided by IIOPOutputStream and EncapsOutputStream
  98. public abstract org.omg.CORBA.portable.InputStream create_input_stream();
  99. public final void write_boolean(boolean value) {
  100. impl.write_boolean(value);
  101. }
  102. public final void write_char(char value) {
  103. impl.write_char(value);
  104. }
  105. public final void write_wchar(char value) {
  106. impl.write_wchar(value);
  107. }
  108. public final void write_octet(byte value) {
  109. impl.write_octet(value);
  110. }
  111. public final void write_short(short value) {
  112. impl.write_short(value);
  113. }
  114. public final void write_ushort(short value) {
  115. impl.write_ushort(value);
  116. }
  117. public final void write_long(int value) {
  118. impl.write_long(value);
  119. }
  120. public final void write_ulong(int value) {
  121. impl.write_ulong(value);
  122. }
  123. public final void write_longlong(long value) {
  124. impl.write_longlong(value);
  125. }
  126. public final void write_ulonglong(long value) {
  127. impl.write_ulonglong(value);
  128. }
  129. public final void write_float(float value) {
  130. impl.write_float(value);
  131. }
  132. public final void write_double(double value) {
  133. impl.write_double(value);
  134. }
  135. public final void write_string(String value) {
  136. impl.write_string(value);
  137. }
  138. public final void write_wstring(String value) {
  139. impl.write_wstring(value);
  140. }
  141. public final void write_boolean_array(boolean[] value, int offset, int length) {
  142. impl.write_boolean_array(value, offset, length);
  143. }
  144. public final void write_char_array(char[] value, int offset, int length) {
  145. impl.write_char_array(value, offset, length);
  146. }
  147. public final void write_wchar_array(char[] value, int offset, int length) {
  148. impl.write_wchar_array(value, offset, length);
  149. }
  150. public final void write_octet_array(byte[] value, int offset, int length) {
  151. impl.write_octet_array(value, offset, length);
  152. }
  153. public final void write_short_array(short[] value, int offset, int length) {
  154. impl.write_short_array(value, offset, length);
  155. }
  156. public final void write_ushort_array(short[] value, int offset, int length){
  157. impl.write_ushort_array(value, offset, length);
  158. }
  159. public final void write_long_array(int[] value, int offset, int length) {
  160. impl.write_long_array(value, offset, length);
  161. }
  162. public final void write_ulong_array(int[] value, int offset, int length) {
  163. impl.write_ulong_array(value, offset, length);
  164. }
  165. public final void write_longlong_array(long[] value, int offset, int length) {
  166. impl.write_longlong_array(value, offset, length);
  167. }
  168. public final void write_ulonglong_array(long[] value, int offset,int length) {
  169. impl.write_ulonglong_array(value, offset, length);
  170. }
  171. public final void write_float_array(float[] value, int offset, int length) {
  172. impl.write_float_array(value, offset, length);
  173. }
  174. public final void write_double_array(double[] value, int offset, int length) {
  175. impl.write_double_array(value, offset, length);
  176. }
  177. public final void write_Object(org.omg.CORBA.Object value) {
  178. impl.write_Object(value);
  179. }
  180. public final void write_TypeCode(TypeCode value) {
  181. impl.write_TypeCode(value);
  182. }
  183. public final void write_any(Any value) {
  184. impl.write_any(value);
  185. }
  186. public final void write_Principal(Principal value) {
  187. impl.write_Principal(value);
  188. }
  189. public final void write(int b) throws java.io.IOException {
  190. impl.write(b);
  191. }
  192. public final void write_fixed(java.math.BigDecimal value) {
  193. impl.write_fixed(value);
  194. }
  195. public final void write_Context(org.omg.CORBA.Context ctx,
  196. org.omg.CORBA.ContextList contexts) {
  197. impl.write_Context(ctx, contexts);
  198. }
  199. public final org.omg.CORBA.ORB orb() {
  200. return impl.orb();
  201. }
  202. // org.omg.CORBA_2_3.portable.OutputStream
  203. public final void write_value(java.io.Serializable value) {
  204. impl.write_value(value);
  205. }
  206. public final void write_value(java.io.Serializable value, java.lang.Class clz) {
  207. impl.write_value(value, clz);
  208. }
  209. public final void write_value(java.io.Serializable value, String repository_id) {
  210. impl.write_value(value, repository_id);
  211. }
  212. public final void write_value(java.io.Serializable value,
  213. org.omg.CORBA.portable.BoxedValueHelper factory) {
  214. impl.write_value(value, factory);
  215. }
  216. public final void write_abstract_interface(java.lang.Object obj) {
  217. impl.write_abstract_interface(obj);
  218. }
  219. // java.io.OutputStream
  220. public final void write(byte b[]) throws IOException {
  221. impl.write(b);
  222. }
  223. public final void write(byte b[], int off, int len) throws IOException {
  224. impl.write(b, off, len);
  225. }
  226. public final void flush() throws IOException {
  227. impl.flush();
  228. }
  229. public final void close() throws IOException {
  230. impl.close();
  231. }
  232. // com.sun.corba.se.impl.encoding.MarshalOutputStream
  233. public final void start_block() {
  234. impl.start_block();
  235. }
  236. public final void end_block() {
  237. impl.end_block();
  238. }
  239. public final void putEndian() {
  240. impl.putEndian();
  241. }
  242. public void writeTo(java.io.OutputStream s)
  243. throws IOException
  244. {
  245. impl.writeTo(s);
  246. }
  247. public final byte[] toByteArray() {
  248. return impl.toByteArray();
  249. }
  250. // org.omg.CORBA.DataOutputStream
  251. public final void write_Abstract (java.lang.Object value) {
  252. impl.write_Abstract(value);
  253. }
  254. public final void write_Value (java.io.Serializable value) {
  255. impl.write_Value(value);
  256. }
  257. public final void write_any_array(org.omg.CORBA.Any[] seq, int offset, int length) {
  258. impl.write_any_array(seq, offset, length);
  259. }
  260. public void setMessageMediator(MessageMediator messageMediator)
  261. {
  262. this.corbaMessageMediator = (CorbaMessageMediator) messageMediator;
  263. }
  264. public MessageMediator getMessageMediator()
  265. {
  266. return corbaMessageMediator;
  267. }
  268. // org.omg.CORBA.portable.ValueBase
  269. public final String[] _truncatable_ids() {
  270. return impl._truncatable_ids();
  271. }
  272. // Other
  273. protected final int getSize() {
  274. return impl.getSize();
  275. }
  276. protected final int getIndex() {
  277. return impl.getIndex();
  278. }
  279. protected int getRealIndex(int index) {
  280. // Used in indirections. Overridden by TypeCodeOutputStream.
  281. return index;
  282. }
  283. protected final void setIndex(int value) {
  284. impl.setIndex(value);
  285. }
  286. protected final ByteBuffer getByteBuffer() {
  287. return impl.getByteBuffer();
  288. }
  289. protected final void setByteBuffer(ByteBuffer byteBuffer) {
  290. impl.setByteBuffer(byteBuffer);
  291. }
  292. public final boolean isLittleEndian() {
  293. return impl.isLittleEndian();
  294. }
  295. // XREVISIT - return to final if possible
  296. // REVISIT - was protected - need access from msgtypes test.
  297. public ByteBufferWithInfo getByteBufferWithInfo() {
  298. return impl.getByteBufferWithInfo();
  299. }
  300. protected void setByteBufferWithInfo(ByteBufferWithInfo bbwi) {
  301. impl.setByteBufferWithInfo(bbwi);
  302. }
  303. // REVISIT: was protected - but need to access from xgiop.
  304. public final BufferManagerWrite getBufferManager() {
  305. return impl.getBufferManager();
  306. }
  307. public final void write_fixed(java.math.BigDecimal bigDecimal, short digits, short scale) {
  308. impl.write_fixed(bigDecimal, digits, scale);
  309. }
  310. public final void writeOctetSequenceTo(org.omg.CORBA.portable.OutputStream s) {
  311. impl.writeOctetSequenceTo(s);
  312. }
  313. public final GIOPVersion getGIOPVersion() {
  314. return impl.getGIOPVersion();
  315. }
  316. public final void writeIndirection(int tag, int posIndirectedTo) {
  317. impl.writeIndirection(tag, posIndirectedTo);
  318. }
  319. // Use Latin-1 for GIOP 1.0 or when code set negotiation was not
  320. // performed.
  321. protected CodeSetConversion.CTBConverter createCharCTBConverter() {
  322. return CodeSetConversion.impl().getCTBConverter(OSFCodeSetRegistry.ISO_8859_1);
  323. }
  324. // Subclasses must decide what to do here. It's inconvenient to
  325. // make the class and this method abstract because of dup().
  326. protected abstract CodeSetConversion.CTBConverter createWCharCTBConverter();
  327. protected final void freeInternalCaches() {
  328. impl.freeInternalCaches();
  329. }
  330. void printBuffer() {
  331. impl.printBuffer();
  332. }
  333. public void alignOnBoundary(int octetBoundary) {
  334. impl.alignOnBoundary(octetBoundary);
  335. }
  336. // Needed by request and reply messages for GIOP versions >= 1.2 only.
  337. public void setHeaderPadding(boolean headerPadding) {
  338. impl.setHeaderPadding(headerPadding);
  339. }
  340. // ValueOutputStream -----------------------------
  341. public void start_value(String rep_id) {
  342. impl.start_value(rep_id);
  343. }
  344. public void end_value() {
  345. impl.end_value();
  346. }
  347. }