summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/io/ZipArchive.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2010-07-01 18:29:22 +0000
committerPaul Phillips <paulp@improving.org>2010-07-01 18:29:22 +0000
commit3501f38288edb55b2ec78209127430b0b8f062ff (patch)
treeada6dedfc4e6083d80210f706f4c53ac28b21c55 /src/compiler/scala/tools/nsc/io/ZipArchive.scala
parent49344ed1da7c03476de8958e8c7aa224493f4adb (diff)
downloadscala-3501f38288edb55b2ec78209127430b0b8f062ff.tar.gz
scala-3501f38288edb55b2ec78209127430b0b8f062ff.tar.bz2
scala-3501f38288edb55b2ec78209127430b0b8f062ff.zip
Some modifications to ZipFile to make sure the ...
Some modifications to ZipFile to make sure the stream is always closed after iteration. No review.
Diffstat (limited to 'src/compiler/scala/tools/nsc/io/ZipArchive.scala')
-rw-r--r--src/compiler/scala/tools/nsc/io/ZipArchive.scala34
1 files changed, 21 insertions, 13 deletions
diff --git a/src/compiler/scala/tools/nsc/io/ZipArchive.scala b/src/compiler/scala/tools/nsc/io/ZipArchive.scala
index 4be11fc9a8..e65e0040c0 100644
--- a/src/compiler/scala/tools/nsc/io/ZipArchive.scala
+++ b/src/compiler/scala/tools/nsc/io/ZipArchive.scala
@@ -15,13 +15,13 @@ import PartialFunction._
import scala.collection.mutable.{ Map, HashMap }
import scala.collection.JavaConversions.asIterator
+import annotation.tailrec
/**
* @author Philippe Altherr
* @version 1.0, 23/03/2004
*/
-object ZipArchive
-{
+object ZipArchive {
def fromPath(path: Path): ZipArchive = fromFile(path.toFile)
/**
@@ -48,15 +48,24 @@ object ZipArchive
def fromURL(url: URL): AbstractFile = new URLZipArchive(url)
private[io] class ZipEntryTraversableClass(in: InputStream) extends Traversable[ZipEntry] {
- val zis = new ZipInputStream(in)
+ val zis = () => new ZipInputStream(in)
def foreach[U](f: ZipEntry => U) = {
- def loop(x: ZipEntry): Unit = if (x != null) {
- f(x)
- zis.closeEntry()
- loop(zis.getNextEntry())
+ var in: ZipInputStream = null
+ @tailrec def loop(): Unit = {
+ val entry = in.getNextEntry()
+ if (entry != null) {
+ f(entry)
+ in.closeEntry()
+ loop()
+ }
+ }
+
+ try {
+ in = zis()
+ loop()
}
- loop(zis.getNextEntry())
+ finally in.close()
}
}
}
@@ -70,7 +79,7 @@ private[io] trait ZipContainer extends AbstractFile
/** Abstract types */
type SourceType // InputStream or AbstractFile
type CreationType // InputStream or ZipFile
- type ZipTrav = Traversable[ZipEntry] { def zis: ZipInputStream }
+ type ZipTrav = Traversable[ZipEntry] { def zis: () => ZipInputStream }
/** Abstract values */
protected val creationSource: CreationType
@@ -185,8 +194,7 @@ private[io] trait ZipContainer extends AbstractFile
* @author Philippe Altherr
* @version 1.0, 23/03/2004
*/
-final class ZipArchive(file: File, val archive: ZipFile) extends PlainFile(file) with ZipContainer
-{
+final class ZipArchive(file: File, val archive: ZipFile) extends PlainFile(file) with ZipContainer {
self =>
type SourceType = AbstractFile
@@ -236,7 +244,7 @@ final class ZipArchive(file: File, val archive: ZipFile) extends PlainFile(file)
private def zipTraversableFromZipFile(z: ZipFile): ZipTrav =
new Iterable[ZipEntry] {
- def zis: ZipInputStream = null // not valid for this type
+ def zis: () => ZipInputStream = null // not valid for this type
def iterator = asIterator(z.entries())
}
}
@@ -254,7 +262,7 @@ final class URLZipArchive(url: URL) extends AbstractFile with ZipContainer
type CreationType = InputStream
protected lazy val creationSource = input
- protected lazy val root = new ZipRootCreator(x => byteInputStream(x.traverser.zis))()
+ protected lazy val root = new ZipRootCreator(x => byteInputStream(x.traverser.zis()))()
protected def DirEntryConstructor = (_, name, path) => new DirEntry(name, path)
protected def FileEntryConstructor = new FileEntry(_, _, _, _)