I've been trying to do an object serialization and Base64 encode the result. It works with Sun's lib:
Bean01 bean01 = new Bean01();
bean01.setDefaultValues();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
new ObjectOutputStream( baos ).writeObject( bean01 );
System.out.println(Base64.encode(baos.toByteArray()));
This works fine. However, I would like to do the same using org.apache.commons.codec.binary.base64, but this does not return the same string:
System.out.println(org.apache.commons.codec.binary.Base64.encodeBase64(baos.toByteArray()));
What would be the correct way to achieve the correct Base64 encoding of a byteArray using Apache's encoder?
Actually the
commons-codec
version and specific Sun internal version you are using do give the same results. I think you thought they were giving different versions because you are implicitly callingtoString()
on an array when you do:which is definitely does not print out the array contents. Instead, that will only print out the address of the array reference.
I've written the following program to test the encoders against each other. You'll see from the output below that the give the same results:
And here's the output from a run of it:
From commons-codec home page:
I think your problem is the "various level" of compliance.
My advice: choose one base64 encoder/decoder and stick to it