Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7789909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:26:08+00:00 2026-06-01T21:26:08+00:00

I am trying to solve a problem given by the Book Scala for the

  • 0

I am trying to solve a problem given by the Book “Scala for the Impatient”, which asked to implement java’s BufferedInputStream as a trait. Here is my implementation,

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
}

But this give me a java stackoverflow error, so what’s wrong with it? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:26:09+00:00Added an answer on June 1, 2026 at 9:26 pm

    It looks like you are getting a stack overflow where you don’t expect one. The key to troubleshoot these is to look at the repeating cycle of the stack trace. It usually points to what is repeatedly allocating frames. Here it will show something like that:

    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)
    

    So reading from bottom to top, it looks like your read(byte, ...) is calling bis.read(byte, ...) which is calling BufferedInputStream.read which is then calling your read(byte, ...) again.

    It would appear that new BufferedInputStream(this) is calling read on the underlying InputStream but since the underlying this is your object that then delegates calls on bis we have infinite recursion.

    I’m guessing that the author wants you to use the abstract override stackable modifications pattern where you can use super to refer to the right read method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to solve a problem which is something like this: I'm given n
I have a problem here which i am trying to solve. The program is
I'm trying to solve this problem : http://uva.onlinejudge.org/external/7/732.html . For the given example, they
I'm trying to solve Problem 13 of project euler, which involves the sum of
I'm trying to solve the problem here but I don't know why my code
I'm trying to solve the following problem: Given an input of, say, 0000000000000000 0011111111110000
A problem I'm trying to solve: given that you have two distinct strings composed
Here is the problem I'm trying to solve for my game. I have this
I am trying to solve one problem from Liang's book I have done most
I'm trying to solve this particular problem using c++. Problem: Given a set of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.