StringBufferINputStream deprecated for StringReader

Could someone please explain this to me? It’s been bothering me for about 10 years now.

The only class that lets you get an InputStream out of a String has been deprecate in favour of a class that only gives you a Reader.

Yes, it’s a reader out of a string, but … its not an IS.

And practically every library on the planet [ * ] uses IS’s … IIRC partly because Sun encouraged everyone to. All data is expected to be provided as streams.

I wanted to clean up some code thats throwing out compiler warnings, and this turned out to be the main culprit. But … some quick googling reveals World + Dog all using it despite deprecation, and all complaining that Sun hasn’t yet provided an alternative.

I keep hoping there’s some missing class (e.g. since 1.5) I’ve not yet noticed that fixes the problem. Suggestions?

[ * ] = that I come into contact with - most obvious / common ones are XML libs, but obviously I do an awful lot with networking so no surprise there. But every large data-handling lib I’ve seen uses streams because, well, that’s the ONLY sensible way to do it, isn’t it? Lets you have arbitrarily large datasets with constant mem usage?

Would something like (just typed, not compiled) do for UTF8:


public class ReaderInputStream extends InputStream {
    private Reader target;

    public ReaderInputStream(Reader reader) {
          this.target = reader;
    }

    public int read() throws IOException {
          return target.read();
    }
}

Kev

Is it that big a deal?

new ByteArrayInputStream(x.getBytes())

works for me. Of course supply a charset/encoding to the getBytes() call if needed.