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 in C++ ?
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.
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.
No, you can use serialization instead.
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
here are the methods to accomplish what you want.
public static Object toObjectFromByteArray(byte[] byteArr) {
if (byteArr == null) {
return null;
}
Object resultObj = null;
ByteArrayInputStream bin = null;
ObjectInputStream ooin = null;
try {
bin = new ByteArrayInputStream(byteArr);
ooin = new ObjectInputStream(bin);
resultObj = ooin.readObject();
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
finally {
try {
if (ooin != null) {
ooin.close();
}
if (bin != null) {
bin.close();
}
}
catch (IOException ex1) {
ex1.printStackTrace();
}
}
return resultObj;
}
public static byte[] toByteArray(Object obj) {
ByteArrayOutputStream barr = null;
ObjectOutputStream oout = null;
byte[] bytearr = null;
try {
byte[] b2 = null;
barr = new ByteArrayOutputStream(10000);
oout = new ObjectOutputStream(barr);
oout.writeObject(obj);
oout.flush();
oout.close();
bytearr = barr.toByteArray();
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
finally {
try {
if (oout != null) {
oout.close();
}
if (barr != null) {
barr.close();
}
}
catch (IOException ex1) {
ex1.printStackTrace();
}
}
return bytearr;
}
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.