Hi

I serialize objects and then encode them using base64, which the server returns 
to the client. The
client receives a byte array object, but I cannot decode it using
org.apache.commons.codec.binary.Base64. When I do Base64.decodeBase64() it says 
res is not a byte
array. However the code below says the object is a byte array. How do you 
convert res to the
original object (in my case it's a object implementing SecretKey)?

Thanks, Rudi

Object res = xmlrpcclient.execute("method", params);
Class clazz = res.getClass();
if (clazz.isArray() && clazz.getComponentType().equals(byte.class)) {
   System.out.println("TRUE");  // res is a base64 encoded array 
}


Code for serializing and encoding:

public static byte[] encode(Object obj) {
        try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                oos.writeObject(obj);
                oos.close();
                byte[] bytearray = baos.toByteArray();
              return Base64.encodeBase64(bytearray);
        } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
        return null;
}
        
public static Object decode(byte[] encoded) {
        try {
                byte[] decoded = Base64.decodeBase64(encoded);
                ByteArrayInputStream bais = new ByteArrayInputStream(decoded);
                ObjectInputStream ois = new ObjectInputStream(bais);
                return ois.readObject();
        } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
        return null;
}



                
___________________________________________________________ 
How much free photo storage do you get? Store your holiday 
snaps for FREE with Yahoo! Photos http://uk.photos.yahoo.com

Reply via email to