From 00db012c72614355680834516ccd649cf48d63ac Mon Sep 17 00:00:00 2001 From: Burak Emir Date: Wed, 18 Oct 2006 14:21:13 +0000 Subject: abstractfile api + small cleanup --- src/compiler/scala/tools/nsc/io/AbstractFile.scala | 42 +++++++++++++--------- src/compiler/scala/tools/nsc/io/PlainFile.scala | 19 ++-------- src/compiler/scala/tools/nsc/io/ZipArchive.scala | 16 +-------- .../scala/tools/nsc/matching/PatternMatchers.scala | 12 +++---- .../nsc/symtab/classfile/AbstractFileReader.scala | 30 +--------------- 5 files changed, 35 insertions(+), 84 deletions(-) (limited to 'src') diff --git a/src/compiler/scala/tools/nsc/io/AbstractFile.scala b/src/compiler/scala/tools/nsc/io/AbstractFile.scala index 475ea7fada..5d2321c96e 100644 --- a/src/compiler/scala/tools/nsc/io/AbstractFile.scala +++ b/src/compiler/scala/tools/nsc/io/AbstractFile.scala @@ -92,25 +92,33 @@ abstract class AbstractFile extends Object with Iterable[AbstractFile] { /** returns an input stream so the file can be read */ def read : InputStream; - final def toCharArray = { - val input = read; - var str : String = ""; - val buf = new Array[Byte](100); - var eof = false; - while (!eof) { - val x = input.read(buf); - eof = x == -1; - if (!eof) str = str + new String(buf, 0, x); - } - input.close; - str.toCharArray; - } - /** Reads the content of this abstract file into a byte array. */ - //def getBytes: Array[Byte] = error("getBytes not supported by "+this.getClass()) + /** size of this file if it is a concrete file */ + def size: Option[Int] = None + + /** returns contents of file (if applicable) in a byte array. + * warning: use Global.getSourceFile() to use the proper + * encoding when converting to the char array + * @throws java.io.IOException + */ + final def toCharArray = new String(toByteArray).toCharArray - /** Reads the content of this abstract file into a char array. */ - //def getChars: Array[Char] = error("getChars not supported by "+this.getClass()) + /** returns contents of file (if applicable) in a byte array + * @throws java.io.IOException + */ + final def toByteArray = { + val in = read + var rest = size.get + val arr = new Array[Byte](rest); + while (rest > 0) { + val res = in.read(arr, arr.length - rest, rest); + if (res == -1) + throw new java.io.IOException("read error"); + rest = rest - res; + } + in.close(); + arr + } /** Returns all abstract subfiles of this abstract directory. */ def elements: Iterator[AbstractFile] diff --git a/src/compiler/scala/tools/nsc/io/PlainFile.scala b/src/compiler/scala/tools/nsc/io/PlainFile.scala index 8d24387b4f..1cf3241b80 100644 --- a/src/compiler/scala/tools/nsc/io/PlainFile.scala +++ b/src/compiler/scala/tools/nsc/io/PlainFile.scala @@ -41,6 +41,8 @@ class PlainFile(val file: File) extends AbstractFile { override def read = new FileInputStream(file) + override def size = Some(file.length.toInt) + override def hashCode(): Int = try { file.getCanonicalPath().hashCode() } catch { case _: IOException => 0 } @@ -61,23 +63,6 @@ class PlainFile(val file: File) extends AbstractFile { /** Returns the time that this abstract file was last modified. */ def lastModified: Long = file.lastModified() - /** Reads the content of this abstract file into a byte array. - override def getBytes: Array[Byte] = { - assert(!isDirectory, "cannot read directory '" + this + "'"); - val in = new FileInputStream(file) - var rest: Int = file.length().toInt - val buf: Array[Byte] = new Array[Byte](rest) - while (rest > 0) { - val res = in.read(buf, buf.length - rest, rest); - if (res == -1) - throw new IOException("read error"); - rest = rest - res - } - in.close() - buf - } - */ - /** Returns all abstract subfiles of this abstract directory. */ def elements: Iterator[AbstractFile] = { assert(isDirectory, "not a directory '" + this + "'") diff --git a/src/compiler/scala/tools/nsc/io/ZipArchive.scala b/src/compiler/scala/tools/nsc/io/ZipArchive.scala index 5bb9d1d40e..177a13922c 100644 --- a/src/compiler/scala/tools/nsc/io/ZipArchive.scala +++ b/src/compiler/scala/tools/nsc/io/ZipArchive.scala @@ -188,21 +188,7 @@ final class ZipArchive(file: File, val archive: ZipFile) extends PlainFile(file) override def read = archive.getInputStream(entry); - /** in zip archives, we assume class files conform to Java spec by using UTF-8 * / - def getBytes: Array[Byte] = { - val in: InputStream = archive.getInputStream(entry); - var rest: Int = entry.getSize().toInt; - val buf = new Array[Byte](rest); - while (rest > 0) { - val res = in.read(buf, buf.length - rest, rest); - if (res == -1) - throw new IOException("read error"); - rest = rest - res; - } - in.close(); - buf - } - */ + override def size = Some(entry.getSize().toInt) } } diff --git a/src/compiler/scala/tools/nsc/matching/PatternMatchers.scala b/src/compiler/scala/tools/nsc/matching/PatternMatchers.scala index 395809241e..27c5792069 100644 --- a/src/compiler/scala/tools/nsc/matching/PatternMatchers.scala +++ b/src/compiler/scala/tools/nsc/matching/PatternMatchers.scala @@ -230,9 +230,9 @@ trait PatternMatchers requires (transform.ExplicitOuter with PatternNodes) { nref = nref + 1 if(sym.owner == currentOwner) { // oldOwner should match currentOwner nsafeRef = nsafeRef + 1 - } else if(nref == 1) { - //Console.println("sym owner: "+sym.owner+" but currentOwner = "+currentOwner) - } + } /*else if(nref == 1) { + Console.println("sym owner: "+sym.owner+" but currentOwner = "+currentOwner) + }*/ case t if nref > 1 => // abort, no story to tell case t => super . traverse (t) @@ -592,10 +592,10 @@ trait PatternMatchers requires (transform.ExplicitOuter with PatternNodes) { //Console.println(" -- adding new header") //assert index >= 0 : casted; if (index < 0) { Predef.error("error entering:" + casted); return null } - target.and = {curHeader = newHeader(pat.pos, casted, index); curHeader}; // (*) + curHeader = newHeader(pat.pos, casted, index) + target.and = curHeader; // (*) - if (bodycond != null) - target.and = bodycond(target.and) // restores body with the guards + if (bodycond != null) target.and = bodycond(target.and) // restores body with the guards curHeader.or = patternNode(pat, curHeader, env) enter(patArgs, curHeader.or, casted, env) diff --git a/src/compiler/scala/tools/nsc/symtab/classfile/AbstractFileReader.scala b/src/compiler/scala/tools/nsc/symtab/classfile/AbstractFileReader.scala index 684ecb3d9e..0163ea5724 100644 --- a/src/compiler/scala/tools/nsc/symtab/classfile/AbstractFileReader.scala +++ b/src/compiler/scala/tools/nsc/symtab/classfile/AbstractFileReader.scala @@ -19,35 +19,7 @@ class AbstractFileReader(val file: AbstractFile) { /** the buffer containing the file */ - val buf: Array[Byte] = file match { - case p:PlainFile => - assert(!file.isDirectory, "cannot read directory '" + this + "'"); - val in = new FileInputStream(p.file); - var rest: Int = p.file.length().toInt; - val bbuf: Array[Byte] = new Array[Byte](rest); - while (rest > 0) { - val res = in.read(bbuf, bbuf.length - rest, rest); - if (res == -1) - throw new IOException("read error"); - rest = rest - res; - } - in.close(); - bbuf - case z:ZipArchive#FileEntry => // zipfileentry - val in = z.getArchive.getInputStream(z.entry) - var rest: Int = z.entry.getSize().toInt; - val buf = new Array[Byte](rest); - while (rest > 0) { - val res = in.read(buf, buf.length - rest, rest); - if (res == -1) - throw new IOException("read error"); - rest = rest - res; - } - in.close(); - buf - case _ => - error("internal error: do not know how to get bytes of "+file) - } + val buf: Array[Byte] = file.toByteArray /** the current input pointer */ -- cgit v1.2.3