[英]Byte array of array of array of Object
I have an Object array of Object array. 我有一个对象数组的对象数组。 I want to convert it into a byte array and then receive it back as array of Object array.
我想将其转换为字节数组,然后将其作为对象数组的数组接收回来。 I have used the
ObjectOutputStream
in conjunction with the ByteArrayOutputStream
to convert it into a byte array: 我已经将
ObjectOutputStream
与ByteArrayOutputStream
结合使用,将其转换为字节数组:
ByteArrayOutputStream b = new BAOS();
ObjectOutputStream o = new OOS();
o.writeObject(obj) // obj being the array
Then when I try to read it, all that I get is the content of the first array only. 然后,当我尝试读取它时,我得到的只是第一个数组的内容。 Also the size of the object array received is equal to the size of the individual arrays.
同样,接收到的对象数组的大小等于各个数组的大小。
I have tried using iteration for writeObject()
but to no avail. 我曾尝试对
writeObject()
使用迭代,但无济于事。
Ok, so further I have tried the multi dimensional array approach as well: 好的,所以我还尝试了多维数组方法:
byte[] firstArr = new byte[1];
oos.writeObject(orgArr[0]);
firstArr = baos.toByteArray();
byte[] secondArr = new byte[1];
oos.writeObject(orgArr[1]);
secondArr = baos.toByteArray();
byte[] combined = new byte[2];
combined[0] = firstArr[0];
combined[1] = secondArr[1];
Both arrays are the same, of same length and both the firstArr
and secondArr
are Object arrays. 两个数组相同,长度相同,并且
firstArr
和secondArr
都是Object数组。 So, the problem that I have is that when I deserialize it using: 所以,我遇到的问题是当我使用以下方法反序列化它时:
ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream(om.nakedPayload));
Object[] list = (Object[]) ois.readObject();
The length of the array list returned is 38. Which is the length of either of the 2 arrays ( firstArr
/ secondArr
). 返回的数组列表的长度为38。这是2个数组中的任何一个的长度(
firstArr
/ secondArr
)。 Also, the data that it contains is just of the firstArr
. 同样,它包含的数据也只是
firstArr
。 The om.nakedPayload
is the data which I read from a Kafka topic. om.nakedPayload
是我从Kafka主题中读取的数据。 We have a wrapper written here so essentially, it expects a byte[]
for read/write purposes. 实际上,我们在这里编写了一个包装器,它期望一个
byte[]
用于读/写目的。
Let's simplify the task and assume your object is Integer, then the code for serializing/deserializing will look like following: 让我们简化任务并假设您的对象是Integer,然后用于序列化/反序列化的代码将如下所示:
import java.io.*;
public class ArrayOfArrays {
public static void main(String[] args) throws Exception{
String fileName = "myobject.data";
FileOutputStream fileOut = new FileOutputStream(fileName);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
Integer[][] intArray = new Integer[3][3];
intArray[0] = new Integer[] {11, 12, 13};
intArray[1] = new Integer[] {21, 22, 23};
intArray[2] = new Integer[] {31, 32, 33};
out.writeObject(intArray);
FileInputStream fileIn = new FileInputStream(fileName);
ObjectInputStream in = new ObjectInputStream(fileIn);
Integer[][] intArrayIn = (Integer[][]) in.readObject();
}
}
This will give same "array of arrays" object back. 这将返回相同的“数组数组”对象。 Next step we can replace Integer with any class which implements marker interface java.io.Serializable.
下一步,我们可以将Integer替换为任何实现标记接口java.io.Serializable的类。
All non-transient fields will be involved in serialization/deserialization and restored along with root "array of arrays" object. 所有非瞬态字段都将参与序列化/反序列化,并与根“数组数组”对象一起恢复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.