2

Lets supose I define a class

public class PointFloat {
float x;
float y;
}

Then I instantiate an array

PointFloat[] points = new PointFloat[10];

At this point I have an array of ten PointFloat Objects. Lets supose that some code assigns values x and y to every pointfloats. What I need is to store that array in a VARBINARY in a Mysql database.

To accomplish this I would need to convert this array of PointFloats to byte[] so I can insert into the database using a PreparedStatement Nothing new for me to use a PreparedStatement but first time using objects serialization.

How do you convert an array of PointFloat of any size to a byte[]?. Please keep it as simple as possible.

Thank you very much for reading.

Gábor Lipták
  • 9,646
  • 2
  • 59
  • 113
mdev
  • 472
  • 7
  • 18
  • 1
    Is this absolutely necessary? It would obviously be better to store the points in their own table... – Boris the Spider Mar 24 '14 at 22:14
  • Well. That was the first idea. But It would require additional queries. Imagine selecting 1000 records everyone with its points. Then iterating those records to select the points from DB. Yes. It is possible. But if you can store the points in the record itself is more efficient. – mdev Mar 24 '14 at 22:21
  • 1
    I disagree. You can _query_ the required points if they're in the database. You can delete _individual_ points and add _individual_ points. If you whack the whole lot into a massive `BLOB` then the only way to edit is to load **the whole lot**. If you can change the database schema, that's a win every time. – Boris the Spider Mar 24 '14 at 22:28
  • @BorisTheSpider I partially agree with you. May be could be useful except that for each writing operations there will be 1000 reading operations. However. I am concidering to store the points in two versions format. There will be circunstances where your approach would work better. – mdev Mar 25 '14 at 00:06

2 Answers2

2

You can simply use an ObjectOutputStream to write your array into a ByteArrayOutputStream. See this answer for details and example: https://stackoverflow.com/a/2836659/337621

Since your object contains two floats, the standard serialization completely fits your needs.

Community
  • 1
  • 1
Gábor Lipták
  • 9,646
  • 2
  • 59
  • 113
  • 1
    I havent decided if i am going to laydow the table for the points, serialize bytes[] or JSON. But this post is the one that respond the specific question a Made. Thank you very much for your post. – mdev Mar 24 '14 at 23:55
2

At this point I have an array of ten PointFloat Objects

No. At this point, you have an array of 10 null references.

Choose how you want to transform the points to a byte array. You could design a custom representation, or use Java serialization, or JSON, or XML, for example.

I would choose a format that is readable whatever the language is, and that won't be unreadable as soon as you change the Point class (so not the native Java serialization). JSON is very compact (for a text-based representation). There are dozens of JSON serializers, for every language. They're all documented.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Yes. You are right about the null references. I missed that one :). I made a JSON parser and serializer. (I did to avoid importing more and more jars to the project). I am considering serializing to Json. – mdev Mar 24 '14 at 22:15
  • @mdev You _made_ a JSON serialiser? Christ, you really are insane. Did you implement SSL too? Just one question - how is implementing your own (certainly much less tested) code in any way better than importing a jar? – Boris the Spider Mar 24 '14 at 22:31
  • No. Just the JSON. SSL is left to Apache developers. And a made it because JSON is easy to parse (We cant say the same about XML). It did fine parsin JSON user data retrieved from FB and G+. Of course it does fine serializing. Never had a jscript complaining for the JSON generated. Yes. I am a little insane. But the effort for having a compact project has paid itself. – mdev Mar 24 '14 at 23:53