1 7 8 package com.sun.corba.se.impl.transport; 9 10 import java.nio.ByteBuffer ; 11 import java.util.ArrayList ; 12 13 import com.sun.corba.se.spi.orb.ORB; 14 15 import com.sun.corba.se.pept.transport.ByteBufferPool; 16 17 20 21 public class ByteBufferPoolImpl implements ByteBufferPool 22 { 23 private ORB itsOrb; 24 private int itsByteBufferSize; 25 private ArrayList itsPool; 26 private int itsObjectCounter = 0; 27 private boolean debug; 28 29 public ByteBufferPoolImpl(ORB theORB) 32 { 33 itsByteBufferSize = theORB.getORBData().getGIOPFragmentSize(); 34 itsPool = new ArrayList (); 35 itsOrb = theORB; 36 debug = theORB.transportDebugFlag; 37 } 38 39 45 46 public ByteBuffer getByteBuffer(int theAskSize) 56 { 57 ByteBuffer abb = null; 58 59 if ((theAskSize <= itsByteBufferSize) && 60 !itsOrb.getORBData().disableDirectByteBufferUse()) 61 { 62 int poolSize; 64 synchronized (itsPool) 65 { 66 poolSize = itsPool.size(); 67 if (poolSize > 0) 68 { 69 abb = (ByteBuffer )itsPool.remove(poolSize - 1); 70 71 abb.clear(); 73 } 74 } 75 76 if (poolSize <= 0) 81 { 82 abb = ByteBuffer.allocateDirect(itsByteBufferSize); 83 } 84 85 itsObjectCounter++; 89 } 90 else 91 { 92 abb = ByteBuffer.allocate(theAskSize); 95 } 96 97 return abb; 98 } 99 100 101 112 113 public void releaseByteBuffer(ByteBuffer thebb) 121 { 122 if (thebb.isDirect()) 123 { 124 synchronized (itsPool) 125 { 126 boolean refInPool = false; 129 int bbAddr = 0; 130 131 if (debug) 132 { 133 136 for (int i = 0; i < itsPool.size() && refInPool == false; i++) 137 { 138 ByteBuffer tmpbb = (ByteBuffer )itsPool.get(i); 139 if (thebb == tmpbb) 140 { 141 refInPool = true; 142 bbAddr = System.identityHashCode(thebb); 143 } 144 } 145 146 } 147 148 if (refInPool == false || debug == false) 151 { 152 itsPool.add(thebb); 154 } 155 else { 157 String threadName = Thread.currentThread().getName(); 158 Throwable t = 159 new Throwable (threadName + 160 ": Duplicate ByteBuffer reference (" + 161 bbAddr + ")"); 162 t.printStackTrace(System.out); 163 } 164 } 165 166 itsObjectCounter--; 170 } 171 else 172 { 173 thebb = null; 175 } 176 } 177 178 179 public int activeCount() 184 { 185 return itsObjectCounter; 186 } 187 } 188 189 | Popular Tags |