泛型通用池
https://commons.apache.org/proper/commons-pool/examples.html
可用作<ftpClient> 连接池
import java.io.Reader;
import java.io.IOException;
public class ReaderUtil {
public ReaderUtil() {
}
/**
* Dumps the contents of the {@link Reader} to a
* String, closing the {@link Reader} when done.
*/
public String readToString(Reader in) throws IOException {
StringBuffer buf = new StringBuffer();
try {
for(int c = in.read(); c != -1; c = in.read()) {
buf.append((char)c);
}
return buf.toString();
} catch(IOException e) {
throw e;
} finally {
try {
in.close();
} catch(Exception e) {
// ignored
}
}
}
}
import java.io.IOException;
import java.io.Reader;
import org.apache.commons.pool2.ObjectPool;
public class ReaderUtil {
private ObjectPool<StringBuffer> pool;
public ReaderUtil(ObjectPool<StringBuffer> pool) {
this.pool = pool;
}
/**
* Dumps the contents of the {@link Reader} to a String, closing the {@link Reader} when done.
*/
public String readToString(Reader in)
throws IOException {
StringBuffer buf = null;
try {
buf = pool.borrowObject();
for (int c = in.read(); c != -1; c = in.read()) {
buf.append((char) c);
}
return buf.toString();
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("Unable to borrow buffer from pool" + e.toString());
} finally {
try {
in.close();
} catch (Exception e) {
// ignored
}
try {
if (null != buf) {
pool.returnObject(buf);
}
} catch (Exception e) {
// ignored
}
}
}
}
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
public class StringBufferFactory
extends BasePooledObjectFactory<StringBuffer> {
@Override
public StringBuffer create() {
return new StringBuffer();
}
/**
* Use the default PooledObject implementation.
*/
@Override
public PooledObject<StringBuffer> wrap(StringBuffer buffer) {
return new DefaultPooledObject<StringBuffer>(buffer);
}
/**
* When an object is returned to the pool, clear the buffer.
*/
@Override
public void passivateObject(PooledObject<StringBuffer> pooledObject) {
pooledObject.getObject().setLength(0);
}
// for all other methods, the no-op implementation
// in BasePooledObjectFactory will suffice
}
We can, for example, use this factory with the GenericObjectPool to instantiate our ReaderUtil as follows:
ReaderUtil readerUtil = new ReaderUtil(new GenericObjectPool<StringBuffer>(new StringBufferFactory()));