R
Russell Wallace
Suppose one needs to both store (in a file) and transmit (via sockets)
data that will be mostly text, but with the occasional chunk of binary
(e.g. GIF images).
It seems to me that there are three possible ways:
1) Use a Reader (intended for text) and write the binary data directly
as 16 bits to a character.
I assume this _won't_ work, at least not reliably, because various
translations will be done that would mess up the binary data?
2) Use a Reader (intended for text) and encode the binary data as text
in hex, base64 or similar. This would work, though I was hoping for a
more elegant solution.
3) Use a Stream (intended for binary) and write strings as sequences of
16-bit integers.
Is it safe to do this? That is, if you put a Java String through a
channel that treats it as a literal sequence of 16-bit integers, are you
guaranteed to get the same character sequence out the other end? Or are
there Unicode complications, bank switching to squeeze different chunks
of the 32-bit code point space into the space of 16 bit Java characters,
that sort of thing that might mean (char)1234 on system A doesn't mean
the same character as (char)1234 on system B?
In general, what's the recommended way to do this - what do people
normally do if they want to put images in an XML file, say? Is there a
fourth way I haven't thought of?
Thanks,
data that will be mostly text, but with the occasional chunk of binary
(e.g. GIF images).
It seems to me that there are three possible ways:
1) Use a Reader (intended for text) and write the binary data directly
as 16 bits to a character.
I assume this _won't_ work, at least not reliably, because various
translations will be done that would mess up the binary data?
2) Use a Reader (intended for text) and encode the binary data as text
in hex, base64 or similar. This would work, though I was hoping for a
more elegant solution.
3) Use a Stream (intended for binary) and write strings as sequences of
16-bit integers.
Is it safe to do this? That is, if you put a Java String through a
channel that treats it as a literal sequence of 16-bit integers, are you
guaranteed to get the same character sequence out the other end? Or are
there Unicode complications, bank switching to squeeze different chunks
of the 32-bit code point space into the space of 16 bit Java characters,
that sort of thing that might mean (char)1234 on system A doesn't mean
the same character as (char)1234 on system B?
In general, what's the recommended way to do this - what do people
normally do if they want to put images in an XML file, say? Is there a
fourth way I haven't thought of?
Thanks,