views:

122

answers:

2

Hey again Internet !

So i'm trying to write out an object to a ByteArray, but for some reason it's not writting anything, which i see by the fact that the return value is 0, and that by the fact that reading it results in an exception.

BAoutput = new ByteArrayOutputStream();  
Oout = new ObjectOutputStream(BAoutput);  
Oout.writeObject(receiver);

where receiver is an object i get through a parameter. and the exceptions are always the same:

java.io.EOFException
  at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
  at java.io.ObjectInputStream.readObject0(Unknown Source)
  at java.io.ObjectInputStream.readObject(Unknown Source)

Any ideas?

most of the code: (there are a couple of definitions above it, nothing interesting really)

try {
        BAoutput = new ByteArrayOutputStream();
        Oout = new ObjectOutputStream(BAoutput);
        BAinput = new ByteArrayInputStream(BAoutput.toByteArray());
        Oin = new ObjectInputStream(BAinput);

        Oout.writeObject(receiver);
        retval = method.invoke(receiver, args);
        for (Method curr: postMethods){
            curr.setAccessible(true);
            if (curr.invoke(receiver).equals(false)){
                receiver = Oin.readObject();
                throw new PostconditionFailure();
            }
        }           
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } finally {
        try {
            Oin.close();
            Oout.close();
            BAinput.close();
            BAoutput.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
+1  A: 

First things to check:

  • flush/close the ObjectOutputStream (as Synesso already stated in his comment)
  • Make sure that receiver is Serializable (or a primitive type)
Andreas_D
I close the output and the input stream, but perhaps not at the right time ?also, I'm not that certain where and when should i flush.receiver is Serializable.Also, added the exception. the line wasn't marked when i ctrl+Ced it.
Yonatan
The output stream should be closed before you open the input stream for reading and you can flush the outputstream right after the last write.
Andreas_D
A: 

This is not making a lot of sense to me.

So i'm trying to write out an object to a ByteArray, but for some reason it's not writting anything, which i see by the fact that the return value is 0

What is returning 0? The writeObject method does not return any value at all - its a void method!

... and that by the fact that reading it results in an exception.

That doesn't necessarily mean that the write failed.

I think we need to see >>>all<<< of the code that is involved with the reading and the writing, including type declarations and any enclosing try / catch blocks.

EDIT

Now that I see the complete code, it is clear what the real problem is:

    ....
    BAoutput = new ByteArrayOutputStream();
    Oout = new ObjectOutputStream(BAoutput);
    BAinput = new ByteArrayInputStream(BAoutput.toByteArray());
    Oin = new ObjectInputStream(BAinput);
    ...

The ByteArrayOutputStream.toByteArray() method returns a copy of the current contents of the stream. Since nothing has been written to the stream yet, the byte array is naturally empty.

The solution to your problem is to extract the byte array and construct the ByteArrayInputStream after you have finished writing to Oout and have flushed or closed it.

Stephen C
I guess i should have been clearer. I meant using Oin.avaliable() i knew it had nothing to read.Anyways, the code is in the OP.
Yonatan