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 ByteBufferPool} which does no pooling and just
023 * creates new buffers each time
024 */
025public class SimpleBufferPool implements ByteBufferPool {
026
027    private int defaultSize;
028    private boolean useDirect;
029
030    public SimpleBufferPool() {
031        this(false);
032    }
033
034    public SimpleBufferPool(boolean useDirect) {
035        this.useDirect = useDirect;
036    }
037
038    @Override
039    public synchronized ByteBuffer borrowBuffer() {
040        return createBuffer();
041    }
042
043    @Override
044    public void returnBuffer(ByteBuffer buffer) {
045    }
046
047    @Override
048    public void setDefaultSize(int defaultSize) {
049        this.defaultSize = defaultSize;
050    }
051
052    public boolean isUseDirect() {
053        return useDirect;
054    }
055
056    /**
057     * Sets whether direct buffers are used or not
058     */
059    public void setUseDirect(boolean useDirect) {
060        this.useDirect = useDirect;
061    }
062
063    @Override
064    public void start() throws Exception {
065    }
066
067    @Override
068    public void stop() throws Exception {
069    }
070
071    protected ByteBuffer createBuffer() {
072        if (useDirect) {
073            return ByteBuffer.allocateDirect(defaultSize);
074        } else {
075            return ByteBuffer.allocate(defaultSize);
076        }
077    }
078}