Base64 en/decoding between xstream and iPhone SDK

Posted by Matt McMinn on Stack Overflow See other posts from Stack Overflow or by Matt McMinn
Published on 2010-05-04T21:41:02Z Indexed on 2010/05/04 21:48 UTC
Read the original article Hit count: 535

Filed under:
|
|
|

I am passing a byte array from a java server to an iPad client in XML. The server is using xstream to convert the byte array to XML with the EncodedByteArrayConverter, which should convert the array to Base 64. Using xstream, I can decode the xml back to the proper byte array in a java client, but in the iPad client, I'm getting an invalid length error. To do my decoding, I'm using the code at the bottom of this page. The length of the string is indeed not a multiple of 4, so there must be something strange with my string - although since xstream can decode it just fine, I'm guessing there's just something I need to to on the iPad side to get it to decode. I've tried cutting off padding at the end of the string to get it down to the right size, and that does allow the decoder to work, but I end up with JPG's that have invalid headers, and are not displayable.

On the server side, I'm using the following code:

Object rtrn = getByteArray(); 
XStream xstream = new XStream();
String xml = xstream.toXML(rtrn);

On the client side, I'm calling the above decoder from the XML parsing callback like this:

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    NSLog(@"Converting data; string length: %d", [string length]);
    //NSLog(@"%@", string);
    NSData *data = [Base64 decode:string];
    NSLog(@"converted data length: %d", [data length]);
}

Any ideas what could be going wrong?

© Stack Overflow or respective owner

Related posts about xstream

Related posts about iphone-sdk