1 24 25 package com.mckoi.database.global; 26 27 import java.util.Date ; 28 import com.mckoi.util.BigNumber; 29 import java.io.*; 30 31 37 38 public class ObjectTranslator { 39 40 43 public static Object translate(Object ob) { 44 if (ob == null) { 45 return null; 46 } 47 else if (ob instanceof String ) { 48 return StringObject.fromString((String ) ob); 49 } 50 else if (ob instanceof StringObject || 51 ob instanceof BigNumber || 52 ob instanceof Date || 53 ob instanceof ByteLongObject || 54 ob instanceof Boolean || 55 ob instanceof StreamableObject) { 56 return ob; 57 } 58 else if (ob instanceof byte[]) { 59 return new ByteLongObject((byte[]) ob); 60 } 61 else if (ob instanceof Serializable) { 62 return serialize(ob); 63 } 64 else { 65 throw new Error ("Unable to translate object. " + 67 "It is not a primitive type or serializable."); 68 } 69 } 70 71 74 public static ByteLongObject serialize(Object ob) { 75 try { 76 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 77 ObjectOutputStream ob_out = new ObjectOutputStream(bout); 78 ob_out.writeObject(ob); 79 ob_out.close(); 80 return new ByteLongObject(bout.toByteArray()); 81 } 82 catch (IOException e) { 83 throw new Error ("Serialization error: " + e.getMessage()); 84 } 85 } 86 87 90 public static Object deserialize(ByteLongObject blob) { 91 if (blob == null) { 92 return null; 93 } 94 else { 95 try { 96 ByteArrayInputStream bin = 97 new ByteArrayInputStream(blob.getByteArray()); 98 ObjectInputStream ob_in = new ObjectInputStream(bin); 99 Object ob = ob_in.readObject(); 100 ob_in.close(); 101 return ob; 102 } 103 catch (ClassNotFoundException e) { 104 throw new Error ("Class not found: " + e.getMessage()); 105 } 106 catch (IOException e) { 107 throw new Error ("De-serialization error: " + e.getMessage()); 108 } 109 } 110 } 111 112 } 113 | Popular Tags |