001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.transport.udp;
018
019import java.nio.ByteBuffer;
020
021/**
022 * A simple implementation of {@link BufferPool} which does no pooling and just
023 * creates new buffers each time
024 * 
025 * 
026 */
027public class SimpleBufferPool implements ByteBufferPool {
028
029    private int defaultSize;
030    private boolean useDirect;
031
032    public SimpleBufferPool() {
033        this(false);
034    }
035
036    public SimpleBufferPool(boolean useDirect) {
037        this.useDirect = useDirect;
038    }
039
040    public synchronized ByteBuffer borrowBuffer() {
041        return createBuffer();
042    }
043
044    public void returnBuffer(ByteBuffer buffer) {
045    }
046
047    public void setDefaultSize(int defaultSize) {
048        this.defaultSize = defaultSize;
049    }
050
051    public boolean isUseDirect() {
052        return useDirect;
053    }
054
055    /**
056     * Sets whether direct buffers are used or not
057     */
058    public void setUseDirect(boolean useDirect) {
059        this.useDirect = useDirect;
060    }
061
062    public void start() throws Exception {
063    }
064
065    public void stop() throws Exception {
066    }
067
068    protected ByteBuffer createBuffer() {
069        if (useDirect) {
070            return ByteBuffer.allocateDirect(defaultSize);
071        } else {
072            return ByteBuffer.allocate(defaultSize);
073        }
074    }
075
076}