/** * This method reads the given InputStream until the End Of File is reached, discarding all the data that is read * in the process. It is noteworthy that this method does <b>not</b> close the stream. * * @param in the InputStream to read * @param bufferSize size of the read buffer * @throws IOException if an I/O error occurs */ public static void readUntilEOF(InputStream in, int bufferSize) throws IOException { // Use BufferPool to avoid excessive memory allocation and garbage collection byte buffer[] = BufferPool.getByteArray(bufferSize); try { int nbRead; while(true) { nbRead = in.read(buffer, 0, buffer.length); if(nbRead==-1) break; } } finally { BufferPool.releaseByteArray(buffer); } } }
/** * Convenience method that has the same effect as calling {@link #getByteArray(int)} with * a length equal to {@link #getDefaultBufferSize()}. * * @return a byte array with a length of {@link #getDefaultBufferSize()} */ public static synchronized byte[] getByteArray() { return getByteArray(getDefaultBufferSize()); }
/** * 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()); }
/** * Convenience method that has the same effect as calling {@link #getByteBuffer(int)} with * a buffer capacity of {@link #getDefaultBufferSize()}. * * @return a ByteBuffer with a capacity equal to {@link #getDefaultBufferSize()} */ public static synchronized ByteBuffer getByteBuffer() { return getByteBuffer(getDefaultBufferSize()); }
/** * Convenience method that has the same effect as calling {@link #getCharArray(int)} with * a length equal to {@link #getDefaultBufferSize()}. * * @return a char array with a length of {@link #getDefaultBufferSize()} */ public static synchronized char[] getCharArray() { return getCharArray(getDefaultBufferSize()); }
int originalBufferCount = BufferPool.getBufferCount(factory); long originalPoolSize = BufferPool.getPoolSize(); 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); assert originalPoolSize == BufferPool.getPoolSize(); assert !BufferPool.containsBuffer(buffer1, factory); assert !BufferPool.containsBuffer(buffer2, factory); assert !BufferPool.containsBuffer(buffer3, factory); BufferPool.releaseBuffer(buffer3, factory); 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); assert BufferPool.releaseBuffer(buffers[b], factory); assert BufferPool.containsBuffer(buffers[b], factory); assertBufferCount(originalBufferCount+(b+1), factory);
/** * This method is overridden to release the internal buffer if {@link #close()} has not been called, to avoid any * memory leak. */ @Override protected void finalize() throws Throwable { // If this stream hasn't been closed, release the buffer before finalizing the object if(buffer!=null) BufferPool.releaseByteArray(buffer); super.finalize(); } }
/** * This method is a shorthand for {@link #copyStream(java.io.InputStream, java.io.OutputStream, int)} called with a * {@link BufferPool#getDefaultBufferSize() default buffer size}. * * @param in the InputStream to read from * @param out the OutputStream to write to * @return the number of bytes that were copied * @throws FileTransferException if something went wrong while reading from or writing to one of the provided streams */ public static long copyStream(InputStream in, OutputStream out) throws FileTransferException { return copyStream(in, out, BufferPool.getDefaultBufferSize()); }
/** * Creates a new <code>BufferedRandomOutputStream</code> on top of the given {@link RandomAccessOutputStream}. * An internal buffer of the specified size is created. * * @param raos the underlying RandomAccessOutputStream used by this buffered output stream * @param size size of the buffer in bytes */ public BufferedRandomOutputStream(RandomAccessOutputStream raos, int size) { this.raos = raos; this.buffer = BufferPool.getByteArray(size); }
/** * Convenience method that has the same effect as calling {@link #getCharBuffer(int)} with * a buffer capacity of {@link #getDefaultBufferSize()}. * * @return a CharBuffer with a capacity equal to {@link #getDefaultBufferSize()} */ public static synchronized CharBuffer getCharBuffer() { return getCharBuffer(getDefaultBufferSize()); }
/** * Makes the given buffer available for further calls to {@link #getByteBuffer(int)} with the same buffer capacity. * Returns <code>true</code> if the buffer was added to the pool, <code>false</code> if the buffer was already in * the pool. * * <p>After calling this method, the given buffer instance <b>must not be used</b>, otherwise it could get * corrupted if other threads were using it.</p> * * @param buffer the buffer instance to make available for further use * @return <code>true</code> if the buffer was added to the pool, <code>false</code> if the buffer was already in the pool * @throws IllegalArgumentException if specified buffer is null */ public static synchronized boolean releaseByteBuffer(ByteBuffer buffer) { return releaseBuffer(buffer, new ByteBufferFactory()); }
/** * 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); }
public LocalRandomAccessInputStream(FileChannel channel) { this.channel = channel; this.bb = BufferPool.getByteBuffer(); }
char buffer[] = BufferPool.getCharArray(bufferSize); try { BufferPool.releaseCharArray(buffer);
/** * Asserts that BufferPool contains <code>expectedCount</code> buffers of the kind corresponding to the factory. * * @param expectedCount the expected number of buffers * @param factory the factory corresponding to the kind of buffer to count */ private void assertBufferCount(int expectedCount, BufferPool.BufferFactory factory) { assert expectedCount == BufferPool.getBufferCount(factory); } }
/** * Closes this stream. Calls the TarBuffer's close() method. * @throws IOException on error */ @Override public void close() throws IOException { if (!closed) { try { buffer.close(); } finally { BufferPool.releaseByteArray(recordBuf); BufferPool.releaseByteArray(nameBuf); BufferPool.releaseByteArray(oneBuf); closed = true; } } }
/** * This method is a shorthand for {@link #readUntilEOF(java.io.InputStream, int)} called with a * {@link BufferPool#getDefaultBufferSize default buffer size}. * * @param in the InputStream to read * @throws IOException if an I/O error occurs */ public static void readUntilEOF(InputStream in) throws IOException { readUntilEOF(in, BufferPool.getDefaultBufferSize()); }
/** * Creates a new <code>ZipOutputStream</code> that writes Zip-compressed data to the given <code>OutputStream</code>. * If a {@link RandomAccessOutputStream} is supplied, the Zip entries will be written without data descriptor, * which will yield a slightly smaller file. * * @param out the underlying OutputStream stream where compressed data is written to */ public ZipOutputStream(OutputStream out) { this.out = out; this.hasRandomAccess = out instanceof RandomAccessOutputStream; // Use BufferPool to avoid excessive memory allocation and garbage collection. deflaterBuf = BufferPool.getByteArray(DEFAULT_DEFLATER_BUFFER_SIZE); }
/** * Makes the given buffer available for further calls to {@link #getCharBuffer(int)} with the same buffer capacity. * Returns <code>true</code> if the buffer was added to the pool, <code>false</code> if the buffer was already in * the pool. * * <p>After calling this method, the given buffer instance <b>must not be used</b>, otherwise it could get * corrupted if other threads were using it.</p> * * @param buffer the buffer instance to make available for further use * @return <code>true</code> if the buffer was added to the pool, <code>false</code> if the buffer was already in the pool * @throws IllegalArgumentException if specified buffer is null */ public static synchronized boolean releaseCharBuffer(CharBuffer buffer) { return releaseBuffer(buffer, new CharBufferFactory()); }
/** * 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); }