1 package hudson.util; 2 3 import com.thoughtworks.xstream.converters.Converter; 4 import com.thoughtworks.xstream.converters.MarshallingContext; 5 import com.thoughtworks.xstream.converters.UnmarshallingContext; 6 import com.thoughtworks.xstream.io.HierarchicalStreamReader; 7 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 8 import hudson.Util; 9 10 13 public class HexBinaryConverter implements Converter { 14 15 public boolean canConvert(Class type) { 16 return type==byte[].class; 17 } 18 19 public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { 20 byte[] data = (byte[]) source; 21 writer.setValue(Util.toHexString(data)); 22 } 23 24 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { 25 String data = reader.getValue(); 27 byte[] r = new byte[data.length()/2]; 28 for( int i=0; i<data.length(); i+=2 ) 29 r[i/2] = (byte)Integer.parseInt(data.substring(i,i+2),16); 30 31 return r; 32 } 33 } 34 | Popular Tags |