1. /*
  2. * @(#)BufferManagerWriteStream.java 1.15 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.encoding;
  8. import java.nio.ByteBuffer;
  9. import com.sun.corba.se.impl.orbutil.ORBConstants;
  10. import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
  11. import com.sun.corba.se.impl.protocol.giopmsgheaders.MessageBase;
  12. import com.sun.corba.se.impl.protocol.giopmsgheaders.FragmentMessage;
  13. import com.sun.corba.se.impl.encoding.BufferManagerWrite;
  14. import com.sun.corba.se.impl.encoding.ByteBufferWithInfo;
  15. import com.sun.corba.se.impl.encoding.CDROutputObject;
  16. import com.sun.corba.se.spi.orb.ORB;
  17. import com.sun.corba.se.pept.transport.Connection;
  18. import com.sun.corba.se.pept.encoding.OutputObject;
  19. /**
  20. * Streaming buffer manager.
  21. */
  22. public class BufferManagerWriteStream extends BufferManagerWrite
  23. {
  24. private int fragmentCount = 0;
  25. BufferManagerWriteStream( ORB orb )
  26. {
  27. super(orb) ;
  28. }
  29. public boolean sentFragment() {
  30. return fragmentCount > 0;
  31. }
  32. /**
  33. * Returns the correct buffer size for this type of
  34. * buffer manager as set in the ORB.
  35. */
  36. public int getBufferSize() {
  37. return orb.getORBData().getGIOPFragmentSize();
  38. }
  39. public void overflow (ByteBufferWithInfo bbwi)
  40. {
  41. // Set the fragment's moreFragments field to true
  42. MessageBase.setFlag(bbwi.byteBuffer, Message.MORE_FRAGMENTS_BIT);
  43. sendFragment(false);
  44. // Reuse the old buffer
  45. // REVISIT - need to account for case when needed > available
  46. // even after fragmenting. This is the large array case, so
  47. // the caller should retry when it runs out of space.
  48. bbwi.position(0);
  49. bbwi.buflen = bbwi.byteBuffer.limit();
  50. bbwi.fragmented = true;
  51. // Now we must marshal in the fragment header/GIOP header
  52. // REVISIT - we can optimize this by not creating the fragment message
  53. // each time.
  54. FragmentMessage header = ((CDROutputObject)outputObject).getMessageHeader().createFragmentMessage();
  55. header.write(((CDROutputObject)outputObject));
  56. }
  57. private void sendFragment(boolean isLastFragment)
  58. {
  59. Connection conn = ((OutputObject)outputObject).getMessageMediator().getConnection();
  60. // REVISIT: need an ORB
  61. //System.out.println("sendFragment: last?: " + isLastFragment);
  62. conn.writeLock();
  63. try {
  64. // Send the fragment
  65. conn.sendWithoutLock(((OutputObject)outputObject));
  66. fragmentCount++;
  67. } finally {
  68. conn.writeUnlock();
  69. }
  70. }
  71. // Sends the last fragment
  72. public void sendMessage ()
  73. {
  74. sendFragment(true);
  75. sentFullMessage = true;
  76. }
  77. /**
  78. * Close the BufferManagerWrite and do any outstanding cleanup.
  79. *
  80. * No work to do for a BufferManagerWriteStream
  81. */
  82. public void close(){};
  83. }