commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sebb <seb...@gmail.com>
Subject Re: Question about Base64 encoding
Date Mon, 14 Jan 2013 23:22:49 GMT
On 14 January 2013 23:03, Gert-Jan Schouten <gj.schouten@matrixweb.nl> wrote:
> 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();

Why are you converting it to a String?

Note that baos.toString().getBytes() may not preserve the original bytes.

>         //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!

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message