java - Converting a Map<String, Object> to a byte[] and back -
alright, have 2 methods should work far can tell gets called itemstack
, , serializes it.
- the serialization entered
byteoutputstream
, turnedbyte[]
.byte[]
turnedbyte
, special separator set bytes in between. - each of these bytes (there
itemstack[]
) enteredbyte[]
, , stored inbyte[][][]
. - later on, try retrieve
itemstack[]
byte[]
calling method, uses special separator set of bytes separatebyte[]
byte
, convertmap<string, object>
,itemstack
.
this must confusing, me, post have below (just 2 methods). if more required, please let me know, , can most-likely it.
my problem doesn't work. no errors or anything, reason data not make way through. if has solutions this, please help. maybe way splitting data... or maybe cutting off or adding on bytes string or object.
private static byte[] contentstobytes(block block, itemstack[] contents) throws ioexception { byte[] bytes = new byte[] {block.getdata()}; byte[] datasplitter = itemstackdatasplitter.getbytes("utf-8"); (int = 0; < 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); (int j = 0; j < datasplitter.length; j++) { copybytes[bytes.length + j] = datasplitter[j]; } (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; (int = 1; < bytes.length; i++) { byte current = bytes[i]; if (current == datasplitter[0]) { byte[] datasplittertest = arrays.copyofrange(bytes, i, - 1 + datasplitter.length); boolean match = true; (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)); } += 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()]); }
so sounds going through great lengths write own serialization, java has great serialization built in.
if it's because these objects trying serialize don't implement serializable, make temporary wrapper class make can serialized, can use default serialization.
example
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 { ... } }
now have override methods store itemstack or block means (usually primitives)
serialization afterwards should simple, this.
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();
Comments
Post a Comment