Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Advanced-java] C-like structs?
- From: shawn.r.crane@xxxxxxxx (Crane, Shawn R (N-MAXIM Group))
- Subject: [Advanced-java] C-like structs?
- Date: Thu, 28 Feb 2002 17:02:05 -0500
Jason,
I'm just writing the first thing I think of, so reader beware! You could
write a utility class that takes an Object as input and spits out a byte
array to an output stream. You would need to implement an interface for
each class you want to stream that returns the field names in the order you
want to serialize them. I'm thinking of something like this:
public interface LegacyStream {
public String[] getLegacyStreamFields();
}
public class LegacyStreamer {
public static void writeObject(OutputStream out, LegacyStream
object) throws Exception {
String[] fields = object.getLegacyStreamFields();
Class c = object.getClass();
for (int i=0; i < fields.length; i++) {
Field f = c.getField(fields[i]);
Class type = f.getType();
if ( type.equals(Byte.class) ) {
byte byteVal = f.getByte(object);
// TODO: Write 1 byte here to output
stream.
}
else if ( type.equals(Integer.class) ) {
int intVal = f.getInt(object);
// TODO: Write the 4 bytes here to output
stream.
}
// TODO: And so on...
}
}
public static Object getObject(InputStream in, Class target) throws
Exception {
Object object = target.newInstance();
String[] fields =
((LegacyStream)object).getLegacyStreamFields();
for (int i=0; i < fields.length; i++) {
Field f = target.getField(fields[i]);
Class type = f.getType();
if ( type.equals(Byte.class) ) {
// TODO: Read 1 byte here and create
variable byteVal.
f.setByte(object, byteVal);
}
else if ( type.equals(Integer.class) ) {
// TODO: Read 4 bytes here and create
variable intVal.
f.setInt(object, intVal);
}
// TODO: And so on...
}
}
}
public class Foo implements LegacyStream {
public x;
public y;
public Foo() {
}
public String[] getLegacyStreamFields() {
String[] ret = new String[2];
ret[0] = "x";
ret[1] = "y";
return ret;
}
}
Then you could call:
LegacyStreamer.writeObject(outputStream, foo);
to write the output to a file and
Foo foo = (Foo) LegacyStreamer.getObject(inputStream,
Class.forName("Foo"));
to get the object back from a file.
Okay, now I'm too lazy to actually see if this compiles or even put in the
java.io code, so you've been warned. But you get the idea. Basically, you
could create a large variety of little data classes, roughly like a C
program would use structs for, that implement the LegacyStream interface to
tell the LegacyStreamer how to stream them. You could get more complex, but
this might be a start.
Good luck!
Shawn R. Crane
shawn.r.crane@xxxxxxxx
-----Original Message-----
From: Jason Boyd [mailto:jboyd@xxxxxx]
Sent: Thursday, February 28, 2002 1:15 PM
To: advanced-java@xxxxxxxxxxxxxxxxxxxxxx
Subject: [Advanced-java] C-like structs?
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
- Prev by Date: [Advanced-java] C-like structs?
- Next by Date: [Advanced-java] XOR of byte
- Previous by thread: [Advanced-java] C-like structs?
- Next by thread: [Advanced-java] XOR of byte
- Index(es):