1 7 8 package com.sun.corba.se.impl.encoding; 9 10 import java.nio.ByteBuffer ; 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 29 public class ByteBufferWithInfo 30 { 31 private ORB orb; 32 private boolean debug; 33 private int index; public ByteBuffer byteBuffer; public int buflen; public int needed; public boolean fragmented; 41 public ByteBufferWithInfo(org.omg.CORBA.ORB orb, 42 ByteBuffer 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 orb, ByteBuffer byteBuffer) 58 { 59 this(orb, byteBuffer, 0); 60 } 61 62 public ByteBufferWithInfo(org.omg.CORBA.ORB orb, 63 BufferManagerWrite bufferManager) 64 { 65 this(orb, bufferManager, true); 66 } 67 68 73 public ByteBufferWithInfo(org.omg.CORBA.ORB 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 int bbAddress = System.identityHashCode(byteBuffer); 91 StringBuffer sb = new StringBuffer (80); 92 sb.append("constructor (ORB, BufferManagerWrite) - got ") 93 .append("ByteBuffer id (").append(bbAddress) 94 .append(") from ByteBufferPool."); 95 String msgStr = sb.toString(); 96 dprint(msgStr); 97 } 98 } 99 else 100 { 101 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 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 public int getSize() 127 { 128 return position(); 129 } 130 131 public int getLength() 133 { 134 return buflen; 135 } 136 137 public int position() 139 { 140 return index; 149 } 150 151 public void position(int newPosition) 153 { 154 byteBuffer.position(newPosition); 159 index = newPosition; 160 } 161 162 public void setLength(int theLength) 164 { 165 buflen = theLength; 166 byteBuffer.limit(buflen); 167 } 168 169 public void growBuffer(com.sun.corba.se.spi.orb.ORB orb) 171 { 172 174 177 int newLength = byteBuffer.limit() * 2; 178 179 while (position() + needed >= newLength) 180 newLength = newLength * 2; 181 182 ByteBufferPool byteBufferPool = orb.getByteBufferPool(); 183 ByteBuffer newBB = byteBufferPool.getByteBuffer(newLength); 184 185 if (debug) 186 { 187 int newbbAddress = System.identityHashCode(newBB); 189 StringBuffer sb = new StringBuffer (80); 190 sb.append("growBuffer() - got ByteBuffer id ("); 191 sb.append(newbbAddress).append(") from ByteBufferPool."); 192 String msgStr = sb.toString(); 193 dprint(msgStr); 194 } 195 196 byteBuffer.position(0); 197 newBB.put(byteBuffer); 198 199 if (debug) 201 { 202 int bbAddress = System.identityHashCode(byteBuffer); 204 StringBuffer sb = new StringBuffer (80); 205 sb.append("growBuffer() - releasing ByteBuffer id ("); 206 sb.append(bbAddress).append(") to ByteBufferPool."); 207 String msgStr2 = sb.toString(); 208 dprint(msgStr2); 209 } 210 byteBufferPool.releaseByteBuffer(byteBuffer); 211 212 byteBuffer = newBB; 214 215 buflen = newLength; 217 byteBuffer.limit(buflen); 218 } 219 220 public String toString() 221 { 222 StringBuffer str = new StringBuffer ("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 msg) 236 { 237 ORBUtility.dprint("ByteBufferWithInfo", msg); 238 } 239 } 240 | Popular Tags |