aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzsxwing <zsxwing@gmail.com>2014-08-01 13:25:04 -0700
committerMatei Zaharia <matei@databricks.com>2014-08-01 13:25:04 -0700
commitf5d9bea20e0db22c09c1191ca44a6471de765739 (patch)
treeada9c67de7a3d7e2c2870b722e6de1f776f3e0be
parentbaf9ce1a4ecb7acf5accf7a7029f29604b4360c2 (diff)
downloadspark-f5d9bea20e0db22c09c1191ca44a6471de765739.tar.gz
spark-f5d9bea20e0db22c09c1191ca44a6471de765739.tar.bz2
spark-f5d9bea20e0db22c09c1191ca44a6471de765739.zip
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
-rw-r--r--core/src/main/scala/org/apache/spark/util/Utils.scala35
1 files 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
}