1 19 20 21 27 42 package com.sslexplorer.core; 43 44 45 46 51 public class ByteArrayPool 52 { 53 public static final int __POOL_SIZE= 54 Integer.getInteger("org.mortbay.util.ByteArrayPool.pool_size",8).intValue(); 55 56 public static final ThreadLocal __pools=new BAThreadLocal(); 57 public static int __slot; 58 59 60 64 public static byte[] getByteArray(int size) 65 { 66 byte[][] pool = (byte[][])__pools.get(); 67 boolean full=true; 68 for (int i=pool.length;i-->0;) 69 { 70 if (pool[i]!=null && pool[i].length==size) 71 { 72 byte[]b = pool[i]; 73 pool[i]=null; 74 return b; 75 } 76 else 77 full=false; 78 } 79 80 if (full) 81 for (int i=pool.length;i-->0;) 82 pool[i]=null; 83 84 return new byte[size]; 85 } 86 87 88 public static byte[] getByteArrayAtLeast(int minSize) 89 { 90 byte[][] pool = (byte[][])__pools.get(); 91 for (int i=pool.length;i-->0;) 92 { 93 if (pool[i]!=null && pool[i].length>=minSize) 94 { 95 byte[]b = pool[i]; 96 pool[i]=null; 97 return b; 98 } 99 } 100 101 return new byte[minSize]; 102 } 103 104 105 106 public static void returnByteArray(final byte[] b) 107 { 108 if (b==null) 109 return; 110 111 byte[][] pool = (byte[][])__pools.get(); 112 for (int i=pool.length;i-->0;) 113 { 114 if (pool[i]==null) 115 { 116 pool[i]=b; 117 return; 118 } 119 } 120 121 int s = __slot++; 123 if (s<0)s=-s; 124 pool[s%pool.length]=b; 125 } 126 127 128 129 130 private static final class BAThreadLocal extends ThreadLocal 131 { 132 protected Object initialValue() 133 { 134 return new byte[__POOL_SIZE][]; 135 } 136 } 137 } 138 | Popular Tags |