You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Salim Erradey <se...@innovance.com> on 2002/05/17 00:49:20 UTC

Using BASE64Encoder/Decoder and the SAXParser

Hi,

I am using the Xerces XMLSerializer to output a document that represents a
snapshot of the objects our application manipulates. The data serialized is
of different types (String, int, double, ...). Recently we decided to add
support for java.lang.Object attributes. We opted to use sun's base64
encoder/decoder classes to translate Object to String and vice-versa. The
String is then serialized or parsed accordingly.

When writing the application into XML, the following code is used before
serializing XML (works fine):

    private String translateBlob(Object object) {
        String stringValue;
        try {
            ByteArrayOutputStream buf = new ByteArrayOutputStream();
			ObjectOutputStream out = new
ObjectOutputStream(buf);
			out.writeObject(object);
			out.close();
			byte[] data = buf.toByteArray();
        	BASE64Encoder b64enc = new BASE64Encoder();
        	stringValue = b64enc.encode(data);
        } catch ( IOException ioe ) {
            stringValue = ioe.getMessage();
        }

        return stringValue;
    }

When reading parsing the XML and creating the data into the application, the
following code is used:

   private Object translateBlob(String valueString) {
        Object obj;
        try {
            BASE64Decoder b64dec = new BASE64Decoder();
			byte[] data = b64dec.decodeBuffer(valueString);
			ByteArrayInputStream in1 = new
ByteArrayInputStream(data);
			ObjectInputStream in2 = new ObjectInputStream(in1);
			obj = in2.readObject();
        } catch ( IOException ioe ) {
            obj = null;
        } catch ( ClassNotFoundException cnfe ) {
            obj = null;
        }

        return obj;
    }
I get a java.io.UTFDataFormatException when in2.readObject is executed. Both
the XMLSerializer and SAX Parser use UTF-8 encoding. I've tried these
snippets of code without the serializer/parser and it works. I am guessing
that there is some incompatibility with the encoding used.

Any ideas or similar experiences shared is appreciated.

Thanks,

Salim Erradey
e-mail: serradey@innovance.com
Tel: (613) 728-3757 x4413