Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Advanced-java] C-like structs?
- From: jorti@xxxxxxxxx (Jose Ortiz)
- Subject: [Advanced-java] C-like structs?
- Date: Thu, 28 Feb 2002 15:44:30 -0500
Have your java classes implement Serializable. Qualify the members you
do not want to serialize with the 'transient' keyword. Use
ObjectOutputStream and ObjectInputStream to save complete object graphs.
BTW, the C/C++ example you showed fails if the struct has pointers.
Given
typedef struct MDATA {
int x, y;
MyClass *mc; // A pointer to a user class/struct
} * MYDATAptr;
you do get and object doing this:
file.read(&data, sizeof(MYDATA));
however, the mc member points to garbage. Implementing a serialization
mechanism in C++ requires some effort.
Jose
Jason Boyd wrote:
>
> In C/C++, a common way to serialize data is to define a struct containing
> data in the exact packed format it would be on disk, and then (in
> pseudo-code) do this:
>
> typedef struct MYDATA {
> int x, y;
> double wx, wy;
> } * MYDATAptr;
>
> MYDATA data; // ... fill with actual data
>
> ...
>
> FILE file = open(filename);
> file.write(data, sizeof(MYDATA));
> file.close();
>
> OR:
>
> file.read(&data, sizeof(MYDATA));
>
> Is there a way to do this in Java? That is, simply read from an input
> stream or write into an output stream the raw data of a class without
> having to write serialization code?
>
> The reason is that I need to read heavily nested legacy files (written by
> a C app) which are entirely based on this paradigm, and which use a *lot*
> of different struct types.
>
> Maybe I'm missing an obvious idiom, but I haven't seen a simple way to do
> this in Java... Anyone?
>
> Jason
>
> _______________________________________________
> Advanced-java mailing list
> Advanced-java@xxxxxxxxxxxxxxxxxxxxxx
> http://lists.xcf.berkeley.edu/mailman/listinfo/advanced-java
--
-------------------------------------------------------------------
Jose L. Ortiz, Ph.D.
Senior Development Engineer
_/_/ _/_/ _/_/_/_/ _/
_/ _/ _/ _/ _/ _/ _/ Mechanical Dynamics, Inc.
_/ _/_/ _/ _/ _/ _/ 2300 Traverwood Drive
_/ _/ _/ _/ _/ _/ Ann Arbor, MI 48105, USA
_/ _/ _/_/_/_/ _/ phone : (734) 887-2551
e-mail: jorti@xxxxxxxxx
The virtual prototyping company web : http://www.adams.com
-------------------------------------------------------------------
- References:
- [Advanced-java] C-like structs?
- From: Jason Boyd
- [Advanced-java] C-like structs?
- Prev by Date: [Advanced-java] C-like structs?
- Next by Date: [Advanced-java] C-like structs?
- Previous by thread: [Advanced-java] C-like structs?
- Next by thread: [Advanced-java] C-like structs?
- Index(es):