• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Commons codec Base64 problem

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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[]".
 
aravind kanganar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad its working!
reply
    Bookmark Topic Watch Topic
  • New Topic