1 package org.jgroups.util; 2 3 import java.util.concurrent.ArrayBlockingQueue ; 4 import java.util.concurrent.BlockingQueue ; 5 6 13 public class OutgoingBufferPool { 14 private BlockingQueue <ExposedDataOutputStream> buffers; 15 16 17 public OutgoingBufferPool(int capacity) { 18 buffers=new ArrayBlockingQueue <ExposedDataOutputStream>(capacity); 19 for(int i=0; i < capacity; i++) { 20 ExposedByteArrayOutputStream out_stream=new ExposedByteArrayOutputStream(1024); 21 ExposedDataOutputStream dos=new ExposedDataOutputStream(out_stream); 22 try { 23 buffers.put(dos); 24 } 25 catch(InterruptedException e) { 26 Thread.currentThread().interrupt(); 27 return; 28 } 29 } 30 } 31 32 33 public ExposedDataOutputStream take() throws InterruptedException { 34 return buffers.take(); 35 } 36 37 public void put(ExposedDataOutputStream buf) throws InterruptedException { 38 ((ExposedByteArrayOutputStream)buf.getOutputStream()).reset(); 39 buf.reset(); 40 buffers.put(buf); 41 } 42 43 44 public String dumpStats() { 45 ExposedByteArrayOutputStream stream; 46 StringBuilder sb=new StringBuilder (); 47 sb.append(buffers.size()).append(" elements, capacities:\n"); 48 for(ExposedDataOutputStream buf: buffers) { 49 stream=(ExposedByteArrayOutputStream)buf.getOutputStream(); 50 sb.append("size=").append(stream.size()).append(", capacity=").append(stream.getCapacity()).append(")\n"); 51 } 52 return sb.toString(); 53 } 54 55 56 } 57 | Popular Tags |