开发者

Is there cast in Java similar to <reinterpret_cast> in C++

I get in my function message array of bytes and type of object, I need to restore object from bytes. Is there in Java any cast like 开发者_StackOverflow中文版in C++ ?


No, you can use serialization instead.


There is no way in Java to have an arbitrary block of bytes and then tell the compiler "you need to treat this as an object of type X".

How were those bytes that you want to "restore into an object" created in the first place?

Java has a serialization mechanism to convert objects to a stream of bytes and vice versa.


I'm not exactly sure what you're asking here, but each object in Java (and this includes arrays) has runtime type information associated with it. So when you cast an object to a different type, an exception is thrown right away if the new type doesn't match. This is very different from C/C++ where you can just tell the compiler to treat a block of memory as whatever object you want it to be.

If you're looking for code to convert an arbitrary set of bytes into an object or vice-versa you'll need to do it a different way, either using the built-in serialization facilities or else rolling your own conversion code.


No, you need to serialize your object. http://java.sun.com/developer/technicalArticles/Programming/serialization/

This may not be useful if your object data is expected to be readable in other languages.


reinterpret_cast can be simulated in Java to a fair degree, though not for directly deserializing an entire object. As others have suggested, Java's built-in serialization mechanism or other third-party serialization libraries would be your friend there, unless you yourself have the luxury of decomposing your object into simpler types on the sending end.

As for the specific question of whether Java has an equivalent of reinterpret_cast, the answer is a partial yes. If you are trying to read primitives from a binary data stream, ByteBuffer is your friend. Wrap it around an array of bytes, tell it whether the data is little endian or big endian, and then use its various methods to decode the data. This can be used to simulate various C++ reinterpret_cast conversions from bytes into 32-bit integers or floats or whatever.


If you really want to take a block of memory, and reinterpret it as a java object, you can achieve that through sun.misc.Unsafe.

You'd need to iterate the class/type of the object, find all the valid fields you'd need to set, read their specific types out of the off-heap memory buffer, and set them via reflection.

It's not something I'd recommend, making use of other serialization mechanics would be a much better alternative.

Edit: Something like this perhaps:

class Test {
    int x;
    int y;
}

public static void main(String[] args) throws Exception {
    final Unsafe unsafe = Unsafe.getUnsafe();

    final int count = 2;
    final long address = unsafe.allocateMemory(Integer.BYTES * count);

    for (int i = 0; i < count; ++i) {
        unsafe.putInt(address + i * Integer.BYTES, ThreadLocalRandom.current().nextInt(0, 10));
    }

    final Class<Test> klass = Test.class;
    final Test test = klass.newInstance();

    int i = 0;
    for (final Field f : klass.getDeclaredFields()) {
        f.setAccessible(true);
        f.putInt(test, unsafe.getInt(address + i * Integer.BYTES));

        ++i;
    }
}

Of course you easily reinterpret these primitives into anything. I don't even have to Unsafe#putInt, but obviously I need ints according to Test

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜