1

I need to attach a Base64 binary element to a SOAP message...Im doing a dry run to check if I can convert a value read from a file into Base64 binary..

Here is the below code..In the last line I try to print the type of encoded1(I assume it should be Base64 binary values) but I get the following display..."Attachment[B"...How can I confirm if its Base64 binary string?

Path path = Paths.get("c:/tomcatupload/text.csv");
byte[] attachment1 = Files.readAllBytes(path);
byte[] encoded1 = Base64.encode(attachment1);
System.out.println("Attachment"+ encoded1.getClass().getName());
6
  • [B just means that it is a byte array Commented Sep 10, 2014 at 12:41
  • If you look online there are several online Base64 decoders. Just copy/paste your Base64 data into the page and it will show you the corresponding "real" data. Commented Sep 10, 2014 at 12:41
  • 1
    If you used Base64.encode the result will be Base64 encoded. Commented Sep 10, 2014 at 12:41
  • If you convert an array of bytes into a Base64-encoded array of bytes, all you have is another array of bytes, it doesn't hold any "Base64 type" inherently. Commented Sep 10, 2014 at 12:49
  • are you saying you don't trust java.util.Base64? :) Commented Sep 10, 2014 at 12:50

2 Answers 2

2

Base-64 encoding is a way to convert arbitrary bytes to bytes that fit in a range of text characters in ASCII encoding. This is done without any interpretation whatsoever - raw bytes are converted to base-64 on sender's end; the receiver converts them back to a stream of bytes, and that's all there is to it.

When your code prints encoded1.getClass().getName(), all it gets is the static type of the byte array. In order to interpret the data encoded in base-64 as something meaningful to your program, you need to know the format of underlying data transported as base-64. Once the bytes are delivered to you (in your case, that's encoded1 array of bytes) you need to decide what's inside, and act accordingly.

For example, if a serialized Java object is sent to you as base-64, you need to take encoded1, make an in-memory stream from it, and read the object using the regular serialization mechanism:

ByteArrayInputStream memStream = new ByteArrayInputStream(encoded1);
ObjectInputStream objStream = new ObjectInputStream(memStream);
Object attachedObject = objStream.readObject();
Sign up to request clarification or add additional context in comments.

Comments

2

The encoding by Base64.encode() was successful if and only if size of encoded1 > size of obtained attachment1.

Please refer, to understand how the encoding works.

http://en.wikipedia.org/wiki/Base64

By the way, your last statement doesn't print the array content. It prints the name of the class to which encoded1 belongs to.

1 Comment

I would change your sentence to "The encoding by Base64.encode() was successful if and only if size of encoded1 > size of attachment1", because on its own, greater length does not mean base64-encoded :P But I'm being picky here ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.