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 Re: Question about Base64 encoding
Date Tue, 15 Jan 2013 21:45:51 GMT
Flushing it did not help, but closing it did! Thanks a lot!

GJ

--------------------------------

Right here you need to flush() or close() your ObjectOutputStream
before you harvest the bytes.

yours,

Julius


On 15 January 2013 18:07, Julius Davies <juliusdavies@gmail.com> wrote:

> On Mon, Jan 14, 2013 at 3:37 PM, Gert-Jan Schouten
> <gj.schouten@matrixweb.nl> 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 <sebbaz@gmail.com> wrote:
> >
> >> 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);
>
> 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: user-unsubscribe@commons.apache.org
> >> For additional commands, e-mail: user-help@commons.apache.org
> >>
> >>
>
>
>
> --
> 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: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

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