A strangeness problem was found today when converting byte[] to string and then back to byte[]. byte[] length has changed.
The specific code is as follows:
map<string, persion> map =NewHashmap<string, persion>(); Map.put ("Cn1",NewPersion ("China 1", 30)); Map.put ("Cn2",NewPersion ("China 2", 30)); Map<integer, map<string, persion>> classes =NewHashmap<integer, Map<string, persion>>(); Classes.put (1, map); {Kryo Kryo=NewKryo (); Byteoutputstream Stream=NewByteoutputstream (); Output Output=NewOutput (stream); Kryo.writeclassandobject (output, classes); Output.flush (); byte[] bytes =stream.getbytes (); String Str=NewString (Bytes, "Utf-8"); byte[] Bytes2 = Str.getbytes ("Utf-8"); System.out.println (Bytes.length+ "\ n" + str.length () + "\ n" +bytes2.length); }
The output is:
102410161036
After the experiment found that the problem is coding:
Byteoutputstream The default encoding is "iso8859-1" rather than "utf-8".
Therefore, the "iso8859-1" encoding must be specified when a byte is fetched from stream and converted to a string.
byte [] bytes =new String (bytes, "iso8859-1"byte[] Bytes2 = Str.getbytes ("Iso8859-1" + "\ n" + str.length () + "\ n" + bytes2.length); class= "Java" >
At this point the output length is 1024.
Go Java byte[] Turn String trap