1. /*
  2. * @(#)EncapsulationUtility.java 1.23 03/12/19
  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.ior;
  8. import java.util.List;
  9. import java.util.LinkedList;
  10. import java.util.Iterator;
  11. import org.omg.IOP.TAG_INTERNET_IOP ;
  12. import org.omg.CORBA_2_3.portable.OutputStream ;
  13. import org.omg.CORBA_2_3.portable.InputStream ;
  14. import com.sun.corba.se.spi.ior.TaggedComponent ;
  15. import com.sun.corba.se.spi.ior.Identifiable ;
  16. import com.sun.corba.se.spi.ior.IdentifiableFactoryFinder ;
  17. import com.sun.corba.se.spi.ior.WriteContents ;
  18. import com.sun.corba.se.spi.orb.ORB ;
  19. import com.sun.corba.se.impl.ior.FreezableList ;
  20. import com.sun.corba.se.impl.encoding.CDROutputStream ;
  21. import com.sun.corba.se.impl.encoding.EncapsOutputStream ;
  22. import com.sun.corba.se.impl.encoding.EncapsInputStream ;
  23. /**
  24. * This static utility class contains various utility methods for reading and
  25. * writing CDR encapsulations.
  26. *
  27. * @author Ken Cavanaugh
  28. */
  29. public class EncapsulationUtility
  30. {
  31. private EncapsulationUtility()
  32. {
  33. }
  34. /** Read the count from is, then read count Identifiables from
  35. * is using the factory. Add each constructed Identifiable to container.
  36. */
  37. public static void readIdentifiableSequence( List container,
  38. IdentifiableFactoryFinder finder, InputStream istr)
  39. {
  40. int count = istr.read_long() ;
  41. for (int ctr = 0; ctr<count; ctr++) {
  42. int id = istr.read_long() ;
  43. Identifiable obj = finder.create( id, istr ) ;
  44. container.add( obj ) ;
  45. }
  46. }
  47. /** Write all Identifiables that we contain to os. The total
  48. * length must be written before this method is called.
  49. */
  50. public static void writeIdentifiableSequence( List container, OutputStream os)
  51. {
  52. os.write_long( container.size() ) ;
  53. Iterator iter = container.iterator() ;
  54. while (iter.hasNext()) {
  55. Identifiable obj = (Identifiable)( iter.next() ) ;
  56. os.write_long( obj.getId() ) ;
  57. obj.write( os ) ;
  58. }
  59. }
  60. /** Helper method that is used to extract data from an output
  61. * stream and write the data to another output stream. Defined
  62. * as static so that it can be used in another class.
  63. */
  64. static public void writeOutputStream( OutputStream dataStream,
  65. OutputStream os )
  66. {
  67. byte[] data = ((CDROutputStream)dataStream).toByteArray() ;
  68. os.write_long( data.length ) ;
  69. os.write_octet_array( data, 0, data.length ) ;
  70. }
  71. /** Helper method to read the octet array from is, deencapsulate it,
  72. * and return
  73. * as another InputStream. This must be called inside the
  74. * constructor of a derived class to obtain the correct stream
  75. * for unmarshalling data.
  76. */
  77. static public InputStream getEncapsulationStream( InputStream is )
  78. {
  79. byte[] data = readOctets( is ) ;
  80. EncapsInputStream result = new EncapsInputStream( is.orb(), data,
  81. data.length ) ;
  82. result.consumeEndian() ;
  83. return result ;
  84. }
  85. /** Helper method that reads an octet array from an input stream.
  86. * Defined as static here so that it can be used in another class.
  87. */
  88. static public byte[] readOctets( InputStream is )
  89. {
  90. int len = is.read_ulong() ;
  91. byte[] data = new byte[len] ;
  92. is.read_octet_array( data, 0, len ) ;
  93. return data ;
  94. }
  95. static public void writeEncapsulation( WriteContents obj,
  96. OutputStream os )
  97. {
  98. EncapsOutputStream out = new EncapsOutputStream( (ORB)os.orb() ) ;
  99. out.putEndian() ;
  100. obj.writeContents( out ) ;
  101. writeOutputStream( out, os ) ;
  102. }
  103. }