aspose file tools*
The moose likes Beginning Java and the fly likes Transform array to string, and back again to array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Transform array to string, and back again to array?" Watch "Transform array to string, and back again to array?" New topic
Author

Transform array to string, and back again to array?

Mads Nielsen
Greenhorn

Joined: Aug 13, 2011
Posts: 28
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Aug 13, 2011
Posts: 28
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 27, 2005
Posts: 19785
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Transform array to string, and back again to array?