From 1991ed2f1535c2401338c863c1150c15d1316729 Mon Sep 17 00:00:00 2001 From: "Taro L. Saito" Date: Wed, 30 Mar 2016 20:32:09 -0700 Subject: [PATCH] Use System.arraycopy for write(byte[]) apache/spark#12074 --- .../org/xerial/snappy/SnappyOutputStream.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/xerial/snappy/SnappyOutputStream.java b/src/main/java/org/xerial/snappy/SnappyOutputStream.java index 57ce25f8..08126795 100755 --- a/src/main/java/org/xerial/snappy/SnappyOutputStream.java +++ b/src/main/java/org/xerial/snappy/SnappyOutputStream.java @@ -109,10 +109,27 @@ public SnappyOutputStream(OutputStream out, int blockSize, BufferAllocatorFactor * @see java.io.OutputStream#write(byte[], int, int) */ @Override - public void write(byte[] b, int off, int len) + public void write(byte[] b, int byteOffset, int byteLength) throws IOException { - rawWrite(b, off, len); + if (closed) { + throw new IOException("Stream is closed"); + } + int cursor = 0; + while (cursor < byteLength) { + int readLen = Math.min(byteLength - cursor, blockSize - inputCursor); + // copy the input data to uncompressed buffer + if (readLen > 0) { + System.arraycopy(b, byteOffset + cursor, inputBuffer, inputCursor, readLen); + inputCursor += readLen; + } + if (inputCursor < blockSize) { + return; + } + + compressInput(); + cursor += readLen; + } } /**