18

I am trying to decode data in an xml format into bytes base64 and I am having an issues. My method is in java which takes a String data and converts it into bytes like as bellow.

String data = "......"; //string of data in xml format byte[] dataBytes = Base64.getDecoder().decode(data); 

Which failed and gave the exception like bellow.

java.lang.IllegalArgumentException: Illegal base64 character 3c at java.util.Base64$Decoder.decode0(Base64.java:714) at java.util.Base64$Decoder.decode(Base64.java:526) at java.util.Base64$Decoder.decode(Base64.java:549) at XmlReader.main(XmlReader.java:61) 

Is the xml format not compatible with base64?

10
  • well, what is data? Commented Sep 15, 2017 at 13:15
  • it looks something like this: <Version description="....." productname="....." productversion="1.0" validvalues="....">1.0</Version> Commented Sep 15, 2017 at 13:15
  • That is not a base64 encoded String. Commented Sep 15, 2017 at 13:17
  • how can it be changed to a base64 string? Do I need to use encode? Commented Sep 15, 2017 at 13:18
  • Yes, of course. You have some human readable data like the one you have shown so far, then you encode it to base64 which will only have the allowed base64 chars, then you decode that base64 string into the original content again. Commented Sep 15, 2017 at 13:19

4 Answers 4

45

Just use this method

getMimeDecoder()

String data = "......"; byte[] dataBytes = Base64.getMimeDecoder().decode(data); 
Sign up to request clarification or add additional context in comments.

2 Comments

In deed this works, though I'm curious what's the difference between those 2 APIs, docs are not quite helpful.
@azizbekian Mime decoder allows for illegal characters while trying to decode a Base64 string.
6

I got this same error and problem was that the string was starting with data:image/png;base64, ...

The solution was:

byte[] imgBytes = Base64.getMimeDecoder().decode(imgBase64.split(",")[1]); 

1 Comment

And this is what I did resp = resp.replace("data:image/jpeg;base64,", "");
2

You should first get the bytes out of the string (in some character encoding).

For these bytes you use the encoder to create the Base64 representation for that bytes.

This Base64 string can then be decoded back to bytes and with the same encoding you convert these bytes to a string.

import java.nio.charset.StandardCharsets; import java.util.Base64; public class Base64Example { public static void main(String[] args) { final String xml = "<root-node><sub-node/></root-node>"; final byte[] xmlBytes = xml.getBytes(StandardCharsets.UTF_8); final String xmlBase64 = Base64.getEncoder().encodeToString(xmlBytes); System.out.println(xml); System.out.println(xmlBase64); final byte[] xmlBytesDecoded = Base64.getDecoder().decode(xmlBase64); final String xmlDecoded = new String(xmlBytesDecoded, StandardCharsets.UTF_8); System.out.println(xmlDecoded); } } 

Output is:

<root-node><sub-node/></root-node> PHJvb3Qtbm9kZT48c3ViLW5vZGUvPjwvcm9vdC1ub2RlPg== <root-node><sub-node/></root-node> 

Comments

-7

Thanks to @luk2302 I was able to resolve the issue. Before decoding the string, I need to first encode it to Base64

 byte[] dataBytes = Base64.getEncoder().encode(data.getBytes()); dataBytes = Base64.getDecoder().decode(dataBytes); 

3 Comments

Are you just encoding text and decoding it right away? If so, I fail to understand what's achieved through this.
Probably the encode function is correcting the encoding mismatch that is not making the conversion work at first instance
@funder7 With this you end up with the original (encoded) string, not the real decoded value - encoded (by someone else) -> encoded (by you) -> decoded (by you) = encoded (by someone else); i.e. the original

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.