/** * Convenience method that has the same effect as calling {@link #getBuffer(com.mucommander.commons.io.BufferPool.BufferFactory, int)} * with a size equal to {@link #getDefaultBufferSize()}. * * @param factory BufferFactory used to identify the target buffer class and create a new buffer (if necessary) * @return a buffer with a size equal to {@link #getDefaultBufferSize()} */ public static synchronized Object getBuffer(BufferFactory factory) { return getBuffer(factory, getDefaultBufferSize()); }
Object buffer1 = BufferPool.getBuffer(factory, TEST_BUFFER_SIZE_1); assertBufferSize(buffer1, factory, TEST_BUFFER_SIZE_1); Object buffer2 = BufferPool.getBuffer(factory, TEST_BUFFER_SIZE_1); assertBufferSize(buffer2, factory, TEST_BUFFER_SIZE_1); assert buffer2!=buffer1; Object buffer3 = BufferPool.getBuffer(factory, TEST_BUFFER_SIZE_2); assertBufferSize(buffer3, factory, TEST_BUFFER_SIZE_2); assertBufferCount(originalBufferCount+1, factory); assert BufferPool.containsBuffer(buffer3, factory); assert buffer3==BufferPool.getBuffer(factory, TEST_BUFFER_SIZE_2); assertBufferCount(originalBufferCount, factory); assert !BufferPool.containsBuffer(buffer3, factory); BufferPool.getBuffer(factory, getBufferLength(buffers[b], factory)); assertBufferCount(originalBufferCount+(3-b-1), factory); assertBufferSize(BufferPool.getBuffer(factory), factory, BufferPool.INITIAL_DEFAULT_BUFFER_SIZE); assertBufferSize(BufferPool.getBuffer(factory), factory, TEST_BUFFER_SIZE_1); BufferPool.setMaxPoolSize(TEST_MAX_POOL_SIZE); assert TEST_MAX_POOL_SIZE == BufferPool.getMaxPoolSize(); long bufferSize = getBufferSize(BufferPool.getBuffer(factory, TEST_BUFFER_SIZE_1), factory); // in bytes int nbBuffers = (int)(TEST_MAX_POOL_SIZE/bufferSize); buffers = new Object[nbBuffers]; for(int i=0; i<nbBuffers; i++)
/** * Returns a ByteBuffer of the specified capacity. This method first checks if a ByteBuffer instance of the * specified capacity exists in the pool. If one is found, it is removed from the pool and returned. If not, * a new instance is created and returned. * * <p>This method won't return the same buffer instance until it has been released with * {@link #releaseByteBuffer(ByteBuffer)}.</p> * * <p>This method is a shorthand for {@link #getBuffer(com.mucommander.commons.io.BufferPool.BufferFactory,int)} called * with a {@link com.mucommander.commons.io.BufferPool.ByteBufferFactory} instance.</p>. * @param capacity capacity of the ByteBuffer * @return a ByteBuffer with the specified capacity */ public static synchronized ByteBuffer getByteBuffer(int capacity) { return (ByteBuffer)getBuffer(new ByteBufferFactory(), capacity); }
/** * Returns a CharBuffer of the specified capacity. This method first checks if a CharBuffer instance of the * specified capacity exists in the pool. If one is found, it is removed from the pool and returned. If not, * a new instance is created and returned. * * <p>This method won't return the same buffer instance until it has been released with * {@link #releaseCharBuffer(CharBuffer)}.</p> * * <p>This method is a shorthand for {@link #getBuffer(com.mucommander.commons.io.BufferPool.BufferFactory,int)} called * with a {@link com.mucommander.commons.io.BufferPool.CharBufferFactory} instance.</p>. * @param capacity capacity of the CharBuffer * @return a CharBuffer with the specified capacity */ public static synchronized CharBuffer getCharBuffer(int capacity) { return (CharBuffer)getBuffer(new CharBufferFactory(), capacity); }
/** * Returns a byte array of the specified length. This method first checks if a byte array of the specified length * exists in the pool. If one is found, it is removed from the pool and returned. If not, a new instance is created * and returned. * * <p>This method won't return the same buffer instance until it has been released with * {@link #releaseByteArray(byte[])}.</p> * * <p>This method is a shorthand for {@link #getBuffer(com.mucommander.commons.io.BufferPool.BufferFactory,int)} called * with a {@link com.mucommander.commons.io.BufferPool.ByteArrayFactory} instance.</p>. * * @param length length of the byte array * @return a byte array of the specified size */ public static synchronized byte[] getByteArray(int length) { return (byte[])getBuffer(new ByteArrayFactory(), length); }
/** * Returns a char array of the specified length. This method first checks if a char array of the specified length * exists in the pool. If one is found, it is removed from the pool and returned. If not, a new instance is created * and returned. * * <p>This method won't return the same buffer instance until it has been released with * {@link #releaseCharArray(char[])}.</p> * * <p>This method is a shorthand for {@link #getBuffer(com.mucommander.commons.io.BufferPool.BufferFactory,int)} called * with a {@link com.mucommander.commons.io.BufferPool.CharArrayFactory} instance.</p>. * * @param length length of the char array * @return a char array of the specified length */ public static synchronized char[] getCharArray(int length) { return (char[])getBuffer(new CharArrayFactory(), length); }