1. /*
  2. * @(#)IdEncapsulationBase.java 1.5 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.internal.ior;
  8. import com.sun.corba.se.internal.ior.Writeable ;
  9. import com.sun.corba.se.internal.core.GIOPVersion ;
  10. import com.sun.corba.se.internal.corba.EncapsOutputStream ;
  11. import com.sun.corba.se.internal.corba.EncapsInputStream ;
  12. import com.sun.corba.se.internal.iiop.CDROutputStream ;
  13. import org.omg.CORBA_2_3.portable.OutputStream ;
  14. import org.omg.CORBA_2_3.portable.InputStream ;
  15. /** Provide support for properly reading and writing IdEncapsulation objects
  16. * (tagged profiles and components).
  17. */
  18. public abstract class IdEncapsulationBase implements Writeable
  19. {
  20. /** Write the data for this object as a CDR encapsulation.
  21. * This is used for writing tagged components and profiles.
  22. * These data types must be written out as encapsulations,
  23. * which means that we need to first write the data out to
  24. * an encapsulation stream, then extract the data and write
  25. * it to os as an array of octets.
  26. */
  27. final public void write( OutputStream os )
  28. {
  29. EncapsOutputStream out = new EncapsOutputStream( os.orb() ) ;
  30. out.putEndian() ;
  31. writeContents( out ) ;
  32. writeOutputStream( out, os ) ;
  33. }
  34. /** This method actually writes the real data to the encapsulation
  35. * stream. It must be defined in a subclass.
  36. */
  37. abstract public void writeContents( OutputStream os ) ;
  38. /** Helper method that is used to extract data from an output
  39. * stream and write the data to another output stream. Defined
  40. * as static so that it can be used in another class.
  41. */
  42. static public void writeOutputStream( CDROutputStream dataStream,
  43. OutputStream os )
  44. {
  45. byte[] data = dataStream.toByteArray() ;
  46. os.write_long( data.length ) ;
  47. os.write_octet_array( data, 0, data.length ) ;
  48. }
  49. /** Helper method to read the octet array from is, deencapsulate it,
  50. * and return
  51. * as another InputStream. This must be called inside the
  52. * constructor of a derived class to obtain the correct stream
  53. * for unmarshalling data.
  54. */
  55. static public InputStream getEncapsulationStream( InputStream is )
  56. {
  57. byte[] data = readOctets( is ) ;
  58. EncapsInputStream result = new EncapsInputStream( is.orb(), data,
  59. data.length ) ;
  60. result.consumeEndian() ;
  61. return result ;
  62. }
  63. /** Helper method that reads an octet array from an input stream.
  64. * Defined as static here so that it can be used in another class.
  65. */
  66. static public byte[] readOctets( InputStream is )
  67. {
  68. int len = is.read_ulong() ;
  69. byte[] data = new byte[len] ;
  70. is.read_octet_array( data, 0, len ) ;
  71. return data ;
  72. }
  73. }