diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/compress/BlockDecompressorStream.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/compress/BlockDecompressorStream.java index 72509c72c7d..de457d19240 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/compress/BlockDecompressorStream.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/compress/BlockDecompressorStream.java @@ -71,8 +71,8 @@ public class BlockDecompressorStream extends DecompressorStream { if (noUncompressedBytes == originalBlockSize) { // Get original data size try { - originalBlockSize = rawReadInt(); - } catch (IOException ioe) { + originalBlockSize = rawReadInt(); + } catch (EOFException e) { return -1; } noUncompressedBytes = 0; diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/compress/TestBlockDecompressorStream.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/compress/TestBlockDecompressorStream.java index c976572877b..cdab772e2fc 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/compress/TestBlockDecompressorStream.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/compress/TestBlockDecompressorStream.java @@ -18,11 +18,15 @@ package org.apache.hadoop.io.compress; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.nio.ByteBuffer; import org.junit.Test; @@ -74,4 +78,29 @@ public class TestBlockDecompressorStream { fail("unexpected IOException : " + e); } } + + @Test + public void testReadWhenIoExceptionOccure() throws IOException { + File file = new File("testReadWhenIOException"); + try { + file.createNewFile(); + InputStream io = new FileInputStream(file) { + @Override + public int read() throws IOException { + throw new IOException("File blocks missing"); + } + }; + + try (BlockDecompressorStream blockDecompressorStream = + new BlockDecompressorStream(io, new FakeDecompressor(), 1024)) { + int byteRead = blockDecompressorStream.read(); + fail("Should not return -1 in case of IOException. Byte read " + + byteRead); + } catch (IOException e) { + assertTrue(e.getMessage().contains("File blocks missing")); + } + } finally { + file.delete(); + } + } } \ No newline at end of file