How to encode CharSequence using CharSet (without conversion to String)

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?

+6
source share
2 answers

Iterate over sequence characters and write them to a record.

 OutputStream outputStream = .... CharSequence charSequence = .... Charset charset = .... Writer writer = new OutputStreamWriter(outputStream, charset); for (int i = 0; i < charSequence.length(); i++) { writer.write(charSequence.charAt(i)); } 
+6
source

You can use Charset to encode CharSequence into an array of bytes:

 private static byte[] encodeUtf8(CharSequence cs) { ByteBuffer bb = Charset.forName("UTF-8").encode(CharBuffer.wrap(cs)); byte[] result = new byte[bb.remaining()]; bb.get(result); return result; } 

If you use an instance of WritableByteChannel instead of OutputStream , its write method accepts ByteBuffer directly, so you don’t even have to copy the byte buffer to an array of bytes first.

+5
source

Source: https://habr.com/ru/post/952747/


All Articles