From f5d9bea20e0db22c09c1191ca44a6471de765739 Mon Sep 17 00:00:00 2001 From: zsxwing Date: Fri, 1 Aug 2014 13:25:04 -0700 Subject: [PATCH] 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 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 --- .../scala/org/apache/spark/util/Utils.scala | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/core/src/main/scala/org/apache/spark/util/Utils.scala b/core/src/main/scala/org/apache/spark/util/Utils.scala index f8fbb3ad6d..30073a8285 100644 --- a/core/src/main/scala/org/apache/spark/util/Utils.scala +++ b/core/src/main/scala/org/apache/spark/util/Utils.scala @@ -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 }