Skip to content

Commit

Permalink
Fixes for #100
Browse files Browse the repository at this point in the history
  • Loading branch information
xerial committed Apr 14, 2015
1 parent 3fe3251 commit 6d9925b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
28 changes: 11 additions & 17 deletions src/main/java/org/xerial/snappy/SnappyOutputStream.java
Expand Up @@ -231,25 +231,19 @@ private boolean hasSufficientOutputBufferFor(int inputSize) {
* @throws IOException
*/
public void rawWrite(Object array, int byteOffset, int byteLength) throws IOException {

if(inputCursor + byteLength < blockSize) {
int cursor = 0;
while(cursor < byteLength) {
int readLen = Math.min(byteLength - cursor, blockSize - inputCursor);
// copy the input data to uncompressed buffer
Snappy.arrayCopy(array, byteOffset, byteLength, inputBuffer, inputCursor);
inputCursor += byteLength;
return;
}

compressInput();

for(int readBytes = 0; readBytes < byteLength; ) {
int inputLen = Math.min(blockSize, byteLength - readBytes);
if(!hasSufficientOutputBufferFor(inputLen)) {
dumpOutput();
if(readLen > 0) {
Snappy.arrayCopy(array, byteOffset + cursor, readLen, inputBuffer, inputCursor);
inputCursor += readLen;
}
int compressedSize = Snappy.rawCompress(array, byteOffset + readBytes, inputLen, outputBuffer, outputCursor + 4);
writeInt(outputBuffer, outputCursor, compressedSize);
outputCursor += 4 + compressedSize;
readBytes += inputLen;
if(inputCursor < blockSize)
return;

compressInput();
cursor += readLen;
}
}

Expand Down
Expand Up @@ -152,7 +152,7 @@ public void batchingOfWritesShouldNotAffectCompressedDataSize() throws Exception
int[] chunkSizes = new int[] { 1, 100, 1023, 1024, 10000};
for (int chunkSize : chunkSizes) {
byte[] compressedData = compressAsChunks(orig, chunkSize);
assertEquals(expectedCompressedData.length, compressedData.length);
assertEquals(String.format("when chunk size = %,d", chunkSize), expectedCompressedData.length, compressedData.length);
assertArrayEquals(expectedCompressedData, compressedData);
}
}
Expand Down

0 comments on commit 6d9925b

Please sign in to comment.