MAPREDUCE-2631. Potential resource leaks in BinaryProtocol$TeeOutputStream.java. Contributed by Sunil G.
This commit is contained in:
parent
5877f20f9c
commit
28660f51af
@ -24,6 +24,7 @@
|
|||||||
|
|
||||||
import org.apache.hadoop.classification.InterfaceAudience;
|
import org.apache.hadoop.classification.InterfaceAudience;
|
||||||
import org.apache.hadoop.classification.InterfaceStability;
|
import org.apache.hadoop.classification.InterfaceStability;
|
||||||
|
import org.apache.hadoop.io.IOUtils;
|
||||||
import org.apache.hadoop.util.DataChecksum;
|
import org.apache.hadoop.util.DataChecksum;
|
||||||
/**
|
/**
|
||||||
* A Checksum output stream.
|
* A Checksum output stream.
|
||||||
@ -60,8 +61,11 @@ public void close() throws IOException {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
closed = true;
|
closed = true;
|
||||||
finish();
|
try {
|
||||||
out.close();
|
finish();
|
||||||
|
} finally {
|
||||||
|
IOUtils.closeStream(out);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -36,6 +36,7 @@
|
|||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.apache.hadoop.io.BytesWritable;
|
import org.apache.hadoop.io.BytesWritable;
|
||||||
import org.apache.hadoop.io.DataOutputBuffer;
|
import org.apache.hadoop.io.DataOutputBuffer;
|
||||||
|
import org.apache.hadoop.io.IOUtils;
|
||||||
import org.apache.hadoop.io.Text;
|
import org.apache.hadoop.io.Text;
|
||||||
import org.apache.hadoop.io.Writable;
|
import org.apache.hadoop.io.Writable;
|
||||||
import org.apache.hadoop.io.WritableComparable;
|
import org.apache.hadoop.io.WritableComparable;
|
||||||
@ -200,8 +201,8 @@ private static class TeeOutputStream extends FilterOutputStream {
|
|||||||
file = new FileOutputStream(filename);
|
file = new FileOutputStream(filename);
|
||||||
}
|
}
|
||||||
public void write(byte b[], int off, int len) throws IOException {
|
public void write(byte b[], int off, int len) throws IOException {
|
||||||
file.write(b,off,len);
|
file.write(b, off, len);
|
||||||
out.write(b,off,len);
|
out.write(b, off, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(int b) throws IOException {
|
public void write(int b) throws IOException {
|
||||||
@ -215,9 +216,12 @@ public void flush() throws IOException {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
flush();
|
try {
|
||||||
file.close();
|
flush();
|
||||||
out.close();
|
} finally {
|
||||||
|
IOUtils.closeStream(file);
|
||||||
|
IOUtils.closeStream(out);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,7 +22,13 @@
|
|||||||
import org.apache.hadoop.io.DataInputBuffer;
|
import org.apache.hadoop.io.DataInputBuffer;
|
||||||
import org.apache.hadoop.io.DataOutputBuffer;
|
import org.apache.hadoop.io.DataOutputBuffer;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
public class TestIFileStreams {
|
public class TestIFileStreams {
|
||||||
@ -99,4 +105,18 @@ public void testBadLength() throws Exception {
|
|||||||
fail("Did not detect bad data in checksum");
|
fail("Did not detect bad data in checksum");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCloseStreamOnException() throws Exception {
|
||||||
|
OutputStream outputStream = Mockito.mock(OutputStream.class);
|
||||||
|
IFileOutputStream ifos = new IFileOutputStream(outputStream);
|
||||||
|
Mockito.doThrow(new IOException("Dummy Exception")).when(outputStream)
|
||||||
|
.flush();
|
||||||
|
try {
|
||||||
|
ifos.close();
|
||||||
|
fail("IOException is not thrown");
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
assertEquals("Dummy Exception", ioe.getMessage());
|
||||||
|
}
|
||||||
|
Mockito.verify(outputStream).close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user