I am trying to read data from a binary stream, parts of which should be parsed as UTF-8.
Using InputStreamdirectly for binary data and InputStreamReaderon top of it for UTF-8 text does not work, as the reader will read further and ruin the subsequent binary data, even if it is told to read the maximum of ncharacters.
I understand that this question is very similar to Reading from InputStream in several formats , but the proposed solution is specific to HTTP streams, which does not help me.
I thought to just read everything as binary data and subsequently convert the corresponding fragments to text. But I only have the length of the given characters in characters, not in bytes. So I need a thing that reads characters from a stream in order to know about the encoding.
Is there a way to tell InputStreamReader not to read further than it takes to read a given number of characters? Or is there a reader that supports both binary data and encoded text and can switch between these modes on the fly?
source
share