KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)ByteBufferWithInfo.java 1.10 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
8 package com.sun.corba.se.impl.encoding;
9
10 import java.nio.ByteBuffer JavaDoc;
11
12
13 import com.sun.corba.se.impl.encoding.BufferManagerWrite;
14 import com.sun.corba.se.impl.orbutil.ORBUtility;
15 import com.sun.corba.se.pept.transport.ByteBufferPool;
16 import com.sun.corba.se.spi.orb.ORB;
17
18
19 // Notes about the class.
20
// Assumptions, the ByteBuffer's position is set by the constructor's
21
// index variable and the ByteBuffer's limit points to the end of the
22
// data. Also, since the index variable tracks the current empty
23
// position in the buffer, the ByteBuffer's position is updated
24
// any time there's a call to this class's position().
25
// Although, a ByteBuffer's length is it's capacity(), the context in
26
// which length is used in this object, this.buflen is actually the
27
// ByteBuffer limit().
28

29 public class ByteBufferWithInfo
30 {
31     private ORB orb;
32     private boolean debug;
33     // REVISIT - index should eventually be replaced with byteBuffer.position()
34
private int index; // Current empty position in buffer.
35
// REVISIT - CHANGE THESE TO PRIVATE
36
public ByteBuffer JavaDoc byteBuffer;// Marshal buffer.
37
public int buflen; // Total length of buffer. // Unnecessary...
38
public int needed; // How many more bytes are needed on overflow.
39
public boolean fragmented; // Did the overflow operation fragment?
40

41     public ByteBufferWithInfo(org.omg.CORBA.ORB JavaDoc orb,
42                               ByteBuffer JavaDoc byteBuffer,
43                               int index)
44     {
45         this.orb = (com.sun.corba.se.spi.orb.ORB)orb;
46         debug = this.orb.transportDebugFlag;
47     this.byteBuffer = byteBuffer;
48         if (byteBuffer != null)
49         {
50             this.buflen = byteBuffer.limit();
51         }
52         position(index);
53     this.needed = 0;
54         this.fragmented = false;
55     }
56
57     public ByteBufferWithInfo(org.omg.CORBA.ORB JavaDoc orb, ByteBuffer JavaDoc byteBuffer)
58     {
59     this(orb, byteBuffer, 0);
60     }
61
62     public ByteBufferWithInfo(org.omg.CORBA.ORB JavaDoc orb,
63                               BufferManagerWrite bufferManager)
64     {
65         this(orb, bufferManager, true);
66     }
67
68     // Right now, EncapsOutputStream's do not use pooled byte buffers.
69
// EncapsOutputStream's is the only one that does not use pooled
70
// byte buffers. Hence, the reason for the boolean 'usePooledByteBuffers'.
71
// See EncapsOutputStream for additional information.
72

73     public ByteBufferWithInfo(org.omg.CORBA.ORB JavaDoc orb,
74                               BufferManagerWrite bufferManager,
75                               boolean usePooledByteBuffers)
76     {
77         this.orb = (com.sun.corba.se.spi.orb.ORB)orb;
78         debug = this.orb.transportDebugFlag;
79
80         int bufferSize = bufferManager.getBufferSize();
81
82         if (usePooledByteBuffers)
83         {
84             ByteBufferPool byteBufferPool = this.orb.getByteBufferPool();
85             this.byteBuffer = byteBufferPool.getByteBuffer(bufferSize);
86
87             if (debug)
88             {
89                 // print address of ByteBuffer gotten from pool
90
int bbAddress = System.identityHashCode(byteBuffer);
91                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc(80);
92                 sb.append("constructor (ORB, BufferManagerWrite) - got ")
93                   .append("ByteBuffer id (").append(bbAddress)
94                   .append(") from ByteBufferPool.");
95                 String JavaDoc msgStr = sb.toString();
96                 dprint(msgStr);
97             }
98         }
99         else
100         {
101              // don't allocate from pool, allocate non-direct ByteBuffer
102
this.byteBuffer = ByteBuffer.allocate(bufferSize);
103         }
104
105         position(0);
106         this.buflen = bufferSize;
107         this.byteBuffer.limit(this.buflen);
108         this.needed = 0;
109         this.fragmented = false;
110     }
111
112     // Shallow copy constructor
113
public ByteBufferWithInfo (ByteBufferWithInfo bbwi)
114     {
115         this.orb = bbwi.orb;
116         this.debug = bbwi.debug;
117         this.byteBuffer = bbwi.byteBuffer;
118         this.buflen = bbwi.buflen;
119         this.byteBuffer.limit(this.buflen);
120         position(bbwi.position());
121         this.needed = bbwi.needed;
122         this.fragmented = bbwi.fragmented;
123     }
124
125     // So IIOPOutputStream seems more intuitive
126
public int getSize()
127     {
128         return position();
129     }
130
131     // accessor to buflen
132
public int getLength()
133     {
134          return buflen;
135     }
136
137     // get position in this buffer
138
public int position()
139     {
140         // REVISIT - This should be changed to return the
141
// value of byteBuffer.position() rather
142
// than this.index. But, byteBuffer.position
143
// is manipulated via ByteBuffer writes, reads,
144
// gets and puts. These locations need to be
145
// investigated and updated before
146
// byteBuffer.position() can be returned here.
147
// return byteBuffer.position();
148
return index;
149     }
150
151     // set position in this buffer
152
public void position(int newPosition)
153     {
154         // REVISIT - This should be changed to set only the
155
// value of byteBuffer.position rather
156
// than this.index. This change should be made
157
// in conjunction with the change to this.position().
158
byteBuffer.position(newPosition);
159         index = newPosition;
160     }
161
162     // mutator to buflen
163
public void setLength(int theLength)
164     {
165         buflen = theLength;
166         byteBuffer.limit(buflen);
167     }
168
169     // Grow byteBuffer to a size larger than position() + needed
170
public void growBuffer(com.sun.corba.se.spi.orb.ORB orb)
171     {
172         // This code used to live directly in CDROutputStream.grow.
173

174         // Recall that the byteBuffer size is 'really' the limit or
175
// buflen.
176

177         int newLength = byteBuffer.limit() * 2;
178
179         while (position() + needed >= newLength)
180             newLength = newLength * 2;
181
182         ByteBufferPool byteBufferPool = orb.getByteBufferPool();
183         ByteBuffer JavaDoc newBB = byteBufferPool.getByteBuffer(newLength);
184
185         if (debug)
186         {
187             // print address of ByteBuffer just gotten
188
int newbbAddress = System.identityHashCode(newBB);
189             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(80);
190             sb.append("growBuffer() - got ByteBuffer id (");
191             sb.append(newbbAddress).append(") from ByteBufferPool.");
192             String JavaDoc msgStr = sb.toString();
193             dprint(msgStr);
194         }
195
196         byteBuffer.position(0);
197         newBB.put(byteBuffer);
198
199         // return 'old' byteBuffer reference to the ByteBuffer pool
200
if (debug)
201         {
202             // print address of ByteBuffer being released
203
int bbAddress = System.identityHashCode(byteBuffer);
204             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(80);
205             sb.append("growBuffer() - releasing ByteBuffer id (");
206             sb.append(bbAddress).append(") to ByteBufferPool.");
207             String JavaDoc msgStr2 = sb.toString();
208             dprint(msgStr2);
209         }
210         byteBufferPool.releaseByteBuffer(byteBuffer);
211
212         // update the byteBuffer with a larger ByteBuffer
213
byteBuffer = newBB;
214
215         // limit and buflen must be set to newLength.
216
buflen = newLength;
217         byteBuffer.limit(buflen);
218     }
219    
220     public String JavaDoc toString()
221     {
222         StringBuffer JavaDoc str = new StringBuffer JavaDoc("ByteBufferWithInfo:");
223
224         str.append(" buflen = " + buflen);
225         str.append(" byteBuffer.limit = " + byteBuffer.limit());
226         str.append(" index = " + index);
227         str.append(" position = " + position());
228         str.append(" needed = " + needed);
229         str.append(" byteBuffer = " + (byteBuffer == null ? "null" : "not null"));
230         str.append(" fragmented = " + fragmented);
231
232         return str.toString();
233     }
234
235     protected void dprint(String JavaDoc msg)
236     {
237         ORBUtility.dprint("ByteBufferWithInfo", msg);
238     }
239 }
240
Popular Tags