MessageBufferPool.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
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 }


rosjava_bootstrap
Author(s): Daniel Stonier , Damon Kohler
autogenerated on Fri Aug 28 2015 12:41:44