GeneralHolder.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* universalStreamable.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 org.omg.CORBA.BAD_OPERATION;
  35. import org.omg.CORBA.MARSHAL;
  36. import org.omg.CORBA.TypeCode;
  37. import org.omg.CORBA.portable.InputStream;
  38. import org.omg.CORBA.portable.OutputStream;
  39. import org.omg.CORBA.portable.Streamable;
  40. import java.io.IOException;
  41. /**
  42. * This class holds the abstract binary data array of the Streamable
  43. * being stored. It is used to insert and extract into {@link Any} objects
  44. * that have no holder, but have the helper classes.
  45. * Encoding/decoding then must be performed by the helper. This class is
  46. * defined as package private because it is not idiot proof and
  47. * must be used with care.
  48. *
  49. * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  50. */
  51. public class GeneralHolder
  52. implements Streamable
  53. {
  54. /**
  55. * The binary data, stored inside this holder.
  56. */
  57. private BufferedCdrOutput value = new BufferedCdrOutput();
  58. /**
  59. * Create the universal holder that uses the given buffer to store the data.
  60. */
  61. public GeneralHolder(BufferedCdrOutput buffer)
  62. {
  63. value = buffer;
  64. }
  65. /**
  66. * Reads into the internal buffer till the end of stream is
  67. * reached. No alignment operations are performed. This operation
  68. * normally reads from the stream, where the single value,
  69. * stored using {@link #_write}, is written.
  70. *
  71. * @throws MARSHALL, if the IOException is thrown during the
  72. * stream operation.
  73. */
  74. public void _read(InputStream input)
  75. {
  76. try
  77. {
  78. if (input instanceof BufferredCdrInput)
  79. {
  80. BufferredCdrInput b = (BufferredCdrInput) input;
  81. value.write(b.buffer.getBuffer());
  82. }
  83. else
  84. {
  85. int c;
  86. do
  87. {
  88. c = input.read();
  89. if (c >= 0)
  90. value.write(c);
  91. }
  92. while (c >= 0);
  93. }
  94. }
  95. catch (IOException ex)
  96. {
  97. MARSHAL t = new MARSHAL();
  98. t.minor = Minor.Any;
  99. t.initCause(ex);
  100. throw t;
  101. }
  102. }
  103. /**
  104. * The type is not known and cannot be returned.
  105. *
  106. * @throws BAD_OPERATION, always.
  107. */
  108. public TypeCode _type()
  109. {
  110. BAD_OPERATION bad = new BAD_OPERATION();
  111. bad.minor = Minor.Inappropriate;
  112. throw bad;
  113. }
  114. /**
  115. * Writes the binary data being stored into the given output
  116. * stream. This operation supposes that the current stream
  117. * position is 0 or satisfies the required alignments anyway.
  118. *
  119. * @throws MARSHAL if the IOExeption is thrown when writing the
  120. * byte array.
  121. */
  122. public void _write(OutputStream output)
  123. {
  124. try
  125. {
  126. value.buffer.writeTo(output);
  127. }
  128. catch (IOException ex)
  129. {
  130. MARSHAL t = new MARSHAL();
  131. t.minor = Minor.Any;
  132. t.initCause(ex);
  133. throw t;
  134. }
  135. }
  136. /**
  137. * Get the input stream that reads the fields of the stored value.
  138. */
  139. InputStream getInputStream()
  140. {
  141. return value.create_input_stream();
  142. }
  143. /**
  144. * Clone.
  145. */
  146. public GeneralHolder Clone()
  147. {
  148. try
  149. {
  150. BufferedCdrOutput nb = new BufferedCdrOutput(value.buffer.size());
  151. value.buffer.writeTo(nb);
  152. return new GeneralHolder(nb);
  153. }
  154. catch (IOException ex)
  155. {
  156. throw new Unexpected(ex);
  157. }
  158. }
  159. }