Javax Xml Bind Datatypeconverter Base64
This assumes a few things, that you know what the output file name will be and that your data comes as a string.
Base64 Encoding With JDK 1.6+ And Java 8. The method: javax.xml.bind.DatatypeConverter#printBase64Binary(byte[]) encodes a byte[] into a Base64 encoded string.
Javax Xml Bind Annotation
I need to convert some strings using Base64 encoding, and was delighted to see that I didn't have to roll my own converter-Java provides one with javax.xml.bind.DataConverter. However, it has some problems. Here's the output of my time with a Jython REPL: import javax.xml.bind.DatatypeConverter as DC import java.lang.String as String def foo(text). Return DC.printBase64Binary(DC.parseBase64Binary(String(text))).
foo('hello') 'hell' foo('This, it's a punctuated sentence.' ) 'Thisitsapunctuatedsenten' foo(' 'foo ' 'bar ') 'foob' foo(' 'foo ' 'bar '12') 'foobar12' foo(' 'foo ' 'bar '1') 'foob' As you can see, it doesn't handle non-alphanumeric characters at all, and also frequently-but not always-truncates the string by two characters. I guess it might be time to just write my own class, but now I'm bothered that either a) I'm failing at reading the javadoc or something b) The class doesn't work as expected. So any help is much appreciated; thanks in advance. A few findings after spending time resolving a similar problem on a GAE platform (Base64 decoder eats last (two) characters when decoding a base64-string from facebook) If the encoded string is not of a 4.n length then the method DatatypeConverter.parseBase64Binary might drop some trailing characters (rendering the JSON payload syntactically wrong). My solution was to add the following code: while (payload.length% 4!= 0) payload += '='; With regards to the code example in the question, I would suggest a change where the test string gets first encoded and then decoded, ie: return DC.parseBase64Binary(DC.printBase64Binary(String(text).getBytes)).
- Replace javax.xml.bind.DatatypeConverter with java.util.Base64 and (#13. -import javax.xml.bind.DatatypeConverter; + import commons.connectivity.AbstractClient.
- Base64 Encoding With JDK 1.6+ And Java 8. The method: javax.xml.bind.DatatypeConverter#printBase64Binary(byte[]) encodes a byte[] into a Base64 encoded string.