Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
arunsingh
Posts: 52
Registered: ‎11-03-2008
My Device: Not Specified

base64Binary decoding

Hi,

 

I have a .NET Web service that returns a file in the form of base64Binary encoded String. I wish to receive this string on the device, decode it and write it to a file. I tried using net.rim.device.api.io.Base64InputStream but there seem to be some issues with that. Here is the code:

 
 

                    InputStream EncodedIS = ConvertStringToInputStream(_EncodedString);  
                    Base64InputStream EncodedBase64Stream = new Base64InputStream(EncodedIS);
                    int nLength = EncodedBase64Stream.available();
                    byte[] buffer = new byte[nLength];
                    EncodedBase64Stream.read(buffer, 0, nLength);
                    String ActualString = new String(buffer);

 

   " _EncodedString " is the base64 encoded string.

 


                     

Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: base64Binary decoding

Try getting the string normally, and then using one of the Base64InputStream.decode() methods. They are static.
Please use plain text.
Developer
arunsingh
Posts: 52
Registered: ‎11-03-2008
My Device: Not Specified

Re: base64Binary decoding

Thanks for the instant response mreed. But I already have tried that approach and that didnt work either. Can you please check that out at your own end by taking a simple base64 encoded string.

 

The problem withmycodeis that it returns the length of encoded input stream as zero.

 

And while using the static method decode(), it throws an IO Exception.

Please use plain text.
Developer
jonberry
Posts: 484
Registered: ‎07-17-2008
My Device: Not Specified

Re: base64Binary decoding

I've used the decode method in the past - it works.
Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: base64Binary decoding

try { String encoded = "VGhpcyBpcyBteSBiYXNlNjQgc3RyaW5nLg=="; byte[] decoded = Base64InputStream.decode( encoded ); System.out.println( new String( decoded ) ); } catch (Throwable t) { System.out.println( "Unable to decode string: " + t.getMessage() ); }

 

Output: "This is my base64 string."

Please use plain text.