• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transform array to string, and back again to array?

 
Mads Nielsen
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello fellas

I need to send some data over a socket, instead of sending all the data pice by pice, i want to just make one transfer, right now i am transferring all the data as a string representation of an array.



That is working like a charm, however i would like to know the best way of transforming the stringrepresentation back to array.

The only method i can think of is to remove the brackets, and split the string on "," . and since i am never transferring more than 5 items in the array, the split is not going to be a performance issue.

But i am wondering, is there a method to send the array to the socket directly?

What would be the best method for doing this, the array is never going to be large or multidimensional.

Anyone?

Kind regards Mads Nielsen
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I definitely would not use the approach you're taking now. In the absence of more information about the big picture, my first call would be to use an ObjectOutputStream and call writeObject(theArray). Arrays are Serializable, and as long as the elements are either themselves Serialziable or primitives, it's a single step and it saves you the hassle of marshaling and unmarshaling the data.
 
Mads Nielsen
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:I definitely would not use the approach you're taking now. In the absence of more information about the big picture, my first call would be to use an ObjectOutputStream and call writeObject(theArray). Arrays are Serializable, and as long as the elements are either themselves Serialziable or primitives, it's a single step and it saves you the hassle of marshaling and unmarshaling the data.


Wow, that would be a much better solution, thanks.

Can you give me a quick example of sending a simple object over a socket?

Kind regards Mads Nielsen
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mads Nielsen wrote:
Wow, that would be a much better solution, thanks.


You're welcome.

Can you give me a quick example of sending a simple object over a socket?


I already did, except for the socket part, but all you have to do for that is wrap the ObjectOutputStream around your Socket's OutputStream.

For more details and examples you can google for java serialization example.
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The other side of course wraps the socket's InputStream in an ObjectInputStream, and uses its readObject() method to read back the array. You'll need a cast though.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic