@@ -83,6 +83,10 @@ protected void readHeader() throws IOException {
8383 }
8484
8585 // Quick test of the header
86+ if (readBytes == 0 ) {
87+ // Snappy produces at least 1-byte result. So the empty input is not a valid input
88+ throw new SnappyIOException (SnappyErrorCode .EMPTY_INPUT , "Cannot decompress empty stream" );
89+ }
8690 if (readBytes < header .length || header [0 ] != SnappyCodec .MAGIC_HEADER [0 ]) {
8791 // do the default uncompression
8892 readFully (header , readBytes );
@@ -93,8 +97,8 @@ protected void readHeader() throws IOException {
9397 if (codec .isValidMagicHeader ()) {
9498 // The input data is compressed by SnappyOutputStream
9599 if (codec .version < SnappyCodec .MINIMUM_COMPATIBLE_VERSION ) {
96- throw new IOException ( String .format (
97- "compressed with imcompatible codec version %d. At least version %d is required" ,
100+ throw new SnappyIOException ( SnappyErrorCode . INCOMPATIBLE_VERSION , String .format (
101+ "Compressed with an incompatible codec version %d. At least version %d is required" ,
98102 codec .version , SnappyCodec .MINIMUM_COMPATIBLE_VERSION ));
99103 }
100104 }
@@ -351,20 +355,15 @@ protected boolean hasNextChunk() throws IOException {
351355 if (readBytes < chunkSize ) {
352356 throw new IOException ("failed to read chunk" );
353357 }
354- try {
355- int uncompressedLength = Snappy .uncompressedLength (compressed , 0 , chunkSize );
356- if (uncompressed == null || uncompressedLength > uncompressed .length ) {
357- uncompressed = new byte [uncompressedLength ];
358- }
359- int actualUncompressedLength = Snappy .uncompress (compressed , 0 , chunkSize , uncompressed , 0 );
360- if (uncompressedLength != actualUncompressedLength ) {
361- throw new IOException ("invalid uncompressed byte size" );
362- }
363- uncompressedLimit = actualUncompressedLength ;
358+ int uncompressedLength = Snappy .uncompressedLength (compressed , 0 , chunkSize );
359+ if (uncompressed == null || uncompressedLength > uncompressed .length ) {
360+ uncompressed = new byte [uncompressedLength ];
364361 }
365- catch (IOException e ) {
366- throw new IOException ("failed to uncompress the chunk: " + e .getMessage ());
362+ int actualUncompressedLength = Snappy .uncompress (compressed , 0 , chunkSize , uncompressed , 0 );
363+ if (uncompressedLength != actualUncompressedLength ) {
364+ throw new SnappyIOException (SnappyErrorCode .INVALID_CHUNK_SIZE , String .format ("expected %,d bytes, but decompressed chunk has %,d bytes" , uncompressedLength , actualUncompressedLength ));
367365 }
366+ uncompressedLimit = actualUncompressedLength ;
368367
369368 return true ;
370369 }
0 commit comments