• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.util.Vector --> byte[] ?  RSS feed

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java Vector to byte ?

I am doing a draw-image web application. When user drag in the screen, it will get a Vector of points .

Now, I want to save this Vector to MySQL. When reloading this application, it could redraw that path. So, I want to convert this Vector to byte[] . But I couldn't find API.

Do you have some better solution to solve this problem ?

Thanks in advance.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can convert the Vector to an array of Objects using the toArray() method. Then I think you will need to convert/cast each object individually into a byte and store that in another array. Something like this:

The exact way you will convert depends on what exactly you are storing in the Vector. The code might be a little different too with JDK 1.5 or above.
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.

Say, we have
class MyPoint {
int x;
int y;
String name;
......
}

How could I convert this MyPoint to byte ? We don't have a way to convert a Vector directly into byte[] ?

Thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would disregard Stuart's suggestion and use "serialization." To do this:

1) Declare your class to "implement Serializable".
2) Create a ByteArrayOutputStream
3) Create an ObjectOutputStream connected to it
4) Use ObjectOutputStream.writeObject() to save the Vector as raw bytes in the ByteArrayOutputStream
5) Flush the ObjectOutputStream
6) Get the bytes from the ByteArrayOutputStream using the toByteArray() method

You basically do the same thing in reverse on the other end, using ByteArrayInputStream, ObjectInputStream, and readObject(). Here is a tutorial on Sun's web site to fill in the details.
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

But the problem is , your way , ObjectOutputStream, will save Vector to an external file. Is it right ? But for me, I want to save data into database with byte[]

Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did I say anything about a file?

A ByteArrayOutputStream is an OutputStream where, when you call write(), the data goes into a byte[]. After you're through putting data into it, you call toByteArray() to get the byte[]. No files are involved.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, must agree with Ernest. It would really save a lot of time and hassle. And it is really the only practical way to represent some of the more complex data that you want to store. Originally I thought you just wanted to store a series of byte pairs.
[ July 19, 2005: Message edited by: Stuart Gray ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!