/** * 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(); } }
/** * 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; } } }
/** * Closes this output stream and releases any system resources associated with the stream. * * @exception IOException if an I/O error occurs. */ @Override public void close() throws IOException { finish(); if(deflaterBuf !=null) { // Only if close() has not already been called already BufferPool.releaseByteArray(deflaterBuf); deflaterBuf = null; } out.close(); }
/** * Ends the TAR archive and closes the underlying OutputStream. * This means that finish() is called followed by calling the * TarBuffer's close(). * @throws IOException on error */ @Override public void close() throws IOException { if (!closed) { try { finish(); buffer.close(); } finally { BufferPool.releaseByteArray(assemBuf); BufferPool.releaseByteArray(recordBuf); BufferPool.releaseByteArray(oneBuf); closed = true; } } }
/** * 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); } } }
BufferPool.releaseByteArray(blockBuffer); blockBuffer = null;
BufferPool.releaseByteArray(buffer);
/** * This method is overridden to release the internal buffer when this stream is closed. */ @Override public synchronized void close() throws IOException { if(buffer!=null) { // buffer is null if close() was already called try { flush(); } catch(IOException e) { // Continue anyway } // Release the buffer BufferPool.releaseByteArray(buffer); buffer = null; } raos.close(); }
/** * Shorthand for {@link #copyStream(InputStream, OutputStream, byte[], long)} called with a buffer of the specified * size retrieved from {@link BufferPool}. * * @param in the InputStream to read from * @param out the OutputStream to write to * @param bufferSize size of the buffer to use, in bytes * @param length number of bytes to copy from InputStream * @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, int bufferSize, long length) throws FileTransferException { // Use BufferPool to reuse any available buffer of the same size byte buffer[] = BufferPool.getByteArray(bufferSize); try { return copyStream(in, out, buffer, length); } finally { // Make the buffer available for further use BufferPool.releaseByteArray(buffer); } }
/** * Try and detect the character encoding in which the bytes contained by the given <code>InputStream</code> are * encoded, and returns the best guess or <code>null</code> if there is none (not enough data or confidence). * Note that the returned character encoding may or may not be available on the Java runtime -- use * <code>java.nio.Charset#isSupported(String)</code> to determine if it is available. * * <p>A maximum of {@link #MAX_RECOMMENDED_BYTE_SIZE} will be read from the <code>InputStream</code>. The * stream will not be closed and will not be repositionned after the bytes have been read. It is up to the calling * method to use the <code>InputStream#mark()</code> and <code>InputStream#reset()</code> methods (if supported) * or reopen the stream if needed. * </p> * * @param in the InputStream that supplies the bytes * @return the best guess at the character encoding, null if there is none (not enough data or confidence) * @throws IOException if an error occurred while reading the stream */ public static String detectEncoding(InputStream in) throws IOException { byte buf[] = BufferPool.getByteArray(MAX_RECOMMENDED_BYTE_SIZE); try { return detectEncoding(buf, 0, StreamUtils.readUpTo(in, buf)); } finally { BufferPool.releaseByteArray(buf); } }
BufferPool.releaseByteArray(buffer);
BufferPool.releaseByteArray(skipBuf);
BufferPool.releaseByteArray(buffer);
BufferPool.releaseByteArray(deflaterBuf);
BufferPool.releaseByteArray(buf);
BufferPool.releaseByteArray(buffer);