XStream generates huge random strings of junk
I've been using xstream in to serialize an object with name and description (and other fields).
class MyClass {
String name;
String description;
....
MyClass(String name, String description) {
this.name = name;
this.description = description;
}
}
The serialized file comes out HUGE , and the field is filled with junk characters that weight (in MB) hundreds of MB :
<myclass>
<name>Name</name>
<description>My name is ??sA¬A.A¡Aƒ?'A¢?,¬?¡Aƒ??sA,A¢Aƒ?'A+??TAƒ?? A¢?,¬?,¢Aƒ?'A¢?,¬A AƒA¢A¢??sA¬A¢??zA¢Aƒ?'A+??TAƒA¢A¢?
A,A¢Aƒ?'A,A¢AƒA¢A¢?,¬?¡A,A¬Aƒ??▌A,A¡Aƒ?'A¢?,¬?¡Aƒ??sA,A¬Aƒ?'A+??TAƒA¢A¢??sA¬A,A▌Aƒ?'A¢?,¬?¡Aƒ??s
?'A¢?,¬?¡Aƒ??sA,A¬Aƒ?'A+??TAƒ?? A¢?,¬?,¢Aƒ?'A¢?,¬A AƒA¢A¢??sA¬A¢??zA¢Aƒ?'A+??TAƒ??sA,A¢Aƒ?'A,A¢A
A,A¬Aƒ??▌A,A¡Aƒ?'A+??TAƒA¢A¢??sA¬A.A¡Aƒ?'A¢?,¬?¡Aƒ?开发者_C百科?sA,A¡Aƒ?'A+??TAƒ?? A¢?,¬?,¢Aƒ?'A¢?,¬A AƒA¢A¢
ƒ?? A¢?,¬?,¢Aƒ?'A,A¢AƒA¢A¢?,¬?¡A,A¬Aƒ??sA,A Aƒ?'A+??TAƒ??sA,A¢Aƒ?'A,A¢AƒA¢A¢??sA¬A.A¡Aƒ??sA,A¬Aƒ
'A+??TAƒA¢A¢??sA¬A.A¡Aƒ?'A¢?,¬?¡Aƒ??sA,A¢Aƒ?'A+??TAƒ?? A¢?,¬?,¢Aƒ?'A¢?,¬?¡Aƒ??sA,A¢Aƒ?'A+??TAƒ??</name></myclass>
The problem seems to have been a bad encoding issue.
XStream doesn't support specific encoding (each object is written with it's defined encoding). What happened to us was that some characters were encoded in the wrong format , and then read in the wrong format - So every time (we're not sure how) for each char we got 2 "junk" chars. So with every save/load , the size was doubled exponentially...
The solution was to move from
XStream xstream = new XStream();
xstream.toXML(obj);
to
XStream xstream = new XStream();
Writer out = new BufferedWriter(new OutputStreamWriter(output, "UTF8"));
xstream.toXML(tax, out);
精彩评论