CdrEncapsCodecImpl.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* CdrEncapsCodecImpl.java --
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.CORBA;
  32. import gnu.CORBA.CDR.BufferredCdrInput;
  33. import gnu.CORBA.CDR.BufferedCdrOutput;
  34. import gnu.CORBA.CDR.AbstractCdrOutput;
  35. import org.omg.CORBA.Any;
  36. import org.omg.CORBA.LocalObject;
  37. import org.omg.CORBA.MARSHAL;
  38. import org.omg.CORBA.ORB;
  39. import org.omg.CORBA.TCKind;
  40. import org.omg.CORBA.TypeCode;
  41. import org.omg.CORBA.UserException;
  42. import org.omg.IOP.Codec;
  43. import org.omg.IOP.CodecPackage.FormatMismatch;
  44. import org.omg.IOP.CodecPackage.InvalidTypeForEncoding;
  45. import org.omg.IOP.CodecPackage.TypeMismatch;
  46. /**
  47. * The local {@link Codec} implementation for ENCODING_CDR_ENCAPS
  48. * encoding. This is a local implementation; the remote side should
  49. * have its own Codec of this kind.
  50. *
  51. *
  52. * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  53. */
  54. public class CdrEncapsCodecImpl
  55. extends LocalObject
  56. implements Codec
  57. {
  58. /**
  59. * Use serialVersionUID for interoperability.
  60. */
  61. private static final long serialVersionUID = 1;
  62. /**
  63. * If set to true, no wide string or wide character is allowed (GIOP 1.0).
  64. */
  65. private final boolean noWide;
  66. /**
  67. * The version of this encoding.
  68. */
  69. private final Version version;
  70. /**
  71. * The associated ORB.
  72. */
  73. protected final ORB orb;
  74. /**
  75. * If true, this Codec writes the record length (as int) in the beginning
  76. * of the record. This indicator is part of the formal OMG standard, but it is
  77. * missing in Sun's implementation. Both Suns's and this Codec detects
  78. * the indicator, if present, but can also decode data where this information
  79. * is missing. If the length indicator is missing, the first four bytes in
  80. * Suns encoding are equal to 0 (Big Endian marker).
  81. */
  82. private boolean lengthIndicator = true;
  83. /**
  84. * Create an instance of this Codec, encoding following the given version.
  85. */
  86. public CdrEncapsCodecImpl(ORB _orb, Version _version)
  87. {
  88. orb = _orb;
  89. version = _version;
  90. noWide = version.until_inclusive(1, 0);
  91. }
  92. /**
  93. * Return the array of repository ids for this object.
  94. *
  95. * @return { "IDL:gnu/CORBA/cdrEnapsCodec:1.0" }, always.
  96. */
  97. public String[] _ids()
  98. {
  99. return new String[] { "IDL:gnu/CORBA/cdrEnapsCodec:1.0" };
  100. }
  101. /**
  102. * Decode the contents of the byte array into Any.
  103. * The byte array may have the optional four byte length indicator
  104. * in the beginning. If these four bytes are zero, it is assumed,
  105. * that no length indicator is present.
  106. */
  107. public Any decode(byte[] them)
  108. throws FormatMismatch
  109. {
  110. BufferredCdrInput input = createInput(them);
  111. BufferredCdrInput encapsulation = createEncapsulation(them, input);
  112. TypeCode type = encapsulation.read_TypeCode();
  113. try
  114. {
  115. checkTypePossibility("", type);
  116. }
  117. catch (InvalidTypeForEncoding ex)
  118. {
  119. throw new FormatMismatch(ex.getMessage());
  120. }
  121. return readAny(type, encapsulation);
  122. }
  123. private BufferredCdrInput createEncapsulation(byte[] them, BufferredCdrInput input)
  124. {
  125. BufferredCdrInput encapsulation;
  126. if ((them [ 0 ] | them [ 1 ] | them [ 2 ] | them [ 3 ]) == 0)
  127. {
  128. // Skip that appears to be the always present Big Endian marker.
  129. encapsulation = input;
  130. input.read_short();
  131. }
  132. else
  133. encapsulation = input.read_encapsulation();
  134. return encapsulation;
  135. }
  136. /** {@inheritDoc} */
  137. public byte[] encode(Any that)
  138. throws InvalidTypeForEncoding
  139. {
  140. checkTypePossibility("", that.type());
  141. BufferedCdrOutput output = createOutput(that);
  142. // BufferedCdrOutput has internal support for this encoding.
  143. AbstractCdrOutput encapsulation = output.createEncapsulation();
  144. try
  145. {
  146. TypeCodeHelper.write(encapsulation, that.type());
  147. that.write_value(encapsulation);
  148. encapsulation.close();
  149. output.close();
  150. }
  151. catch (Exception ex)
  152. {
  153. MARSHAL m = new MARSHAL();
  154. m.minor = Minor.Encapsulation;
  155. m.initCause(ex);
  156. throw m;
  157. }
  158. return output.buffer.toByteArray();
  159. }
  160. /**
  161. * Decode the value, stored in the byte array, into Any, assuming,
  162. * that the byte array holds the data structure, defined by the
  163. * given typecode.
  164. *
  165. * The byte array may have the optional four byte length indicator
  166. * in the beginning. If these four bytes are zero, it is assumed,
  167. * that no length indicator is present.
  168. */
  169. public Any decode_value(byte[] them, TypeCode type)
  170. throws FormatMismatch, TypeMismatch
  171. {
  172. try
  173. {
  174. checkTypePossibility("", type);
  175. }
  176. catch (InvalidTypeForEncoding ex)
  177. {
  178. throw new TypeMismatch(ex.getMessage());
  179. }
  180. BufferredCdrInput input = createInput(them);
  181. BufferredCdrInput encapsulation = createEncapsulation(them, input);
  182. return readAny(type, encapsulation);
  183. }
  184. /**
  185. * Read an Any from the given stream.
  186. *
  187. * @param type a type of the Any to read.
  188. * @param input the encapsulation stream.
  189. */
  190. private Any readAny(TypeCode type, BufferredCdrInput encapsulation)
  191. throws MARSHAL
  192. {
  193. gnuAny a = new gnuAny();
  194. a.setOrb(orb);
  195. // BufferredCdrInput has internal support for this encoding.
  196. a.read_value(encapsulation, type);
  197. return a;
  198. }
  199. /** {@inheritDoc} */
  200. public byte[] encode_value(Any that)
  201. throws InvalidTypeForEncoding
  202. {
  203. checkTypePossibility("", that.type());
  204. BufferedCdrOutput output = createOutput(that);
  205. AbstractCdrOutput encapsulation = output.createEncapsulation();
  206. try
  207. {
  208. that.write_value(encapsulation);
  209. encapsulation.close();
  210. output.close();
  211. }
  212. catch (Exception ex)
  213. {
  214. MARSHAL m = new MARSHAL();
  215. m.minor = Minor.Encapsulation;
  216. m.initCause(ex);
  217. throw m;
  218. }
  219. return output.buffer.toByteArray();
  220. }
  221. /**
  222. * Create the CDR output stream for writing the given Any.
  223. * The BufferedCdrOutput has internal support for encapsulation encodings.
  224. *
  225. * @param that the Any that will be written.
  226. *
  227. * @return the stream.
  228. *
  229. * @throws InvalidTypeForEncoding if that Any cannot be written under the
  230. * given version.
  231. */
  232. private BufferedCdrOutput createOutput(Any that)
  233. throws InvalidTypeForEncoding
  234. {
  235. BufferedCdrOutput output = new BufferedCdrOutput();
  236. output.setOrb(orb);
  237. output.setVersion(version);
  238. return output;
  239. }
  240. /**
  241. * Checks if the given type can be encoded. Currently only checks for wide
  242. * strings and wide chars for GIOP 1.0.
  243. *
  244. * @param t a typecode to chek.
  245. *
  246. * @throws InvalidTypeForEncoding if the typecode is not valid for the given
  247. * version.
  248. */
  249. private void checkTypePossibility(String name, TypeCode t)
  250. throws InvalidTypeForEncoding
  251. {
  252. if (noWide)
  253. {
  254. try
  255. {
  256. int kind = t.kind().value();
  257. if (kind == TCKind._tk_wchar || kind == TCKind._tk_wstring)
  258. throw new InvalidTypeForEncoding(name + " wide char in " +
  259. version
  260. );
  261. else if (kind == TCKind._tk_alias || kind == TCKind._tk_array ||
  262. kind == TCKind._tk_sequence
  263. )
  264. checkTypePossibility("Array member", t.content_type());
  265. else if (kind == TCKind._tk_struct || kind == TCKind._tk_union)
  266. {
  267. for (int i = 0; i < t.member_count(); i++)
  268. {
  269. checkTypePossibility(t.member_name(i), t.member_type(i));
  270. }
  271. }
  272. }
  273. catch (UserException ex)
  274. {
  275. InternalError ierr = new InternalError();
  276. ierr.initCause(ex);
  277. throw ierr;
  278. }
  279. }
  280. }
  281. /**
  282. * Create the CDR input stream for reading the given byte array.
  283. *
  284. * @param them a byte array to read.
  285. *
  286. * @return the stream.
  287. */
  288. private BufferredCdrInput createInput(byte[] them)
  289. {
  290. BufferredCdrInput input = new BufferredCdrInput(them);
  291. input.setOrb(orb);
  292. input.setVersion(version);
  293. return input;
  294. }
  295. /**
  296. * Check if the Codec writes the length indicator.
  297. */
  298. public boolean hasLengthIndicator()
  299. {
  300. return lengthIndicator;
  301. }
  302. /**
  303. * Sets if the Codec must write the record length in the beginning of the
  304. * array. Encodings both with and without that indicator are understood
  305. * both by Suns and this codec, but the OMG specification seems requiring
  306. * it. The default behavior is to use the length indicator.
  307. *
  308. * @param use_lengthIndicator
  309. */
  310. public void setUseLengthIndicator(boolean use_lengthIndicator)
  311. {
  312. lengthIndicator = use_lengthIndicator;
  313. }
  314. }