08-12-2010 03:45 AM
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);
s =new String(b);
But i get an IOException with this message "decoding error".
How can i solve this?
Solved! Go to Solution.
08-12-2010 05:33 AM
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.
08-12-2010 06:30 AM
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.
02-12-2013 04:58 PM
Unless you plan on writting an extension for WebWorks, I would ask this question on the WebWorks forum here:
02-13-2013 01:29 AM
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.len
Base64InputStream b64 = new Base64InputStream(bis);
ByteArrayOutputStream bos = new ByteArrayOutputStream(dataEncoded.length);
byte buff = new byte; // faster than byte-at-a-time
data = bos.toByteArray();
02-13-2013 06:42 AM
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 wish. You 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());
02-13-2013 12:36 PM
02-13-2013 03:07 PM
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?