2424//--------------------------------------
2525package org .xerial .snappy ;
2626
27+ import org .xerial .snappy .buffer .BufferAllocatorFactory ;
28+ import org .xerial .snappy .buffer .BufferAllocator ;
29+ import org .xerial .snappy .buffer .CachedBufferAllocator ;
30+
2731import java .io .IOException ;
2832import java .io .OutputStream ;
2933
@@ -56,9 +60,11 @@ public class SnappyOutputStream extends OutputStream {
5660 static final int DEFAULT_BLOCK_SIZE = 32 * 1024 ; // Use 32kb for the default block size
5761
5862 protected final OutputStream out ;
59-
60- private final BufferRecycler recycler ;
6163 private final int blockSize ;
64+
65+ private final BufferAllocator inputBufferAllocator ;
66+ private final BufferAllocator outputBufferAllocator ;
67+
6268 protected final byte [] inputBuffer ;
6369 protected final byte [] outputBuffer ;
6470 private int inputCursor = 0 ;
@@ -74,14 +80,25 @@ public SnappyOutputStream(OutputStream out) {
7480 * @throws IOException
7581 */
7682 public SnappyOutputStream (OutputStream out , int blockSize ) {
83+ this (out , blockSize , CachedBufferAllocator .factory );
84+ }
85+
86+ public SnappyOutputStream (OutputStream out , int blockSize , BufferAllocatorFactory bufferAllocatorFactory ) {
7787 this .out = out ;
78- this .recycler = BufferRecycler .instance ();
7988 this .blockSize = Math .max (MIN_BLOCK_SIZE , blockSize );
80- inputBuffer = recycler .allocInputBuffer (this .blockSize );
81- outputBuffer = recycler .allocOutputBuffer (SnappyCodec .HEADER_SIZE + 4 + Snappy .maxCompressedLength (this .blockSize ));
89+ int inputSize = blockSize ;
90+ int outputSize = SnappyCodec .HEADER_SIZE + 4 + Snappy .maxCompressedLength (blockSize );
91+
92+ this .inputBufferAllocator = bufferAllocatorFactory .getBufferAllocator (inputSize );
93+ this .outputBufferAllocator = bufferAllocatorFactory .getBufferAllocator (outputSize );
94+
95+ inputBuffer = inputBufferAllocator .allocate (inputSize );
96+ outputBuffer = inputBufferAllocator .allocate (outputSize );
97+
8298 outputCursor = SnappyCodec .currentHeader .writeHeader (outputBuffer , 0 );
8399 }
84100
101+
85102 /* (non-Javadoc)
86103 * @see java.io.OutputStream#write(byte[], int, int)
87104 */
@@ -265,9 +282,9 @@ public void flush() throws IOException {
265282
266283 static void writeInt (byte [] dst , int offset , int v ) {
267284 dst [offset ] = (byte ) ((v >> 24 ) & 0xFF );
268- dst [offset + 1 ] = (byte ) ((v >> 16 ) & 0xFF );
269- dst [offset + 2 ] = (byte ) ((v >> 8 ) & 0xFF );
270- dst [offset + 3 ] = (byte ) ((v >> 0 ) & 0xFF );
285+ dst [offset + 1 ] = (byte ) ((v >> 16 ) & 0xFF );
286+ dst [offset + 2 ] = (byte ) ((v >> 8 ) & 0xFF );
287+ dst [offset + 3 ] = (byte ) ((v >> 0 ) & 0xFF );
271288 }
272289
273290 static int readInt (byte [] buffer , int pos ) {
@@ -312,10 +329,9 @@ public void close() throws IOException {
312329 try {
313330 flush ();
314331 out .close ();
315- }
316- finally {
317- recycler .releaseInputBuffer (inputBuffer );
318- recycler .releaseOutputBuffer (outputBuffer );
332+ } finally {
333+ inputBufferAllocator .release (inputBuffer );
334+ outputBufferAllocator .release (outputBuffer );
319335 }
320336 }
321337
0 commit comments