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
New Developer
Posts: 21
Registered: ‎11-05-2008
My Device: Not Specified

Base64OutputStream decode error

Hi folks, I have a .NET WebService 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: 

String contentB64 = service.getFileStrB64(ID); byte[] result = Base64InputStream.decode(contentB64); IOUtil.createFile(file:///SDCard/BlackBerry/documents/CMI/FileFromWS.pdf, result);

 

 I have tried with the 3 decode methods of Base64InputStream class and get from all of them an IOException. 
byte[] result = Base64InputStream.decode(contentB64);
byte[] result = Base64InputStream.decode(contentB64, 0, contentB64.length);
byte[] result = Base64InputStream.decode(byteB64, 0, byteB64.lenght());
So, I did a test on BlackBerry Simulator, because i beleived that the error was being generated by the WebService result. I encoded a PDF file (73KB) to base64Binary, I decode the result and I try to save it to a new file. But I get same IOException. Here is the code:  
byte[] document = IOUtil.readFile("file:///SDCard/BlackBerry/documents/CMI/9720.pdf");
byte[] docB64 = Base64OutputStream.encode(document, 0, document.length, false, false);
byte[] bytes = Base64InputStream.decode(docB64, 0, docB64.length);
IOUtil.createFile("file:///SDCard/BlackBerry/documents/CMI/FromBB.pdf", bytes);

 

I think that the problem is the size of the byte[] passed to method decode. 

 

Any idea how can I get this approach, or suggestion. 

 

Thanks for your time.

New Developer
Posts: 21
Registered: ‎11-05-2008
My Device: Not Specified

Re: Base64OutputStream decode error

Hi,

 

Nobody has experienced something similar? , Please help me.

 

Thanks!

Developer
Developer
Posts: 319
Registered: ‎07-20-2008
My Device: Not Specified

Re: Base64OutputStream decode error

Are you sure that the IOException is being thrown by the Base64 class and not some other IO class?  Can you paste the stack trace here?

 

In general, you should use the Base64InputStream class as a stream wrapper instead of using the static methods.  If you always know that the buffer size you'll receive from your web service will be small, than it might be OK but if you get a large response you'll have to allocate 2 complete buffers to hold and operate on the data.

 

 

New Developer
Posts: 2
Registered: ‎10-13-2009
My Device: Not Specified

Re: Base64OutputStream decode error

Hi, 

 

Im experiencing a related issue but this time trying to retrieve files from an Axis 1.4 Service using SOAP.

 

The filetransfer is being done Chunkwise , so for each chunk the client receives a String , converts it using Base64InputStream.decode and copies it to a byte[] . 

 

The problem is dependent on the size of the chunks, if i specify a chunksize smaller than 48*1024 bytes (48KB) it works fine, but for bigger sizes it just wont work.

 

i paste a piece of code (Removed  unrelevant info) :

 

 

for (int j = 0; j < numberofchunks; j++) {
				
				byte[] chunk = null;									
				
				String msg = service.downloadChunkBB();							
							
				messageBytes = msg.getBytes();							
				
				try {
					chunk = Base64InputStream.decode(messageBytes,0,messageBytes.length);
				} catch (IOException e) {
					throw new IllegalStateException("could not convert base64 string: " + msg);
				}
				
				int length = chunk.length;
				try {
				        System.arraycopy(chunk, 0, imageData, chunkstart, length);
				}catch(Exception e) {	}						
				
				chunk = null;				
                               
			}

 

 

Also, if i try to debug this part with eclipse, the debugger is exiting suddenly without throwing exception or something, it just stop.

 

The problem can be reproduced both on Simulator and device.

 

Any clues?

 

Greetings, Alex.