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.

4
  • 1
    Is this absolutely necessary? It would obviously be better to store the points in their own table... Commented Mar 24, 2014 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. Commented Mar 24, 2014 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. Commented Mar 24, 2014 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. Commented Mar 25, 2014 at 0:06

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

3 Comments

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 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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.