|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.mucommander.io.BufferPool
public class BufferPool
This class allows to share and reuse byte buffers to avoid excessive memory allocation and garbage collection. Methods that use byte buffers and that are called repeatedly will benefit from using this class.
This class works with two types of byte buffers indifferently:
byte[])java.nio.ByteBufferUsage of this class is similar to malloc/free:
#get*Buffer(int) to retrieve a buffer instance of a specified size#release*Buffer(byte[]) to make this buffer available for
subsequent calls to #get*Buffer(int). Failing to call this method will prevent the buffer from being
used again and from being garbage-collected.Note: this class is thread safe and thus can safely be used by concurrent threads.
StreamUtils| Nested Class Summary | |
|---|---|
static class |
BufferPool.BufferContainer
Wraps a buffer instance and provides information about the wrapped buffer. |
static class |
BufferPool.BufferFactory
A BufferFactory is responsible for creating buffer and BufferPool.BufferContainer instances, and for returning the buffer
Class. |
static class |
BufferPool.ByteArrayFactory
This class is a BufferPool.BufferFactory implementation for byte array (byte[]) buffers. |
static class |
BufferPool.ByteBufferFactory
This class is a BufferPool.BufferFactory implementation for java.nio.ByteBuffer buffers. |
static class |
BufferPool.CharArrayFactory
This class is a BufferPool.BufferFactory implementation for char array (char[]) buffers. |
| Field Summary | |
|---|---|
static int |
DEFAULT_BUFFER_SIZE
The default buffer size when not specified |
| Constructor Summary | |
|---|---|
BufferPool()
|
|
| Method Summary | |
|---|---|
static java.lang.Object |
getBuffer(int size,
BufferPool.BufferFactory factory)
Returns a byte array of the specified size. |
static byte[] |
getByteArray()
Convenience method that has the same effect as calling getByteArray(int) with
DEFAULT_BUFFER_SIZE. |
static byte[] |
getByteArray(int size)
Returns a byte array of the specified size. |
static java.nio.ByteBuffer |
getByteBuffer()
Convenience method that has the same effect as calling getByteBuffer(int) with
DEFAULT_BUFFER_SIZE. |
static java.nio.ByteBuffer |
getByteBuffer(int capacity)
Returns a ByteBuffer of the specified capacity. |
static char[] |
getCharArray()
Convenience method that has the same effect as calling getCharArray(int) with
DEFAULT_BUFFER_SIZE. |
static char[] |
getCharArray(int size)
Returns a char array of the specified size. |
static void |
releaseBuffer(java.lang.Object buffer,
BufferPool.BufferFactory factory)
Makes the given buffer available for further calls to getBuffer(int, BufferFactory) with the same buffer
size and factory. |
static void |
releaseByteArray(byte[] buffer)
Makes the given buffer available for further calls to getByteArray(int) with the same buffer size. |
static void |
releaseByteBuffer(java.nio.ByteBuffer buffer)
Makes the given buffer available for further calls to getByteBuffer(int) with the same buffer size. |
static void |
releaseCharArray(char[] buffer)
Makes the given buffer available for further calls to getCharArray(int) with the same buffer size. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final int DEFAULT_BUFFER_SIZE
| Constructor Detail |
|---|
public BufferPool()
| Method Detail |
|---|
public static byte[] getByteArray()
getByteArray(int) with
DEFAULT_BUFFER_SIZE.
public static byte[] getByteArray(int size)
This method won't return the same buffer instance until it has been released with
releaseByteArray(byte[]).
This method is a shorthand for getBuffer(int, com.mucommander.io.BufferPool.BufferFactory) called
with a BufferPool.ByteArrayFactory instance.
size - size of the byte array
public static char[] getCharArray()
getCharArray(int) with
DEFAULT_BUFFER_SIZE.
public static char[] getCharArray(int size)
This method won't return the same buffer instance until it has been released with
releaseCharArray(char[]).
This method is a shorthand for getBuffer(int, com.mucommander.io.BufferPool.BufferFactory) called
with a BufferPool.CharArrayFactory instance.
size - size of the char array
public static java.nio.ByteBuffer getByteBuffer()
getByteBuffer(int) with
DEFAULT_BUFFER_SIZE.
public static java.nio.ByteBuffer getByteBuffer(int capacity)
This method won't return the same buffer instance until it has been released with
releaseByteBuffer(ByteBuffer).
This method is a shorthand for getBuffer(int, com.mucommander.io.BufferPool.BufferFactory) called
with a BufferPool.ByteArrayFactory instance.
capacity - capacity of the ByteBuffer
public static java.lang.Object getBuffer(int size,
BufferPool.BufferFactory factory)
BufferPool.BufferFactory.newBuffer(int).
This method won't return the same buffer instance until it has been released with
releaseBuffer(Object, BufferFactory).
size - size of the bufferfactory - BufferFactory used to identify the target buffer class and create a new buffer (if necessary)
public static void releaseByteArray(byte[] buffer)
getByteArray(int) with the same buffer size.
Does nothing if the specified buffer already is in the pool. After calling this method, the given buffer
instance must not be used, otherwise it could get corrupted if some other threads use it.
buffer - the buffer instance to make available for further use
java.lang.IllegalArgumentException - if specified buffer is nullpublic static void releaseCharArray(char[] buffer)
getCharArray(int) with the same buffer size.
Does nothing if the specified buffer already is in the pool. After calling this method, the given buffer
instance must not be used, otherwise it could get corrupted if some other threads use it.
buffer - the buffer instance to make available for further use
java.lang.IllegalArgumentException - if specified buffer is nullpublic static void releaseByteBuffer(java.nio.ByteBuffer buffer)
getByteBuffer(int) with the same buffer size.
Does nothing if the specified buffer already is in the pool. After calling this method, the given buffer
instance must not be used, otherwise it could get corrupted if some other threads use it.
buffer - the buffer instance to make available for further use
java.lang.IllegalArgumentException - if specified buffer is null
public static void releaseBuffer(java.lang.Object buffer,
BufferPool.BufferFactory factory)
getBuffer(int, BufferFactory) with the same buffer
size and factory. Does nothing if the specified buffer already is in the pool.
After calling this method, the given buffer instance must not be used, otherwise it could get
corrupted if some other threads use it.
buffer - the buffer instance to make available for further usefactory - BufferFactory used create a buffer container
java.lang.IllegalArgumentException - if specified buffer is null
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
This file is part of muCommander - Copyright (C) 2002-2008 Maxence Bernard