Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package org.ros.internal.message;
00018
00019 import org.apache.commons.pool.ObjectPool;
00020 import org.apache.commons.pool.PoolableObjectFactory;
00021 import org.apache.commons.pool.impl.StackObjectPool;
00022 import org.jboss.netty.buffer.ChannelBuffer;
00023 import org.ros.exception.RosMessageRuntimeException;
00024
00033 public class MessageBufferPool {
00034
00035 private final ObjectPool<ChannelBuffer> pool;
00036
00037 public MessageBufferPool() {
00038 pool = new StackObjectPool<ChannelBuffer>(new PoolableObjectFactory<ChannelBuffer>() {
00039 @Override
00040 public ChannelBuffer makeObject() throws Exception {
00041 return MessageBuffers.dynamicBuffer();
00042 }
00043
00044 @Override
00045 public void destroyObject(ChannelBuffer channelBuffer) throws Exception {
00046 }
00047
00048 @Override
00049 public boolean validateObject(ChannelBuffer channelBuffer) {
00050 return true;
00051 }
00052
00053 @Override
00054 public void activateObject(ChannelBuffer channelBuffer) throws Exception {
00055 }
00056
00057 @Override
00058 public void passivateObject(ChannelBuffer channelBuffer) throws Exception {
00059 channelBuffer.clear();
00060 }
00061 });
00062 }
00063
00070 public ChannelBuffer acquire() {
00071 try {
00072 return pool.borrowObject();
00073 } catch (Exception e) {
00074 throw new RosMessageRuntimeException(e);
00075 }
00076 }
00077
00084 public void release(ChannelBuffer channelBuffer) {
00085 try {
00086 pool.returnObject(channelBuffer);
00087 } catch (Exception e) {
00088 throw new RosMessageRuntimeException(e);
00089 }
00090 }
00091 }