/** * 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); }
/** * 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); }
/** * Initialization common to all constructors. * * @param blockSize the block size to use * @param recordSize the record size to use */ private void initialize(int blockSize, int recordSize) { this.debug = false; this.blockSize = blockSize; this.recordSize = recordSize; this.recsPerBlock = (this.blockSize / this.recordSize); this.blockBuffer = BufferPool.getByteArray(this.blockSize); if (this.inStream != null) { this.currBlkIdx = -1; this.currRecIdx = this.recsPerBlock; } else { this.currBlkIdx = 0; this.currRecIdx = 0; } }
/** * Constructor for TarInputStream. * @param os the output stream to use * @param blockSize the block size to use * @param recordSize the record size to use */ public TarOutputStream(OutputStream os, int blockSize, int recordSize) { super(os); this.buffer = new TarBuffer(os, blockSize, recordSize); this.debug = false; this.assemLen = 0; this.assemBuf = BufferPool.getByteArray(recordSize); this.recordBuf = BufferPool.getByteArray(recordSize); this.oneBuf = BufferPool.getByteArray(1); }
/** * 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()); }
/** * Creates a new <code>TarInputStream</code> over the specified input stream, using the specified * block size, record size and start offset. * * @param is the input stream to use * @param blockSize the block size to use * @param recordSize the record size to use * @param entryOffset offset from the start of the archive to an entry. Must be a multiple of recordSize, or * <code>0</code> to start at the first entry. * @throws IOException if an error ocurred while initializing the stream */ public TarInputStream(InputStream is, int blockSize, int recordSize, long entryOffset) throws IOException { this.buffer = new TarBuffer(is, blockSize, recordSize); this.recordBuf = BufferPool.getByteArray(buffer.getRecordSize()); this.nameBuf = BufferPool.getByteArray(NAME_BUFFER_SIZE); this.oneBuf = BufferPool.getByteArray(1); this.debug = false; this.hasHitEOF = false; if(entryOffset>0) { if((entryOffset%recordSize)!=0) throw new IllegalArgumentException("entryOffset ("+entryOffset+") is not a multiple of recordSize ("+recordSize+")"); skipBytes(entryOffset); } }
/** * 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); } } }
byte buffer[] = BufferPool.getByteArray(bufferSize);
/** * 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); } }
byte[] buffer = BufferPool.getByteArray(IsoUtil.MODE1_2048);
byte[] skipBuf = BufferPool.getByteArray(SKIP_BYTE_SIZE); // Use a constant buffer size to always reuse the same instance try { StreamUtils.readFully(in, skipBuf, 0, (int)(offset-this.offset));
byte buffer[] = BufferPool.getByteArray(bufferSize);
byte[] buf = BufferPool.getByteArray(MAX_EOCD_SIZE); try {
final byte[] deflaterBuf = BufferPool.getByteArray(DEFAULT_DEFLATER_BUFFER_SIZE); ZipEntryOutputStream zeos = new DeflatedOutputStream(raos, new Deflater(DEFAULT_DEFLATER_COMPRESSION, true), deflaterBuf) {
byte buffer[] = BufferPool.getByteArray(); int bufferSize = buffer.length;