diff options
author | Paul Phillips <paulp@improving.org> | 2010-07-01 18:29:22 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2010-07-01 18:29:22 +0000 |
commit | 3501f38288edb55b2ec78209127430b0b8f062ff (patch) | |
tree | ada6dedfc4e6083d80210f706f4c53ac28b21c55 /src | |
parent | 49344ed1da7c03476de8958e8c7aa224493f4adb (diff) | |
download | scala-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')
-rw-r--r-- | src/compiler/scala/tools/nsc/io/AbstractFile.scala | 3 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/io/ZipArchive.scala | 34 |
2 files changed, 22 insertions, 15 deletions
diff --git a/src/compiler/scala/tools/nsc/io/AbstractFile.scala b/src/compiler/scala/tools/nsc/io/AbstractFile.scala index 5d4f7b8464..4d86dbf114 100644 --- a/src/compiler/scala/tools/nsc/io/AbstractFile.scala +++ b/src/compiler/scala/tools/nsc/io/AbstractFile.scala @@ -17,8 +17,7 @@ import scala.collection.mutable.ArrayBuffer * @author Philippe Altherr * @version 1.0, 23/03/2004 */ -object AbstractFile -{ +object AbstractFile { /** Returns "getFile(new File(path))". */ def getFile(path: String): AbstractFile = getFile(Path(path)) def getFile(path: Path): AbstractFile = getFile(path.toFile) 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(_, _, _, _) |