4

我正在尝试解决“不耐烦的 Scala”一书给出的问题,该书要求将 java 的 BufferedInputStream 实现为特征。这是我的实现,

trait Buffering {           
    this:InputStream =>
        private[this] val bis = {
            new JavaBufferedInputStream(this)
        }
        override def read = bis.read
        override def read(byte:Array[Byte], off:Int, len:Int) = bis.read(byte, off, len)
        override def available = bis.available
        override def close() {
            bis.close
        }
        override def skip(n:Long) = bis.skip(n)
}

def main(args:Array[String]) {
    val bfis = new FileInputStream(new File("foo.txt")) with Buffering
    println(bfis.read)
    bfis.close
}

但这给了我一个 java stackoverflow 错误,那么它有什么问题呢?谢谢!

4

3 回答 3

4

看起来您正在意外地发生堆栈溢出。解决这些问题的关键是查看堆栈跟踪的重复周期。它通常指向重复分配帧的内容。在这里它将显示类似的内容:

at C.Buffering$class.read(C.scala:12)
at C.C$$anon$1.read(C.scala:23)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:256)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
at C.Buffering$class.read(C.scala:12)
at C.C$$anon$1.read(C.scala:23)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:256)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
at C.Buffering$class.read(C.scala:12)

所以从下往上看,看起来你read(byte, ...)在打电话bis.read(byte, ...),哪个在打电话BufferedInputStream.read,然后又在打电话给你read(byte, ...)

看起来它new BufferedInputStream(this)正在调用read底层InputStream,但由于底层this是您的对象,然后委托调用bis我们有无限递归。

我猜作者希望您使用abstract override可堆叠的修改模式,您可以在其中super引用正确的read方法。

于 2012-04-16T06:22:24.243 回答
0

也许这就是答案。只是也许从我的理解。

trait Buffering extends InputStream{
abstract override def read()=
{
    println("Hello");
    super.read()
}
}
val t = new ByteArrayInputStream("hello".getBytes()) with Buffering
于 2014-08-24T14:05:30.597 回答
0

我一直在经历“不耐烦的Scala”。以下是我对第 10 章练习 8 的解决方案:

import java.io._

object BufferedInputStream extends App {

  trait Buffering {
    this: FileInputStream =>
    val br = new BufferedInputStream(this)
  }

  def bufferedReader(f: String): Unit = {
    val fis = new FileInputStream(f) with Buffering
    var c = 0
    var blen = 8192
    var len = 0
    var b = new Array[Byte](blen)
    while (fis.br.available > 0) {
      if (fis.br.available > blen) len = blen
      else len = fis.br.available
      c = fis.br.read(b, 0, len)
      if (c == -1) return
      print((for (i <- 0 to (c - 1)) yield b(i).toChar).mkString)
    }
    fis.br.close
    fis.close
  }

  bufferedReader("webpagexample")

}
于 2015-04-12T09:30:25.150 回答