1

Is there any way of implementing a StringReader class that allows reusing the same StringReader (or similar functionality) object with more than one strings, without changing any of its functionality (methods)?

For example, such a Java class (say, ReusableStringReader) would be a subclass of java.io.StringReader, with an extra method like

public void reset(String str);

which would assign the new string value to the internal StringReader parameter, so that all subsequent calls to the read() methods of the StringReader use the new value. Such a "reusability" is very convenient when one wants to restrict the number of objects used by an application (e.g., one that handles very large number of strings per second via a Reader object).

General ways to achieve this would be:

  1. Inheritance. All the internal state of StringReader is stored in private instance variables, so this is not an option.
  2. Reflection. Reset the internal state of the StringReader object via reflection. There are 4 instance variables to set, which means 4 reflection calls per "reset(String)" call (not too efficient).
  3. Composition. One could use a reusable StringInputStream and from that create a Reader object via InputStreamReader, then use that Reader internally in a subclass of StringReader, to implement the full API of the StringReader class. This is quite a hack, though.

Any ideas for a better solution?

1
  • Please remove the "off-topic" restriction. This is a very valid question about a potential IMPLEMENTATION that solves a real-world problem, which is very clearly described in the question test. I reworded it to remove the terms "open source implementation", but even that should not have been necessary. There is nothing "opinionated" here. Commented Apr 22, 2015 at 23:23

1 Answer 1

3

I think you are most likely prematurely optimizing a non-problem here, unless you have actually measured to see that StringReader instances cause severe GC pressure. It is much more likely that allocations that occur due to reading of content are more meaningful.

So I would not worry at all about reusability: use-once instances are much safer and simpler. In general it is relatively rare to actually benefit from reuse of light-weight objects such as readers.

If you still want to pursue this, however, just write your own Reader with reset(), and do not worry about JDK variant. You may peek at implementation, but it is dead simple. There's little benefit from trying to sub-class, delegate or compose: especially if you are really concerned amount minimal allocations.

Sign up to request clarification or add additional context in comments.

3 Comments

Using a new StringReader per line processed is one approach, as is creating another type of Reader. But what if I wanted a StringReader, as per the original question? :-)
Ah -- is someone expecting a StringReader to be passed? If so, a possibility would be to sub-class, but re-implement... mildly wasteful since existing fields would be ignored, and not elegant, but would allow type to be passed as StringReader without per-line instantiation. I actually have had to do something similar with another JDK i/o class (I forget which one), when API expected a concrete type.
Yes, subclassing essentially means copying all the StringReader code, only to add a method that does a few assignments. Which is exactly why I posted the question. Thanks and +1. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.