1 11 package org.jboss.portal.junit; 12 13 import org.jboss.portal.common.util.Tools; 14 15 import java.io.ObjectOutputStream ; 16 import java.io.ByteArrayOutputStream ; 17 import java.io.IOException ; 18 import java.io.ByteArrayInputStream ; 19 import java.io.ObjectInputStream ; 20 21 28 public class ResultCodec 29 { 30 31 public static String marshallResult(Result result) 32 { 33 try 34 { 35 if (result == null) 36 { 37 throw new IllegalArgumentException ("No null result accepted"); 38 } 39 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 40 ObjectOutputStream oos = new ObjectOutputStream (baos); 41 oos.writeObject(result); 42 oos.close(); 43 return Tools.toHexString(baos.toByteArray()); 44 } 45 catch (IOException e) 46 { 47 throw new Error ("Unexpected", e); 48 } 49 } 50 51 public static Result unmarshallResult(String hex) 52 { 53 try 54 { 55 if (hex == null) 56 { 57 throw new IllegalArgumentException ("No null string accepted"); 58 } 59 byte[] bytes = Tools.fromHexString(hex); 60 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 61 ObjectInputStream ois = new ObjectInputStream (bais); 62 Result result = (Result)ois.readObject(); 63 ois.close(); 64 return result; 65 } 66 catch (IllegalArgumentException e) 67 { 68 throw new Error ("Unexpected", e); 69 } 70 catch (IOException e) 71 { 72 throw new Error ("Unexpected", e); 73 } 74 catch (ClassNotFoundException e) 75 { 76 throw new Error ("Unexpected", e); 77 } 78 } 79 } 80 | Popular Tags |