aspose file tools*
The moose likes Web Services and the fly likes Commons codec Base64 problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Commons codec Base64 problem" Watch "Commons codec Base64 problem" New topic
Author

Commons codec Base64 problem

aravind kanganar
Ranch Hand

Joined: Nov 08, 2009
Posts: 36
Hi all,

I am here with question on usage of base64 class from apache common-codecs.

My code is like this:



What I supply here is byte array of a base 64 binary encoded image, I expected to recieve another byte array with array lenght slightly reduced.

But instead I recieve a byte array of size say, input length is 1020 bytes then out put will be an aryay of size 2 !

Is there any size limit that we should consider while passing argument to decodebase64() method?

I have read in the api doc that the default line size is 76 and '\n\r' are the default line breaks. Do I need to consider any configuration related to this?

Hope I have provided enough details regarding the problem...

Please guide me with your valuable answers/suggestions in this regard.

Thanks in advance,
Aravind
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
What I supply here is byte array of a base 64 binary encoded image

This sounds fishy. Base64-encoding is generally used to create an ASCII representation; how did you create a byte[]?

Are you able to perform a "decodeBase64(encodeBase64(byte[]))" successfully, meaning you get the original byte array?
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 577

Hi Aravind,

What I supply here is byte array of a base 64 binary encoded image, I expected to recieve another byte array with array lenght slightly reduced.

When you say you received another byte array, what is the first byte array? Is that the original byte array of the image, which you then encoded using Base64 and trying to decode using Base64.decodeBase64()?

I think you meant "base64 encoded string created out from byte[]".


Cheers,
Naren
(OCEEJBD6, SCWCD5, SCDJWS, SCJP1.4 and Oracle SQL 1Z0-051)
aravind kanganar
Ranch Hand

Joined: Nov 08, 2009
Posts: 36
Hi Lester,

I am trying to develop a web service that takes image as attachment.
In the xsd, in order to represent 'inline attachment ' I defined a type using 'base64Binary'.
Now the java class corresponsing to the above mentioned type return byte[].

I tried to decode this byte array using decodeBase64() method.

And the decrypted out put is not same as original file.

-Aravind


Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 577

Hi Aravind,

You post is ambiguous atleast for me. I can't figure out how your actual web service is written (e.g you mentioned decryt after decode, are you encryting at the service end? what web service engine you are using? This is important for the client to use Decoding class).

I can guess you might not be using same library encoding and decoding classes at the service and client ends respectively.

If your objective is to retrieve image from service end to client end, I don't know why you use decoding! Your web service engine on the client side would take care of that. The underlying base64 encoding and decoding are done by the web service engine. Just retrieve the byte[] on client end and convert into expected file format. That should be it!

aravind kanganar
Ranch Hand

Joined: Nov 08, 2009
Posts: 36
Hi there,

First of all apologies for the ambiguous post

Hope this will be clear:

1. I have a web service, to which one can send image file as an attachment with the request.
2. The attachment is supposed to be encoded into base64 format, which is happening (I am using Soap UI for sending the request).
3. For the web service I am using spring web service engine here, and a Marshalling payload endpoint.
4. The unmarshalled request object, returns a byte array for the attached image.
5. I am trying to decode this byte array hoping I will recover the original binary content, but this is not happening.
6. For decoding I am using static method: decodeBase64(byte[] base64encoded) from class Base64 (commons-codec API).

Hope I have made my situation clearer.

-Aravind
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 577

For the web service I am using spring web service engine here, and a Marshalling payload endpoint.

That explains!

Try encoding using Spring API class org.springframework.webflow.util.Base64 and remember you don't need to decode at the service end as web service engine does decoding and gives you byte[].
aravind kanganar
Ranch Hand

Joined: Nov 08, 2009
Posts: 36
You are right Naren!!

I was actually storing the 'supposed to be encrypted image' (based on my assumption that the unmarshalled object is giving encrypted byte array) , recived at the endpoint, in a jpg file. Which when I opened appeared scrambled.

The reason for it is that I used to convert the byte array to string (by new String(byteArray) ) and then write it into file using BufferedWriter.
Instead of buffered writer I changed it to FileOutputStream and wrote the byte stream directly, which gave me exact image. And that worked

Thanks alot Naren

-Aravind





Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 577

Glad its working!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Commons codec Base64 problem