Skip to content

Commit

Permalink
bugfix: prevent LzoDecompressor from entering endless loop when a emp…
Browse files Browse the repository at this point in the history
…ty lzo file comes.

Signed-off-by: Todd Lipcon <todd@cloudera.com>
  • Loading branch information
angushe authored and toddlipcon committed Dec 7, 2010
1 parent 04e65b8 commit 9d06b25
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/java/com/hadoop/compression/lzo/LzoDecompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,21 +296,19 @@ public synchronized int decompress(byte[] b, int off, int len)
}

// Check if there is data to decompress
if (compressedDirectBufLen <= 0) {
return 0;
}

// Re-initialize the lzo's output direct-buffer
uncompressedDirectBuf.rewind();
uncompressedDirectBuf.limit(directBufferSize);
if (compressedDirectBufLen > 0) {
// Re-initialize the lzo's output direct-buffer
uncompressedDirectBuf.rewind();
uncompressedDirectBuf.limit(directBufferSize);

// Decompress data
numBytes = decompressBytesDirect(strategy.getDecompressor());
uncompressedDirectBuf.limit(numBytes);
// Decompress data
numBytes = decompressBytesDirect(strategy.getDecompressor());
uncompressedDirectBuf.limit(numBytes);

// Return atmost 'len' bytes
numBytes = Math.min(numBytes, len);
((ByteBuffer)uncompressedDirectBuf).get(b, off, numBytes);
// Return atmost 'len' bytes
numBytes = Math.min(numBytes, len);
((ByteBuffer)uncompressedDirectBuf).get(b, off, numBytes);
}
}

// Set 'finished' if lzo has consumed all user-data
Expand Down
9 changes: 9 additions & 0 deletions src/test/com/hadoop/compression/lzo/TestLzopInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class TestLzopInputStream extends TestCase {
private final String bigFile = "100000.txt";
private final String mediumFile = "1000.txt";
private final String smallFile = "100.txt";
private final String emptyFile = "0.txt";

@Override
protected void setUp() throws Exception {
Expand Down Expand Up @@ -81,6 +82,14 @@ public void testSmallFile() throws NoSuchAlgorithmException, IOException,
runTest(smallFile);
}

/**
* Test against a 0 line file.
*/
public void testEmptyFile() throws NoSuchAlgorithmException, IOException,
InterruptedException {
runTest(emptyFile);
}

/**
* Test that reading an lzo-compressed file produces the same lines as reading the equivalent
* flat file. The test opens both the compressed and flat file, successively reading each
Expand Down
Empty file added src/test/data/0.txt
Empty file.
Binary file added src/test/data/0.txt.lzo
Binary file not shown.

0 comments on commit 9d06b25

Please sign in to comment.