Java 将类对象转换为字节缓冲区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19643386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting class objects to byte buffer
提问by Roy08
I have a class that containts two class objects with some variables. I want to send a instance of the class via USB with a send function (and recieve on the other side). The send function accepts byte arrays (byte[]).
我有一个包含两个类对象和一些变量的类。我想通过带有发送功能的 USB 发送类的实例(并在另一侧接收)。send 函数接受字节数组 (byte[])。
My question is how do i convert the instance of the class to a byte array so that i can send it? And how do i properly reconstruct it on the other side?
我的问题是如何将类的实例转换为字节数组以便我可以发送它?我如何在另一边正确地重建它?
Below is te class Comsstruct that i want to send and recieve. Any sugestions are welcome!
下面是我想发送和接收的 te 类 Comsstruct。欢迎任何建议!
// ObjectInfo struct definition
public class ObjectInfo {
int ObjectXCor;
int ObjectYCor;
int ObjectMass;
};
// ObjectInfo struct definition
public class SensorDataStruct{
int PingData;
int IRData;
int ForceData;
int CompassData;
};
// ObjectInfo struct definition
public class CommStruct{
public ObjectInfo VisionData;
public SensorDataStruct SensorData;
};
public CommStruct SendPacket;
public CommStruct RecievePacket;
Update
更新
I have found a solution but with the suggestions that i got i wonder if this will work (and if it is a good solution)?
我找到了一个解决方案,但根据我得到的建议,我想知道这是否可行(以及它是否是一个好的解决方案)?
There is a serialsation method and a send method:
有一个序列化方法和一个发送方法:
// Method to convert object to a byte array
public static byte[] serializeObject(CommStruct obj) throws IOException
{
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bytesOut);
oos.writeObject(obj);
oos.flush();
byte[] bytes = bytesOut.toByteArray();
bytesOut.close();
oos.close();
return bytes;
}
// Send struct function
public void Send(){
try
{
// First convert the CommStruct to a byte array
// Then send the byte array
server.send(serializeObject(SendPacket));
}
catch (IOException e)
{
Log.e("microbridge", "problem sending TCP message", e);
}
And a recieve handler function:
还有一个接收处理函数:
public void onReceive(com.example.communicationmodulebase.Client client, byte[] data)
{
// Event handler for recieving data
// Try to receive CommStruct
try
{
ByteArrayInputStream bytesIn = new ByteArrayInputStream(data);
ObjectInputStream ois = new ObjectInputStream(bytesIn);
RecievePacket = (CommStruct) ois.readObject();
ois.close();
}
catch (IOException e)
{
Log.e("microbridge", "problem recieving TCP message", e);
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("microbridge", "problem recieving TCP message", e);
}
// Send the recieved data packet back
SendPacket = RecievePacket;
// Send CommStruct
Send();
}
My question is if this is a good solution or not?
我的问题是这是否是一个好的解决方案?
采纳答案by Daniel Figueroa
You could have your classes implement the Serializable
interface, and then send that over a bytestream. But that's a really bad idea and will most probably not work.
您可以让您的类实现Serializable
interface,然后通过字节流发送它。但这是一个非常糟糕的主意,很可能行不通。
The reasons for why this wouldn't work, or isn't guaranteed to work, is that there's no guarantee that different implementations of java will use the same serializing mechanism, thus the bytearray could very well differ in different Java implementations.
这行不通或不能保证工作的原因是不能保证不同的 java 实现将使用相同的序列化机制,因此字节数组在不同的Java 实现中可能会大不相同。
Instead I'd recommend you to convert the objects to some intermediary format like xml or json and send that instead. That way the receiver doesn't even have to be coded in java.
相反,我建议您将对象转换为某种中间格式,例如 xml 或 json,然后将其发送。这样接收器甚至不必用java编码。
回答by H?i Phan V?n
You can reference here: Convert object to byte array and convert byte array to object http://www.java2s.com/Code/Java/File-Input-Output/Convertobjecttobytearrayandconvertbytearraytoobject.htm
您可以在这里参考:Convert object to byte array and convert byte array to object http://www.java2s.com/Code/Java/File-Input-Output/Convertobjecttobytearrayandconvertbytearraytoobject.htm