SPARK-1612: Fix potential resource leaks

JIRA: https://issues.apache.org/jira/browse/SPARK-1612

Move the "close" statements into a "finally" block.

Author: zsxwing <zsxwing@gmail.com>

Closes #535 from zsxwing/SPARK-1612 and squashes the following commits:

ae52f50 [zsxwing] Update to follow the code style
549ba13 [zsxwing] SPARK-1612: Fix potential resource leaks
This commit is contained in:
zsxwing 2014-08-01 13:25:04 -07:00 committed by Matei Zaharia
parent baf9ce1a4e
commit f5d9bea20e

View file

@ -286,17 +286,23 @@ private[spark] object Utils extends Logging {
out: OutputStream,
closeStreams: Boolean = false)
{
val buf = new Array[Byte](8192)
var n = 0
while (n != -1) {
n = in.read(buf)
if (n != -1) {
out.write(buf, 0, n)
try {
val buf = new Array[Byte](8192)
var n = 0
while (n != -1) {
n = in.read(buf)
if (n != -1) {
out.write(buf, 0, n)
}
}
} finally {
if (closeStreams) {
try {
in.close()
} finally {
out.close()
}
}
}
if (closeStreams) {
in.close()
out.close()
}
}
@ -868,9 +874,12 @@ private[spark] object Utils extends Logging {
val buff = new Array[Byte]((effectiveEnd-effectiveStart).toInt)
val stream = new FileInputStream(file)
stream.skip(effectiveStart)
stream.read(buff)
stream.close()
try {
stream.skip(effectiveStart)
stream.read(buff)
} finally {
stream.close()
}
Source.fromBytes(buff).mkString
}