diff options
author | Philipp Haller <hallerp@gmail.com> | 2009-06-30 10:55:32 +0000 |
---|---|---|
committer | Philipp Haller <hallerp@gmail.com> | 2009-06-30 10:55:32 +0000 |
commit | 978e36705aa429a1e957230cd5f5ccaed8dc5b4c (patch) | |
tree | c170240032017c4c4eb759261899831ce93ab95c | |
parent | 96f6c893f1ad84ef81e975820af566a926a246c3 (diff) | |
download | scala-978e36705aa429a1e957230cd5f5ccaed8dc5b4c.tar.gz scala-978e36705aa429a1e957230cd5f5ccaed8dc5b4c.tar.bz2 scala-978e36705aa429a1e957230cd5f5ccaed8dc5b4c.zip |
Applied patch to fix #2104 and added test.
-rw-r--r-- | src/library/scala/io/BufferedSource.scala | 13 | ||||
-rw-r--r-- | test/files/jvm/t2104.check | 0 | ||||
-rw-r--r-- | test/files/jvm/t2104.scala | 51 |
3 files changed, 58 insertions, 6 deletions
diff --git a/src/library/scala/io/BufferedSource.scala b/src/library/scala/io/BufferedSource.scala index d84138e5fe..6727b27fb7 100644 --- a/src/library/scala/io/BufferedSource.scala +++ b/src/library/scala/io/BufferedSource.scala @@ -61,15 +61,16 @@ extends Source val reader = new BufferedReader(new InputStreamReader(inputStream, decoder), bufferSize) override val iter = new Iterator[Char] { - private def getc(): Char = - try { reader.read().toChar } + private def getc(): Int = + try { reader.read() } catch { case e: CharacterCodingException => codec receivedMalformedInput e } - var buf_char = getc - def hasNext = reader.ready() + private[this] var buf_char = getc + def peek = buf_char + def hasNext = { buf_char != -1 } def next = { - val c = buf_char - if (hasNext) buf_char = getc + val c = buf_char.toChar + buf_char = getc c } } diff --git a/test/files/jvm/t2104.check b/test/files/jvm/t2104.check new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/test/files/jvm/t2104.check diff --git a/test/files/jvm/t2104.scala b/test/files/jvm/t2104.scala new file mode 100644 index 0000000000..e672f02578 --- /dev/null +++ b/test/files/jvm/t2104.scala @@ -0,0 +1,51 @@ +/* https://lampsvn.epfl.ch/trac/scala/ticket/2104 + symptom: Source via Buffered Source always loses the last char of the input file. + cause: BufferedSource? doesn't check return for -1 (EOF), and uses reader.ready() improperly as a substitute. + + test: check over all possible strings of length up to N over alphabet chars: + write file, then read back its chars, and get back the original. + +*/ +object Test +{ + val N=4 + + import java.io.{ File => JFile } + import java.io.FileWriter + import io.Source + def overwrite(file: JFile,w: FileWriter=>Unit) { + val fw=new FileWriter(file) + w(fw) + fw.close + } + def delete_after(f: JFile,g: Source=>Unit) = { + g(Source.fromFile(f)) + f.delete + } + def store_tempfile(f: FileWriter=>Unit)(implicit name:String) : JFile = { + val tp=JFile.createTempFile(name,null) + overwrite(tp,f) + tp + } + + implicit val name="bug2104" + val chars=List('\n','\r','a') + + type Cs = List[Char] + def all_strings(n: Int) : List[Cs] = { + if (n==0) List(Nil) + else { + val sufs=all_strings(n-1) + chars.flatMap((c)=>sufs.map(c :: _)) + } + } + def test(n: Int) { + for(l <- all_strings(n)) { + val tmp=store_tempfile((f)=>l.foreach(f.write(_))) + delete_after(tmp,(s)=>assert(s.toList == l)) + } + } + def main(args: Array[String]) { + (0 until N).foreach(test(_)) + } +} |