On Mon, Jan 14, 2013 at 3:37 PM, Gert-Jan Schouten <[email protected]> wrote: > I'm converting it to a String, because in the end, I would like to send it > across the wire with HTTP. But I don't think that matters. If I leave out > the String and just pass baos.toByteArray() into the ByteArrayInputStream, > I get the exact same result. > > > On 14 January 2013 23:22, sebb <[email protected]> wrote: > >> On 14 January 2013 23:03, Gert-Jan Schouten <[email protected]> >> 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);
Right here you need to flush() or close() your ObjectOutputStream before you harvest the bytes. yours, Julius >> > 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: [email protected] >> For additional commands, e-mail: [email protected] >> >> -- yours, Julius Davies 604-222-3310 (Home) $ sudo apt-get install cowsay $ echo "Moo." | cowsay | cowsay -n | cowsay -n http://juliusdavies.ca/cowsay/ --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
