好吧,所以我有两种方法应该可以告诉我哪些方法可以实现ItemStack
,并将其序列化。
ByteOutputStream
并转换为byte[]
。然后byte[]
变成byte
,特殊的分隔符在其间设置一个字节。 ItemStack[]
)都会输入byte[]
,并存储在byte[][][]
中。ItemStack[]
检索byte[]
,该方法使用特殊的分隔符字符集将byte[]
分隔为byte
然后将其转换为Map<String, Object>
,然后转换为ItemStack
。这一切都必须非常令人困惑,因为对我来说,所以我将发布我现在拥有的内容(只有两种方法)。如果需要更多,请告诉我,我最有可能得到它。
我的问题是这不起作用。没有错误或任何东西,但由于某种原因,数据并没有完全成功。如果有人对此有任何解决方案,请帮助。也许这与我分割数据的方式有关......或者我可能会切断或添加字符串或对象的字节。
private static byte[] contentsToBytes(Block block, ItemStack[] contents) throws IOException {
byte[] bytes = new byte[] {block.getData()};
byte[] dataSplitter = ITEMSTACKDATASPLITTER.getBytes("UTF-8");
for (int i = 0; i < contents.length; i++) {
Map<String, Object> serialized = contents[i].serialize();
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(serialized);
byte[] serializedByteArray = byteOut.toByteArray();
byte[] copyBytes = Arrays.copyOf(bytes, bytes.length + dataSplitter.length + serializedByteArray.length);
for (int j = 0; j < dataSplitter.length; j++) {
copyBytes[bytes.length + j] = dataSplitter[j];
}
for (int k = 0; k < serializedByteArray.length; k++) {
copyBytes[bytes.length + dataSplitter.length + k - 1] = serializedByteArray[k];
}
bytes = copyBytes;
}
return bytes;
}
private static ItemStack[] bytesToContents(byte[] bytes) throws IOException, ClassNotFoundException {
ArrayList<ItemStack> stacks = new ArrayList<ItemStack>();
byte[] dataSplitter = ITEMSTACKDATASPLITTER.getBytes("UTF-8");
byte[] currentByteItemStack = new byte[1];
boolean decompress = false;
for (int i = 1; i < bytes.length; i++) {
byte current = bytes[i];
if (current == dataSplitter[0]) {
byte[] dataSplitterTest = Arrays.copyOfRange(bytes, i, i - 1 + dataSplitter.length);
boolean match = true;
for (int j = 0; j < dataSplitter.length; j++) {
if (dataSplitter[j] != dataSplitterTest[j]) {
match = false;
break;
}
}
if (decompress && match) {
ByteArrayInputStream byteIn = new ByteArrayInputStream(Arrays.copyOfRange(currentByteItemStack, 0, currentByteItemStack.length - 2));
ObjectInputStream in = new ObjectInputStream(byteIn);
@SuppressWarnings("unchecked") Map<String, Object> serialized = (Map<String, Object>) in.readObject();
stacks.add(ItemStack.deserialize(serialized));
}
i += dataSplitter.length - 1;
decompress = match;
}
if (decompress) {
currentByteItemStack = Arrays.copyOf(currentByteItemStack, currentByteItemStack.length + 1);
currentByteItemStack[i - 1] = current;
}
}
return stacks.toArray(new ItemStack[stacks.size()]);
}
答案 0 :(得分:4)
所以听起来你正在努力编写自己的序列化,但java已经内置了很好的序列化。
如果是因为您尝试序列化的这些对象没有实现Serializable,那么创建一个可以序列化的临时包装类,然后就可以使用默认的序列化。
实施例
public class MyItemStack implements Externalizable{
private static final long serialVersionUID = 1L;
ItemStack itemStack;
MyItemStack(ItemStack itemStack){
this.itemStack = itemStack;
}
@Override
public void readExternal(ObjectInput arg0) throws IOException, ClassNotFoundException {
...
}
@Override
public void writeExternal(ObjectOutput arg0) throws IOException {
...
}
}
现在你只需要覆盖那些方法来存储ItemStack或Block的真正意义(通常是原语)
之后的序列化应该相当简单,就像这样。
FileOutputStream fos = new FileOutputStream("myfile");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(myHashMap);
oos.close();
FileInputStream fis = new FileInputStream("myfile");
ObjectInputStream ois = new ObjectInputStream(fis);
Map<String,MyItemStack> myMap = (Map<String,MyItemStack>) ois.readObject();
ois.close();