Fix a potential fd leak in AbstractDiskHttpData.getChunk (#10270)

Motivation:

`FileChannel.read()` may throw an IOException. We must deal with this in case of the occurrence of `I/O` error.

Modification:

Place the `FileChannel.read()` method call in the `try-finally` block.

Result:

Advoid fd leak.


Co-authored-by: Norman Maurer <norman_maurer@apple.com>
This commit is contained in:
prgitpr 2020-05-14 16:16:16 +08:00 committed by Norman Maurer
parent 32f2b9c805
commit e7c5773162

View File

@ -295,15 +295,17 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData {
}
int read = 0;
ByteBuffer byteBuffer = ByteBuffer.allocate(length);
try {
while (read < length) {
int readnow = fileChannel.read(byteBuffer);
if (readnow == -1) {
fileChannel.close();
fileChannel = null;
break;
} else {
}
read += readnow;
}
} finally {
fileChannel.close();
fileChannel = null;
}
if (read == 0) {
return EMPTY_BUFFER;