KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > util > HexBinaryConverter


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 /**
11  * @author Kohsuke Kawaguchi
12  */

13 public class HexBinaryConverter implements Converter {
14
15     public boolean canConvert(Class JavaDoc type) {
16         return type==byte[].class;
17     }
18
19     public void marshal(Object JavaDoc source, HierarchicalStreamWriter writer, MarshallingContext context) {
20         byte[] data = (byte[]) source;
21         writer.setValue(Util.toHexString(data));
22     }
23
24     public Object JavaDoc unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
25         String JavaDoc data = reader.getValue(); // needs to be called before hasMoreChildren.
26

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