You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Gert-Jan Schouten <gj...@matrixweb.nl> on 2013/01/15 00:03:27 UTC

Question about Base64 encoding

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!

Re: Question about Base64 encoding

Posted by Gert-Jan Schouten <gj...@matrixweb.nl>.
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 <ju...@gmail.com> wrote:

> On Mon, Jan 14, 2013 at 3:37 PM, Gert-Jan Schouten
> <gj...@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 <se...@gmail.com> wrote:
> >
> >> On 14 January 2013 23:03, Gert-Jan Schouten <gj...@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
>
>

Re: Question about Base64 encoding

Posted by Julius Davies <ju...@gmail.com>.
On Mon, Jan 14, 2013 at 3:37 PM, Gert-Jan Schouten
<gj...@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 <se...@gmail.com> wrote:
>
>> On 14 January 2013 23:03, Gert-Jan Schouten <gj...@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


Re: Question about Base64 encoding

Posted by Gert-Jan Schouten <gj...@matrixweb.nl>.
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 <se...@gmail.com> wrote:

> On 14 January 2013 23:03, Gert-Jan Schouten <gj...@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
>
>

Re: Question about Base64 encoding

Posted by sebb <se...@gmail.com>.
On 14 January 2013 23:03, Gert-Jan Schouten <gj...@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