4 Replies Latest reply: Jan 1, 2010 8:41 PM by EJP RSS

    Should I ask for a BufferedReader or just a Reader?

    843789
      Wrapping a BufferedReader in a BufferedReader is not a big deal, right?

      Please consider these two methods:
      public void exampleA(BufferedReader bufRdr) { ........ }
      public void exampleB(Reader rdr) { BufferedReader bufRdr = new BufferedReader(rdr); ... }
      In exampleA, users with a Reader, but not a BufferedReader, cannot use the method (but I can't think
      of any examples of when a user could not create the BufferedReader).
      In exampleB, if someone passes a BufferedReader then that creates a BufferedReader wrapped in another BufferedReader.
      While method overloading fixs this:
      public void example(BufferedReader bufRdr) { ........ }
      public void example(Reader rdr) { example(new BufferedReader(rdr));}
      If a BufferedReader wrapped in a BufferedReader is nothing of note cleary my best solution is exampleB.
      Method overloading perfectly fixs the problem, but it clutters the API so thats why I'd prefer exampleB.
        • 1. Re: Should I ask for a BufferedReader or just a Reader?
          843789
          You only write the method once, but presumably it will be used multiple times. So avoiding the boilerplate of instantiating a new BufferedReader to use the method, is a good thing. So exampleB or the overloaded versions are preferable. And the fact that the method requires a BufferedReader is really more of an implementation detail, one that might change. So I'd say use exampleB.
          • 2. Re: Should I ask for a BufferedReader or just a Reader?
            EJP
            Specify a Reader, always.

            Your example that constructs a new BufferedReader to call the other overload is highly invalid. That BufferedReader will try to fill its buffer, and data can therefore be lost when the method returns and the BufferedReader is released to GC.
            • 3. Re: Should I ask for a BufferedReader or just a Reader?
              843789
              I would like to test my understanding of the before mentioned comments. Please consider:
              public void foo(Reader rdr) {
                BufferedReader bufRdr = new BufferedReader(rdr);
                ... // do whatever
              }
              After foo exits, there is still a reference to rdr.
              The bufRdr object dies when foo exits.
              bufRdr wraps rdr and provides a buffer where it temporarly holds data to allow for block reads.
              There is no way to guarantee the buffer will flush before bufRdr is gc.
              So, the above foo method is not safe.

              Methods should never wrap a Reader/Writer/InputStream/OutputStream ( that has a reference outside of its scope ) with a local object that has a buffer.
              For performance upside, I buffered anything I could get a reference to.
              Now, I think this attitude is completely wrong.
              • 4. Re: Should I ask for a BufferedReader or just a Reader?
                EJP
                For performance upside, I buffered anything I could get a reference to.
                You must create the BufferedReader/Writer/InputStream/OutputStream when you acquire the resource it is buffering, not every time you want to do an I/O operation.