954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Send array over socket problem - StreamCorruptedException

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?

Clawsy
Posting Whiz in Training
225 posts since Feb 2008
Reputation Points: 11
Solved Threads: 7
 

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.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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};
oos.writeObject(r);
oos.flush();

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

Clawsy
Posting Whiz in Training
225 posts since Feb 2008
Reputation Points: 11
Solved Threads: 7
 

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

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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
Clawsy
Posting Whiz in Training
225 posts since Feb 2008
Reputation Points: 11
Solved Threads: 7
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: