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
ibarrera
Posts: 588
Registered: ‎05-29-2009
My Device: 9100
Accepted Solution

Decoding error creating String from Image

I am trying to create an String from a Image to put it into an XML file. I am doing something like this...

 

        fileConn = (FileConnection) Connector.open(fileName, Connector.READ_WRITE);
        Base64InputStream binput = new Base64InputStream(fileConn.openInputStream());
         int length = (int) fileConn.fileSize();
        byte[] b = new byte[length];
        binput.read(b, 0, length);
        binput.close();
      s =new String(b);

 

 

But i get an IOException with this message "decoding error".

 

How can i solve this?

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Decoding error creating String from Image

You need to WRITE a Base 64 coded file, not read it.  So you need to read in the unencoded file, then write out to a Base64 output  Stream, and capture the bytes created from that. 

Developer
ibarrera
Posts: 588
Registered: ‎05-29-2009
My Device: 9100

Re: Decoding error creating String from Image

Thanks a lot, you were right. But one more question, is there some kind of limit for the String i am generating??

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Decoding error creating String from Image

In my experience, you want to try to limit the amount of storage you use.  So I would not attempt to read the entire image and then Base64 encode the whole image, and then create a String for the whole file.  If you want a limit, I have, created similar processing and I read the image file in 48 KB chunks which when Base 54 encoded were 64 KB character Strings that were sent.  This worked OK. 

Contributor
elmar28
Posts: 16
Registered: ‎01-27-2013
My Device: blackberry9360

Re: Decoding error creating String from Image

Could you write a specific method in the source?
I'm suffering from the BASE64 decode the webworks.

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Decoding error creating String from Image

Unless you plan on writting an extension for WebWorks, I would ask this question on the WebWorks forum here:

http://supportforums.blackberry.com/t5/Web-and-WebWorks-Development/bd-p/browser_dev

Contributor
elmar28
Posts: 16
Registered: ‎01-27-2013
My Device: blackberry9360

Re: Decoding error creating String from Image

Here, I did not solve. decode methods beyond the length of the string of 500 000 of base64 dataurl I want to know. Will you be able to avoid when using the java How do you limit that is the problem of this threat?
It is the code below rewrites.

 

byte[] dataEncoded = ( args[ 0 ].toString() ).getBytes( UTF8_ENCODING );

ByteArrayInputStream bis = new ByteArrayInputStream(dataEncoded,0,dataEncoded.length);
Base64InputStream b64 = new Base64InputStream(bis);
ByteArrayOutputStream bos = new ByteArrayOutputStream(dataEncoded.length);
byte[] buff = new byte[512]; // faster than byte-at-a-time
for(int len=b64.read(buff);len!=-1;len=b64.read(buff))bos.write(buff,0,len);
bos.flush();
data=bos.toByteArray();
bos.close();

data = bos.toByteArray();

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Decoding error creating String from Image

Sorry, I understand that English is probably not your first language.  However the question you ask is not clear to me.

 

It seems that there is a limit to the length of the data you can translate from Base64 encoding to bytes.  This limit appears to be around 500,000 characters. 

 

I am not aware of such a limit.  Can you tell us how you know there is a limit and how this causes problems for you.

 

You can decode Base64 in parts if you wishYou will probably need to put the pieces together, but every 4 bytes of input makes 3 bytes of output so you can decode Base64 in parts as long as each part that you decode is a multiple of 4 bytes

 

In addition, does the following replace all the lines you have coded?:

 

byte [] data = Base64InputStream.decode(args[ 0 ].toString());

 

Contributor
elmar28
Posts: 16
Registered: ‎01-27-2013
My Device: blackberry9360

Re: Decoding error creating String from Image

Thank you Mr. peter_strange.

First, I will explain the problem.
I'm making an app using Webworks.
I'm getting dataURL using the canvas of HTML5.
I have written to the Blob to base64 string using a function of stringToBlob of Webworks.
If the data in the BASE64 is short you can retrieve the data in Blob 480px × 320px screen width.
If the data in the BASE64 is short you can retrieve the data in Blob 480px × 320px screen width.

So I was useless, I tried to examine the various rewrite of java stringToBlob.
Code that is what I was raised in a previous comment.

I was useless to try before Oita also have code that I had written.
I think you and I are stuck with the limits that can be used in BASE64Outputstream.
I was a bad month here anymore, I tried to write a decode various ways.
What there is no workaround for this length of something?

Code that is used to fix this part.
I have been rewritten in the conditional expression of BASE64.
https://github.com/blackberry/WebWorks/blob/master/api/utils/src/main/java/blackberry/utils/StringTo...
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Decoding error creating String from Image

OK that is a little clearer.

 

But I am still not sure where the problem is.  You say there is a limit of 500,000 bytes.  Is this the limit of

a) The String that is passed to StringToBllocb

b) The conversion to non Base 64 encoded bytes

c) The bytes that are returned.

 

Can you confirm that the problem is (b).  If it is (b), then this limit is something we can easily test.  And if there is a limit then we can create code that will get round that limit, by doing the conversion in sections as I mentioned earlier. 

 

Now I am also confused because you mention in your post Base64OutputStream.  The processing that we are using is converting a String to bytes, using Base64InputStream.  This seems wrong to me.  Base64 Input is used to convert code that has been Base64 encoded to the original bytes. So is the String input to this process actually Base 64 encoded already?  Or is this processing intended to convert it to Base 64 encoded data?  If it is expected that the bytes returned will be Base 64 encoded, then I think the code is wrong.  And to be honest it looks wrong because if you have a string that contains Base64 encoded text, then you should not need to convert it to bytes using UTF-8 - there are no UTF-8 characters in it.

 

So I am wondering if in fact this code

data = Base64InputStream.decode( dataEncoded, 0, dataEncoded.length );

should have read:

data = Base64InputStream.encode( dataEncoded, 0, dataEncoded.length, false, false );

 

What do you think?