This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes A nested class serialization. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "A nested class serialization." Watch "A nested class serialization." New topic

A nested class serialization.

Zeev Klein

Joined: Sep 02, 2006
Posts: 3

First, I'm quite new in Java, so my questions might look a bit stupid.

I've tried to search the forum, but couldn't find the case that I'm interested in (or simply didn't understand the explanation ).

The problem:
I'm writing a Java TCP thread (RedHead linux machine) which is supposed to
send a data (originally a large number of the nested Java classes) to a C++ process running on the Unix (AIX) based server.
The data is already converted to the C++ classes representation and I have to convert these nested classes into a byte stream.
Is there a way to convert a nested class into the byte[], without a need to handle each one of the sub-classes ?


marc weber

Joined: Aug 31, 2004
Posts: 11343

"Zeev IsNotRequired" (aka "Zeev NewInJava"),

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your display name here.

Thank you for your prompt attention!


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Zeev Klein

Joined: Sep 02, 2006
Posts: 3
Already did, prior to your request )
Zeev Klein

Joined: Sep 02, 2006
Posts: 3
By the way, is it possible to move my topic to the Threads/Serialization section ? I don't know yet how to do it myself.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
[Zeev]: Already did, prior to your request

No, "NewInJava" is not even remotely similar to a real last name. And playing around like this hardly encourages anyone to help you here. Get a real last name, or your account will be closed.

"I'm not back." - Bill Harding, Twister
Henry Wong

Joined: Sep 28, 2004
Posts: 18503

Originally posted by Zeev NewInJava:
By the way, is it possible to move my topic to the Threads/Serialization section ? I don't know yet how to do it myself.

The section that you may be referring to is the "Threads and Synchronization" forum. Synchronization and Serialization are two different concepts.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Thank you, Mr. Klein. Now on to the question:

As Henry notes, the Threads forum is not appropriate for this topic. The I/O forum might be, but I think that the possible solutions to your problem may benefit from this more general forum, so I'm going to leave the thread here, for now at least.

One approach is to make all the nested classes implement Serializable, and then use an ObjectOutputStream to write the objects to a byte stream. The problem with that, however, is that it assumes there's going to be an ObjectInputStream to read the bytes from the other end of the stream. If instead you've got something in C++doing the reading, then that won't work so well. You might study Java's serialization specification and code up something in C++ to interpret it, but that seems like a lot of work to me; there's probably an easier approach here.

You say you've already converted to a C++ representation. Are you saying these nested classes are Java classes which closely resemble some C++ structures or classes which you're trying to convert to? Can you give an example of what one of these classes looks like, and how it correlates to something on the C++ side? Are the contents of these classes always primitives? Strings? Arrays? Collections? Other more complex types?

I'm thinking that it may be possible to use reflection to make a simple version of serialization, all under your own control, that could be made to fit the C++ format which you require. This is probably not too difficult if the data are all primitives, Strings, primitive wrappers (Integer etc) and arrays or collections of these. If you've got to handle references to other types - well, it gets trickier.

Well "not too difficult" is subjective, and you say you're a Java beginner, so maybe not. But it sounds like you've got experience programming in general, just not much in Java, so I'm thinking this may work out.

It's also possible there are existing tools for this out there, but I'm not sure what they might be, or how closely they might match your requirements. There are tools to convert objects to XML representation - I like XStream. You could use something like that on the Java side, and write a simple interpreter on the C++ side. My gut feeling is that my previous idea of writing your own simple serializer in Java using reflection sounds simpler, but that's because I'm a Java guy, much more than a C++ guy. You may have the opposite idea. Or maybe someone else can suggest another existing tool that would be a better fit for you.

The other thing to consider is, just how many of these nested classes are there? (And how often will they change, or will new types be added?) While writing custom code to serialize each one doesn't sound desireable at first, it may end up easier than a general solution which might prove to be too complex for your current Java skill level. That will have to be your call, but it's something to consider.
[ September 03, 2006: Message edited by: Jim Yingst ]
I agree. Here's the link:
subject: A nested class serialization.
Similar Threads
problem base64 encoding the 3 hex bytes E2 80 A9
Converting DOM_Document into a byte sequence
what is a lightweight way to convert double or integers to byte arrays in Java
Help on wrapper classes and autoboxing
reflection of static inner classes