Java has this useful “interface” io.Writer. Unfortunately, in the traditional “some of our team don’t understand OOP” manner, the original designers of java.io made it a class instead of an interface. Bad move.
This made it slightly tricky for the NIO designers to make their buffers compatible with it (e.g. CharBuffer really really ought to “implements Writer”). They could have provided some kind of kludge workaround, but … didn’t.
So now all the existing thousands of java apps that sensibly were compiled to write to Writer instances cannot output directly into a Buffer. Instead you have to do lots of unnecessary copying; e.g. if dealing with chars/Strings, you have to write to a StringWriter, and then convert that to a String and then copy the String to the CharBuffer. NB: you can’t even put the StringBuffer directly - you first have to convert to a String. Very dumb API design.
So…does anyone have a CharBufferWriter (or BufferWriter) they’d care to share? Takes a CharBuffer as input, and extends Writer to write all incoming chars directly into the CharBuffer? I’m assuming it’s such a useful class that:
- someone’s already made it
- …or there’s some subtle reason why it’s hard
- …or there’s some easier way of converting efficiently which I’ve been too stupid to spot.
If not, I’ll probably (*) make one and post it.
(*) …I’m trying to efficiently interface to a lib that does lots of string manipulation and which I know is pretty slow, and trying to claw back performance. Limiting the amount of copying it does (given that it needs to output a LOT of very large strings) looks like it could help with one of the bottlenecks. Then again, if it doesn’t, I’ll be looking elsewhere…