package cquest.utils; //------------------------------------------------------------------------ // FileAccess.java //------------------------------------------------------------------------ // // HISTORY : //------------------------------------------------------------------------ //|Date |Author |Method/Description | //------------------------------------------------------------------------ //------------------------------------------------------------------------ // import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; /** this class allows to do read/write a file. It is used to save or load a metabolite or a model. In the first case, the file matches with the metabolite and in the second with a model. A model is the prior knowledge used by Amares, so it is the GroupManager @author C. Couturier @version %I%, %U%*/ public class FileAccess { //------------------------------------------------------------------------ //variables //------------------------------------------------------------------------ //------------------------------------------------------------------------ //constructor //------------------------------------------------------------------------ /** constructor @author C. Couturier @version %I%, %U%*/ public FileAccess() { } //------------------------------------------------------------------------ //methods //------------------------------------------------------------------------ /** this method allows to compress a file @param file to be compressed @param extension of the compressed file @return compressed file @author C. Couturier @version %I%, %U%*/ private static File compress(File filein, String extension) { String s = filein.getName(); int k = s.lastIndexOf('.'); String name; if (k > 0) { name = s.substring(0, k); } else { name = filein.getName(); } File filetemp = new File(filein.getParent(), name + extension); //Mrui.logger.info("compress " + filetemp.getName()); FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(filein); fos = new FileOutputStream(filetemp); ZipOutputStream zos = new ZipOutputStream(fos); ZipEntry ze = new ZipEntry(s); zos.putNextEntry(ze); final int BUFSIZ = 4096; byte inbuf[] = new byte[BUFSIZ]; int n; while ((n = fis.read(inbuf)) != -1) zos.write(inbuf, 0, n); fis.close(); fis = null; zos.close(); fos = null; } catch (IOException e) { return (File) null; } finally { try { if (fis != null) fis.close(); if (fos != null) fos.close(); } catch (IOException e) { } } try { filein.delete(); } catch (Exception e) { System.out.println(e.getMessage()); return (File) null; } return filetemp; } /** this method uncompresses a file @param file to uncompress @return uncompressed file @author C. Couturier @version %I%, %U%*/ private static File uncompress(File filein) { String s = filein.getName(); int k = s.lastIndexOf('.'); String name = s.substring(0, k); File fileout = new File(filein.getParent(), name); FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(filein); fos = new FileOutputStream(fileout); ZipInputStream zis = new ZipInputStream(fis); ZipEntry ze = zis.getNextEntry(); final int BUFSIZ = 4096; byte inbuf[] = new byte[BUFSIZ]; int n; while ((n = zis.read(inbuf, 0, BUFSIZ)) != -1) fos.write(inbuf, 0, n); zis.close(); fis = null; fos.close(); fos = null; } catch (IOException e) { System.out.println(e.getMessage()); } finally { try { if (fis != null) fis.close(); if (fos != null) fos.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } return fileout; } /** to write the Model in file. @param file in to write @param group manager @return void @author C. Couturier @version %I%, %U%*/ /* public void writeModel(File fmodel, GroupManager gmn) { try{ FileOutputStream fileOut = new FileOutputStream(fmodel); ObjectOutputStream dataOut = new ObjectOutputStream(fileOut); dataOut.writeObject((GroupManager)gmn); dataOut.close(); fileOut.close(); } catch (Exception e){;} File fileout = compress(fmodel, ".mod"); } /** to read the model useful for Amares @param file to read @return model (GroupManager) @return void @author C. Couturier @version %I%, %U%*/ /* public GroupManager readModel(File fmodel) { GroupManager gmn =(GroupManager)null; if (!(checkExtension("mod", fmodel))) return gmn; File f = uncompress(fmodel); try{ FileInputStream fileIn = new FileInputStream(f); ObjectInputStream dataIn = new ObjectInputStream(fileIn); gmn = (GroupManager)dataIn.readObject(); dataIn.close(); fileIn.close(); }catch (Exception e) {return (GroupManager)null;} try{ f.delete(); }catch(Exception e){return gmn;} return gmn; } /** to write the prior knowledge in file. @param file in to write @param prior knowledge @return void @author C. Couturier @version %I%, %U%*/ /* public void writePK(File fvs, PriorKnowledge pk) { try{ FileOutputStream fileOut = new FileOutputStream(fvs); ObjectOutputStream dataOut = new ObjectOutputStream(fileOut); dataOut.writeObject((PriorKnowledge)pk); dataOut.close(); fileOut.close(); } catch (Exception e){;} File fileout = compress(fvs, ".pk"); } /** to read the prior knowledge useful for Amares @param file to read @return prior knowledge @return void @author C. Couturier @version %I%, %U%*/ /* public PriorKnowledge readPK(File fpk) { PriorKnowledge pk =(PriorKnowledge)null; if (!(checkExtension("pk", fpk))) return pk; File f = uncompress(fpk); try{ FileInputStream fileIn = new FileInputStream(f); ObjectInputStream dataIn = new ObjectInputStream(fileIn); pk = (PriorKnowledge)dataIn.readObject(); dataIn.close(); fileIn.close(); }catch (Exception e) {return (PriorKnowledge)null;} try{ f.delete(); }catch(Exception e){return pk;} return pk; } /** write the starting values in file. The starting values are the values used by Amares to initialize the process @param file in to write @param starting values @return void @author C. Couturier @version %I%, %U%*/ /* public void writeSV(File fvs, StartingValues sv) { try{ FileOutputStream fileOut = new FileOutputStream(fvs); ObjectOutputStream dataOut = new ObjectOutputStream(fileOut); dataOut.writeObject((StartingValues)sv); dataOut.close(); fileOut.close(); } catch (Exception e){;} File fileout = compress(fvs, ".sv"); } /** to read the starting values useful for Amares @param file to read @return starting values [2 (Xcoordinates/Ycoordinates)][2*nbpoints] --> a starting value is composed of 2 points (spot of the top of the peak, spot of the half height of the peak) @return void @author C. Couturier @version %I%, %U%*/ /* public StartingValues readSV(File fvs) { StartingValues sv =(StartingValues)null; if (!(checkExtension("sv", fvs))) return sv; File f = uncompress(fvs); try{ FileInputStream fileIn = new FileInputStream(f); ObjectInputStream dataIn = new ObjectInputStream(fileIn); sv = (StartingValues)dataIn.readObject(); dataIn.close(); fileIn.close(); }catch (Exception e) {return (StartingValues)null;} try{ f.delete(); }catch(Exception e){return sv;} return sv; } /** to check out whether has an extension @param extension @param file name @author C. Couturier @version %I%, %U%*/ private boolean checkExtension(String extension, File file) { int i = file.getName().lastIndexOf('.'); String ext = null; if (i > 0 && i < file.getName().length() - 1) { ext = file.getName().substring(i + 1).toLowerCase(); } else { return false; } if (ext.equals(extension)) { return true; } return false; } }