1 18 package org.objectweb.speedo.mapper.lib; 19 20 import org.objectweb.util.monolog.api.BasicLevel; 21 import org.objectweb.util.monolog.api.Logger; 22 23 import java.io.File ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.ObjectInputStream ; 28 import java.io.ObjectOutputStream ; 29 30 36 public class Object2JMIFileSerializer { 37 38 public final static char[] ext = {'j', 'm', 'i'}; 39 40 public static String serialize(String output, 41 String jdoFileName, 42 Object o, 43 Logger logger) throws IOException { 44 String fn = jdoFileName2FileName(jdoFileName); 45 File f = new File (output, fn); 46 logger.log(BasicLevel.DEBUG, "Store into the file: " + f.getAbsolutePath()); 47 f.getParentFile().mkdirs(); 48 ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream (f)); 49 oos.writeObject(o); 50 return f.getAbsolutePath(); 51 } 52 53 public static Object deserialize(String jdoFileName, ClassLoader cl, Logger logger) 54 throws Exception { 55 String fn = jdoFileName2FileName(jdoFileName); 56 InputStream is = cl.getResourceAsStream(fn); 57 if (is == null) { 58 Exception e = new IOException ("File " + fn 59 + " is not availlable through the classloader (" + cl + ")."); 60 logger.log(BasicLevel.DEBUG, e.getMessage(), e); 61 throw e; 62 } 63 logger.log(BasicLevel.DEBUG, "Load from the file: " + fn); 64 ObjectInputStream ois = new ObjectInputStream (is); 65 return ois.readObject(); 66 } 67 68 public static String jdoFileName2FileName(String jdoFileName) { 69 char[] cs = jdoFileName.toCharArray(); 70 int size = cs.length; 71 cs[size - 1] = ext[2]; 72 cs[size - 2] = ext[1]; 73 cs[size - 3] = ext[0]; 74 for(int i=(size - 5); i>=0; i--) { 75 if (cs[i] == '.') { 76 cs[i] = File.separatorChar; 77 } 78 } 79 return new String (cs); 80 } 81 } 82 | Popular Tags |