Is there a class where one can create it by specifying the encoding, feed byte streams into it and get character streams from it? The main point is I want to conserve memory by not having both entire byte-stream data and entire character-stream data in the memory at the same time.
Something like:
Something s = new Something("utf-8");
s.write(buffer, 0, buffer.length); // it converts the bytes directly to characters internally, so we don't store both
// ... several more s.write() calls
s.close(); // or not needed
String text = s.getString();
// or
char[] text = s.getCharArray();
What is that Something?
Are you looking for ByteArrayInputStream?  You could then wrap that in a InputStreamReader and read characters out of the original byte array.
A ByteArrayInputStream lets you "stream" from a byte array. If you wrap that in an InputStreamReader you can read characters.  The InputStreamReader lets you stipulate the character encoding.
If you want to go directly from an input source of bytes, then you can just construct the appropriate sort of InputStream class (FileInputStream for example) and then wrap that in an InputStreamReader.
You can probably mock it up using CharsetDecoder.  Something along the lines of
    CharsetDecoder decoder = Charset.forName(encoding).newDecoder();
    CharBuffer cb = CharBuffer.allocate(100);
    decoder.decode(ByteBuffer.wrap(buffer1), cb, false);
    decoder.decode(ByteBuffer.wrap(buffer2), cb, false);
    ...
    decoder.decode(ByteBuffer.wrap(bufferN), cb, true);
    cb.position(0);
    return cb.toString();
(Yes, I know this will overflow your CharBuffer -- you may want to copy the contents into a StringBuilder as you go.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With