commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gert-Jan Schouten <gj.schou...@matrixweb.nl>
Subject Question about Base64 encoding
Date Mon, 14 Jan 2013 23:03:27 GMT
Hello all,

I'm doing some experimenting with Base64. I tried encoding an object and
then decoding it:

        Object object = "Foo";

        //Encode object
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Base64OutputStream bos = new Base64OutputStream(baos);
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(object);
        String string = baos.toString();

        //Decode string
        ByteArrayInputStream inputStream = new
ByteArrayInputStream(string.getBytes());
        Base64InputStream bis = new Base64InputStream(inputStream);
        ObjectInputStream ois = new ObjectInputStream(bis);
        Object result = ois.readObject();

However, the 'result' variable does not become the String "Foo". Instead, I
get:

java.io.EOFException
    at
java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2280)
    at
java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(ObjectInputStream.java:3018)
    at
java.io.ObjectInputStream$BlockDataInputStream.readUTF(ObjectInputStream.java:2819)
    at java.io.ObjectInputStream.readString(ObjectInputStream.java:1598)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1319)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)

Strangely enough, when I try this with an Integer or a BigDecimal instead
of a String, it DOES succeed! But when I create my own Serializable Dummy
class that has a BigDecimal and an Integer field, it fails again. Am I
doing something wrong?

Thanks a lot!

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message