Maurice
Maurice

Reputation: 23

Change of format when converting byte array to String

With reference to this post Why are the lengths different when converting a byte array to a String and then back to a byte array?

I understand that changing a byte array containing binary data by doing this

 String s = new String(bytes);

might cause the format of the binary data to change because it creates a string using the default encoding which may convert certain binary characters to unknown characters like "?" and if you convert it back to a byte array it will be wrong.

Currently I have a mime in a ByteArrayInputStream due to mime in mime which looks like this

--boundary
//content type, id, etc...
//empty line
//Binary Data
--boundary--

How do extract the Binary data from the Inputstream and convert it to Base64 if I cannot convert the byte array to a string in the first place? I was thinking of using the boundary to split the Sting converted from the Bytearrayinputstream, but doing so will mess up the Binary data even before I want to encode it to Base64.

Upvotes: 2

Views: 1126

Answers (2)

alphazero
alphazero

Reputation: 27224

You know you have an "empty line" in middle of the bytes (assuming 0x13 or 0x10 byte value), so just seek that byte and you can partition the original byte array. The first part can be simply mapped to String and for the binary data, you now have offset, length, and byte data and that is all you need.

Upvotes: 1

Daniel
Daniel

Reputation: 28084

You can safely convert binary to a String if you present the correct encoding of the String in the binary data! If you know your binary contains a String in ISO8859-1, just do

new String(byteArray,"ISO8859-1") 

and nothing gets lost. And for your information: Base64-Encoded Strings only contains ASCII characters, which are the same in UTF-8 and all typical Windows- and ISO-Encodings, so you won't have any problems with either of them.

Upvotes: 1

Related Questions