I want to write a CharSequence for an OutputStream using the specified CharSet. Basically, a script initialized with the same CharSet will do when a record is called (String).
There is a catch, there are many CharSequences to be written, and some are quite large. To complicate matters, everything can be written to multiple OutputStream products. I can easily implement this using (in fact, I currently implemented it this way):
byte[] rawBytes = CharSequence.toString().getBytes(CharSet) for (OutputStream out : outputTargets) { out.write(rawBytes); }
But obviously, String is an absolutely unnecessary garbage object here, as is the byte [] array. I am looking for a method that allows me to do encoding directly without intermediate objects. Surprisingly, this seems impossible - wherever I looked in the JRE, where CharSequence is accepted, it quickly converts to a string before any work is done.
Most (all?) Conversion work for CharSet seems to be done in non-public classes, so I did not find access to any of them in a transparent and legal way.
How to avoid garbage / use JRE CharSet encoding functions directly?
source share