I'm am trying to send over socket a very big array - length about 1300000 .... and I use ObjectInputStream and ObjectOutputStream. The problem is when I read the input stream... I get this exception:

java.io.StreamCorruptedException: invalid type code: FF
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at objectThread.run(objectThread.java:27)

//ois is the ObjectInputStream
//o is an Object
 while (ois != null && (o = ois.readObject()) != null) // here I got the exception
                System.out.println("while entered...");
                int[] r = (int[]) o;//get the array

Is the array to big? Or how can I send it over network in the most reliable way? Can somebody help please?

Recommended Answers

All 4 Replies

Can you try this again with the array reduced to (say) 3 elements? That will tell you whether it's a size problem or something else.

Yes... I don't get it anymore now with small length but I don't know why when I get to:

Object o = ois.readObject();// it does nothing here, I tested
int[] r = (int[])o;

That's how I sent it:

int[] r = new int[]{1,2,3};

Weird... something wrong? For the rest of communication I use Data stream and works just fine. Is Object stream somehow special?

Beats me! That looks OK, so I'm stuck too.
Please make sure you post the solution when you find it - I really want to know. I've got quite a bit of code that looks just like that, and it works OK, although I haven't tried sending arrays as such because I (amost) always avaoid using arrays in Java

I made it. The problem was that I was using the same input stream from a socket with an ObjectOutputStream and DataOutputStream. So I use now a separate socket connection for Object stream and it works.
Only one problem: the array is 3932160 length and after I send it 2 or 3 times I get this exception at server application (the sender):

Exception in thread "Thread-5" java.lang.OutOfMemoryError: Java heap space
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.