Engstr�m Anders wrote:
> Now - we want to extend this functionality a bit, and allow parameters and
> return values being sent as serialized java objects. The idea is to
> serialize the object into a byte-array and somehow send this array as a
> string within the XML-payload. The string is then put into a byte-array and
> deserialized. This works fine - but I'm not sure we've solved it in the most
> elegant way :/
>
> Again - the idea is to represent the serialized object as a String. Below is
> the utility-code we're using to read/write the object/string:
>
> // Write an object to a String
> public static String getString( Object object )
> throws IOException
> {
> ByteArrayOutputStream baos = new ByteArrayOutputStream();
> ObjectOutputStream os = new ObjectOutputStream(baos);
> os.writeObject(object);
> os.flush();
>
> byte[] b = baos.toByteArray();
> StringBuffer buff = new StringBuffer();
> for( int i = 0; i < b.length; i++ ){
> buff.append( b[i] );
> if( i < (b.length-1) )
> buff.append(",");
> }
> os.close();
> baos.close();
> return buff.toString();
> }
Why are you using a comma-separated format? Why not just write the
buffer as-is? It just slows it down and double the size of the payload.
> //Read an object from a String
> public static Object getObject( String data )
> throws IOException, ClassNotFoundException
> {
> StringTokenizer tok = new StringTokenizer(data, ",");
> byte[] b = new byte[tok.countTokens()];
AFAIK tok.countTokens() is O(n) so it's kinda expensive. Again, why do
you need it? Plus, even if you want to use a comma-separated format
isn't the nr of tokens equal to (data.length()-1)/2? Faster than
countTokens()..
> int i = 0;
> while( tok.hasMoreTokens() )
> b[i++] = Byte.valueOf(tok.nextToken()).byteValue();
>
> ByteArrayInputStream bin = new ByteArrayInputStream(b);
> ObjectInputStream oin = new ObjectInputStream(bin);
> Object o = oin.readObject();
> oin.close();
> bin.close();
> return o;
> }
> This is a bit akward as the format of the string is defined entirely by us..
>
> Do you guys have any other ideas on how to represent the object as a string,
> and then read the string deserializing the object again?
Well, SOAP would provide you with a more formalized format.
I guess you could use a XML<->Java serializer package too (there are a
couple around).
regards,
Rickard
--
Rickard �berg
Email: [EMAIL PROTECTED]
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".