KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > encoding > BufferManagerWriteStream


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
9 import java.nio.ByteBuffer JavaDoc;
10
11 import com.sun.corba.se.impl.orbutil.ORBConstants;
12 import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
13 import com.sun.corba.se.impl.protocol.giopmsgheaders.MessageBase;
14 import com.sun.corba.se.impl.protocol.giopmsgheaders.FragmentMessage;
15 import com.sun.corba.se.impl.encoding.BufferManagerWrite;
16 import com.sun.corba.se.impl.encoding.ByteBufferWithInfo;
17 import com.sun.corba.se.impl.encoding.CDROutputObject;
18 import com.sun.corba.se.spi.orb.ORB;
19 import com.sun.corba.se.pept.transport.Connection;
20 import com.sun.corba.se.pept.encoding.OutputObject;
21
22 /**
23  * Streaming buffer manager.
24  */

25 public class BufferManagerWriteStream extends BufferManagerWrite
26 {
27     private int fragmentCount = 0;
28
29     BufferManagerWriteStream( ORB orb )
30     {
31     super(orb) ;
32     }
33
34     public boolean sentFragment() {
35         return fragmentCount > 0;
36     }
37
38     /**
39      * Returns the correct buffer size for this type of
40      * buffer manager as set in the ORB.
41      */

42     public int getBufferSize() {
43         return orb.getORBData().getGIOPFragmentSize();
44     }
45
46     public void overflow (ByteBufferWithInfo bbwi)
47     {
48         // Set the fragment's moreFragments field to true
49
MessageBase.setFlag(bbwi.byteBuffer, Message.MORE_FRAGMENTS_BIT);
50
51         sendFragment(false);
52
53         // Reuse the old buffer
54

55         // REVISIT - need to account for case when needed > available
56
// even after fragmenting. This is the large array case, so
57
// the caller should retry when it runs out of space.
58
bbwi.position(0);
59         bbwi.buflen = bbwi.byteBuffer.limit();
60         bbwi.fragmented = true;
61
62         // Now we must marshal in the fragment header/GIOP header
63

64         // REVISIT - we can optimize this by not creating the fragment message
65
// each time.
66

67         FragmentMessage header = ((CDROutputObject)outputObject).getMessageHeader().createFragmentMessage();
68
69         header.write(((CDROutputObject)outputObject));
70     }
71
72     private void sendFragment(boolean isLastFragment)
73     {
74         Connection conn = ((OutputObject)outputObject).getMessageMediator().getConnection();
75
76     // REVISIT: need an ORB
77
//System.out.println("sendFragment: last?: " + isLastFragment);
78
conn.writeLock();
79
80         try {
81             // Send the fragment
82
conn.sendWithoutLock(((OutputObject)outputObject));
83
84             fragmentCount++;
85
86         } finally {
87
88             conn.writeUnlock();
89         }
90
91     }
92
93     // Sends the last fragment
94
public void sendMessage ()
95     {
96         sendFragment(true);
97
98         sentFullMessage = true;
99     }
100
101     /**
102      * Close the BufferManagerWrite and do any outstanding cleanup.
103      *
104      * No work to do for a BufferManagerWriteStream
105      */

106     public void close(){};
107
108 }
109
Popular Tags