0

Is there a way to cast an OutputStream into an ArrayList at once?

I'am testing things out here and what I've got for example:

Writing:

// Some list
List<Edge> edges = = new ArrayList<>(); 
edges.add(new Edge...)
edges.add(new Edge...)
...

OutputStream outStream = socket.getOutputStream();
ObjectOutputStream out = new ObjectOutputStream(outStream);
out.writeObject(edges);

Reading

InputStream inStream = socket.getInputStream();
ObjectInputStream in = new ObjectInputStream(inStream);
Object inObject = in.readObject();
if (inObject.getClass() == ArrayList.class) {
   System.err.println("IS LIST");
   List<Edge> edges = (List<Edge>) in.readObject();
}

Result

IS LIST
Jun 01, 2016 6:15:05 PM client.Client main
SEVERE: null
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2608)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1319)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
at client.Client.main(Client.java:86)
4
  • Your title is misleading and I would recommend editing it : it looks like you don't want to cast the OutputStream, but rather cast the content of an InputStream into a List Commented Jun 1, 2016 at 16:20
  • 2
    Why are you reading an object twice? You check if the first object is a list, then you read in another object. Are you really trying to read 2 objects? If not, you should be doing list = (List) inObject Commented Jun 1, 2016 at 16:20
  • @Aaron I'll edit the title, have a suggestion? Commented Jun 1, 2016 at 16:23
  • @VinceEmigh Ow WAUW, that was the problem.. Thanks! Commented Jun 1, 2016 at 16:23

2 Answers 2

2

EOFException is an "End Of File" exception. It means you attempted to read data when there is no more data to be read (already at the end of the file).

This occurs because you're reading 2 objects, but you are sending only 1. Instead of calling in.readObject() a 2nd time, simply cast the object you've already read:

list = (List<Edge>) inObject;
Sign up to request clarification or add additional context in comments.

3 Comments

This is the solution! Thanks, I'll accept in about 5min (have to wait..)
@JimVercoelen No problem :) Keep in mind that depending on what you're doing, sending objects across a network may be excessive, since class data (meta data) is also sent across the network. It may be cheaper to send a packet explaining how the other side of the network should construct the object you are trying to send.
@VinceEmigh I'll keep that one in mind. Just started today on sockets (@school) so I'am still noob in this! ;-p
-1

I'm afraid that a stream is sequence of data made available over time while an ArrayList is a collection of data that is made available immediately.

So casting one to another (while impossible from class hierarchy point of view) would not make any sense.

Please read about streams and their API in Java.

1 Comment

While the previous title was misleading the question OP is asking is coherent; this doesn't answer the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.