111 lines
3.8 KiB
Java
111 lines
3.8 KiB
Java
/*
|
|
* Copyright (c) 2008 Declarative Engineering LLC.
|
|
* All rights reserved. This program and the accompanying materials
|
|
* are made available under the terms of the Declarative Engineering LLC
|
|
* verson 1 which accompanies this distribution, and is available at
|
|
* http://declarativeengineering.com/legal/DE_Developer_License_v1.txt
|
|
*/
|
|
package com.de22.orb;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import com.common.util.IList;
|
|
import com.common.util.LiteList;
|
|
import com.common.util.optimized.IntObjectHashMap;
|
|
|
|
/**
|
|
* This class provides the orb an opportunity to reduce GC by reusing a majority of the buffers, but also increases overhead due to additional synchronization.
|
|
* The caller must synchronize on the repository prior to checking out or checking in any buffers.
|
|
*/
|
|
public class BufferRepository {
|
|
/** The maximum number of buffers that will be cached. */
|
|
private final static int maximumCachedBuffers = 100;
|
|
/** The size of the message buffers. All cached message buffers will be of the same size. */
|
|
private final static int messageBufferSize = 100000; //Note: This must be larger than the encryption buffer sizes.//
|
|
/** The only instance of this class. */
|
|
private final static BufferRepository singleton = new BufferRepository();
|
|
|
|
/** The collection of currently cached message buffers. */
|
|
private LiteList cachedMessageBuffers = new LiteList(maximumCachedBuffers);
|
|
/** The collection of collections of cached byte buffers. The byte buffer lists are mapped by their size. */
|
|
private IntObjectHashMap cachedByteBuffers = new IntObjectHashMap(20);
|
|
/**
|
|
* Gets the one and only instance of this class.
|
|
* @return The only instance of this class.
|
|
*/
|
|
public static BufferRepository getSingleton() {
|
|
return singleton;
|
|
}//getSingleton()//
|
|
/**
|
|
* BufferRepository constructor.
|
|
*/
|
|
private BufferRepository() {
|
|
}//BufferRepository()//
|
|
/**
|
|
* Gets the size of the message buffers.
|
|
* <p>Note: The caller does not need to synchronize on this since it must either be final, or set before the orb is started and never changed.</p>
|
|
* @return The message buffer size.
|
|
*/
|
|
public int getMessageBufferSize() {
|
|
return messageBufferSize;
|
|
}//getMessageBufferSize()//
|
|
/**
|
|
* Checks out a message buffer.
|
|
* @return The buffer checked out, never null.
|
|
*/
|
|
public byte[] checkOutMessageBuffer() {
|
|
byte[] result;
|
|
|
|
if(cachedMessageBuffers.getSize() > 0) {
|
|
result = (byte[]) cachedMessageBuffers.remove(cachedMessageBuffers.getSize() - 1);
|
|
}//if//
|
|
else {
|
|
result = new byte[messageBufferSize];
|
|
}//else//
|
|
|
|
return result;
|
|
}//checkOutMessageBuffer()//
|
|
/**
|
|
* Checks in a previously checked out message buffer.
|
|
* @param buffer The buffer being checked in for reuse.
|
|
*/
|
|
public void checkInMessageBuffer(byte[] buffer) {
|
|
if(buffer.length == messageBufferSize && cachedMessageBuffers.getSize() < maximumCachedBuffers) {
|
|
cachedMessageBuffers.add(buffer);
|
|
}//if//
|
|
}//checkInMessageBuffer()//
|
|
/**
|
|
* Checks out a byte buffer.
|
|
* @param size The size of the required byte buffer.
|
|
* @return The buffer checked out, never null.
|
|
*/
|
|
public ByteBuffer checkOutByteBuffer(int size) {
|
|
IList buffers = (IList) cachedByteBuffers.get(size);
|
|
ByteBuffer result;
|
|
|
|
if(buffers != null && buffers.getSize() > 0) {
|
|
result = (ByteBuffer) buffers.remove(buffers.getSize() - 1);
|
|
}//if//
|
|
else {
|
|
result = ByteBuffer.allocate(size);
|
|
}//else//
|
|
|
|
return result;
|
|
}//checkOutByteBuffer()//
|
|
/**
|
|
* Checks in a previously checked out byte buffer.
|
|
* @param buffer The buffer being checked in for reuse.
|
|
*/
|
|
public void checkInByteBuffer(ByteBuffer buffer) {
|
|
IList buffers = (IList) cachedByteBuffers.get(buffer.capacity());
|
|
|
|
if(buffers == null) {
|
|
buffers = new LiteList(maximumCachedBuffers);
|
|
buffers.add(buffer);
|
|
cachedByteBuffers.put(buffer.capacity(), buffers);
|
|
}//if//
|
|
else if(buffers.getSize() < maximumCachedBuffers) {
|
|
buffers.add(buffer);
|
|
}//else if//
|
|
}//checkInByteBuffer()//
|
|
}//BufferRepository// |