my dog learned polymorphism*
The moose likes I/O and Streams and the fly likes Best way to send or receive an arrayList using i/o streams. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Best way to send or receive an arrayList using i/o streams." Watch "Best way to send or receive an arrayList using i/o streams." New topic
Author

Best way to send or receive an arrayList using i/o streams.

Charles Sexton
Ranch Hand

Joined: Sep 26, 2013
Posts: 192
What is the best way to send an arrayList from the server to the client? ObjectInputStream and ObjectOutputStream?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2285
    
  49
I guess that depends on what you mean by 'best'. Using ObjectInputStream and ObjectOutputStream is certainly an easy way of sending an ArrayList providing the Objects in the ArrayList are Serializable.
Charles Sexton
Ranch Hand

Joined: Sep 26, 2013
Posts: 192
Tony Docherty wrote:I guess that depends on what you mean by 'best'. Using ObjectInputStream and ObjectOutputStream is certainly an easy way of sending an ArrayList providing the Objects in the ArrayList are Serializable.


I should have asked what are the ways that you can send an arrayList from server to client or vice versa......ObjectInput and output are extremely easy to use but are their other ways? What is the most popular way for sending arrays, arrayLists or other data structures from client to server or vice versa?
Charles Sexton
Ranch Hand

Joined: Sep 26, 2013
Posts: 192
Tony Docherty wrote:I guess that depends on what you mean by 'best'. Using ObjectInputStream and ObjectOutputStream is certainly an easy way of sending an ArrayList providing the Objects in the ArrayList are Serializable.


A question on implementing Serializable. What is the benefit of using the serialVersionUID? I know that it can be left out but I read an article that says it is beneficial to use it but I am confused as to why.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2285
    
  49
Charles Sexton wrote:I should have asked what are the ways that you can send an arrayList from server to client or vice versa......ObjectInput and output are extremely easy to use but are their other ways? What is the most popular way for sending arrays, arrayLists or other data structures from client to server or vice versa?

There are always other ways but given that ObjectInputStream and ObjectOutputStream are easy to use, well established ways of doing it personally I wouldn't even consider using another method unless I had proved I had a issue associated with using them, such as, performance, security etc. And even then I would look first at resolving it within the framework because there are ways of performance tuning, encrypting the data etc within the serialization framework.

A question on implementing Serializable. What is the benefit of using the serialVersionUID?

If you don't provide one one will be generated based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. When de-serializing, if this number does not match the classes number, de-serialization will fail with a InvalidClassException. This means if you make a change to your class you may not be able to de-serialize streams created from serializing earlier versions of your class. Now this may be good as the class change may have been so significant it may no longer make sense to de-serialize from the older class version but on the other hand the change may have no effect on the validity of the de-serialization process and so you would want to allow it to proceed.

So how do you override this default behaviour and take control of allowing/disallowing de-serialization, you set you own serialVersionUID and you change it's value if the class changes in a way that would prevent de-serialization from an old class producing a valid object.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41815
    
  62
My first advice would be: try to stay away from the binary serialization provided by Object[In|Out]putStream. If the objects contained in the list are JavaBeans, then the java.beans.XML[De|En]coder classes would work (and avoid the compatibility pitfalls that plague binary serialization).


Ping & DNS - my free Android networking tools app
Charles Sexton
Ranch Hand

Joined: Sep 26, 2013
Posts: 192
Ulf Dittmer wrote:My first advice would be: try to stay away from the binary serialization provided by Object[In|Out]putStream. If the objects contained in the list are JavaBeans, then the java.beans.XML[De|En]coder classes would work (and avoid the compatibility pitfalls that plague binary serialization).


Well I am not really developing a web application it is a software application with a client --->>> server ----->>>> database. But I will keep in mind of the JavaBeans alternative.
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
You should not really be asking how to send an arraylist to the server. Rather how to integrate your client application to your server application. The options are WebServices, JMS, remote EJBs e.t.c. Once you decide on a proper integration strategy then you will know how to send array lists (and any other datatypes) between your client and your server.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2285
    
  49
I think we need to be careful not to confuse the OP here. WebServices, JMS etc might be excellent solutions for Charles but they may also be total overkill. Before we overload him with frameworks can we establish what he is trying to achieve. He may be developing a really simple client server application with a server and couple of clients running on a local network.
Charles Sexton
Ranch Hand

Joined: Sep 26, 2013
Posts: 192
Tony Docherty wrote:I think we need to be careful not to confuse the OP here. WebServices, JMS etc might be excellent solutions for Charles but they may also be total overkill. Before we overload him with frameworks can we establish what he is trying to achieve. He may be developing a really simple client server application with a server and couple of clients running on a local network.


You are right Tony it is a server with the factory design pattern to handle multiple clients. I was just trying to see if their were other ways of sending objects from server to client or vice versa. We all know about object[out][in]putstream. Frameworks are a little useless here because it isn't a web application, although java is used mainly for web development I enjoy writing enterprise applications more.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best way to send or receive an arrayList using i/o streams.