Skip to content

Commit

Permalink
SPARK-1612: Fix potential resource leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
zsxwing committed Apr 24, 2014
1 parent 1fdf659 commit 549ba13
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions core/src/main/scala/org/apache/spark/util/Utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,25 @@ 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)
}
}
}
if (closeStreams) {
in.close()
out.close()
finally {
if (closeStreams) {
try {
in.close()
}
finally {
out.close()
}
}
}
}

Expand Down Expand Up @@ -832,9 +840,13 @@ 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
}

Expand Down

0 comments on commit 549ba13

Please sign in to comment.