FileStoredOpenMISFile.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  * 
00027  */
00028 
00029 package org.openmobileis.synchro.openmsp.server.util;
00030 
00031 import java.io.*;
00032 
00033 import org.openmobileis.common.util.UniqueIdGenerator;
00034 import org.openmobileis.common.util.file.FileUtilities;
00035 
00043 public class FileStoredOpenMISFile implements OpenMISFile {
00044   static final long serialVersionUID = 5521257935120563452L;
00045   public static final String tempDir = "synchrotemp";
00046   protected String fileName;
00047   protected boolean copy = true;
00048   protected java.io.InputStream inputstream = null;
00049 
00053   protected String filePath;
00054   protected String tempFileName;
00055   protected long filedate;
00056   
00057   protected FileStoredOpenMISFile(){}
00058   
00064   protected FileStoredOpenMISFile(InputStream data) throws IOException {
00065         filedate = System.currentTimeMillis();
00066         this.copy = true;
00067         tempFileName = System.getProperty("user.dir")+File.separator+FileStoredOpenMISFile.tempDir+File.separator+UniqueIdGenerator.getManager().getNewStringID();
00068     InputStream sourceFile = new BufferedInputStream(data);
00069     try {
00070       OutputStream destFile = new  BufferedOutputStream(new FileOutputStream(tempFileName));
00071       try {
00072         byte[] buffer = new byte[1024];
00073         int bytes_read;
00074         while(( bytes_read = sourceFile.read(buffer)) != -1) 
00075           destFile.write(buffer, 0, bytes_read);
00076       } finally {
00077         destFile.flush();
00078         destFile.close();
00079       }
00080     } finally {
00081       sourceFile.close();
00082     }
00083   }
00084 
00091   public FileStoredOpenMISFile(String completeName, InputStream data) throws IOException {
00092     this(data);
00093     completeName = FileUtilities.convertFileNameToSystem(completeName);
00094     fileName = FileUtilities.getNameFromCompleteFileName(completeName);
00095     filePath = FileUtilities.getPathFromCompleteFileName(completeName);
00096  }
00097 
00098   public FileStoredOpenMISFile(String filepath, String filename) throws IOException {
00099             this.fileName = filename;
00100             this.filePath = FileUtilities.convertFileNameToSystem(filepath);  
00101             this.tempFileName = FileUtilities.convertFileNameToSystem(filepath+filename);
00102             this.copy=false;
00103   }
00104 
00105   public FileStoredOpenMISFile(String name, String path, InputStream data) throws IOException {
00106     this(data);
00107     fileName = name;
00108     filePath = path;
00109   }
00110 
00114   public String getFileName() {
00115     return fileName;
00116   }
00117 
00121   public java.io.InputStream getFileDataStream() throws IOException {
00122           if (this.inputstream != null) this.inputstream.close();
00123           this.inputstream = new FileInputStream(tempFileName);
00124           return new BufferedInputStream(this.inputstream);
00125   }
00126 
00130   public String getFilePath() {
00131     return filePath;
00132   }
00133 
00134   public long getFileDate() {
00135     return filedate;
00136   }
00137 
00138   public void setFileDate(java.util.Date date)  {
00139     filedate = date.getTime();
00140   }
00141 
00145   public String getFileCompleteName() {
00146     return filePath+fileName;
00147   }
00148 
00149 
00153   public long getFileLength()  {
00154           if (tempFileName != null)     {
00155                   File file = new File(tempFileName);
00156                   return file.length();
00157           }
00158           return 0;
00159   }
00160 
00164   public byte[] getFileData() throws IOException {
00165           if (tempFileName != null)     {
00166                   return FileUtilities.readFile(tempFileName);
00167           }
00168           return new byte[0];
00169   }
00170   
00171   public void removeTempFile()throws IOException        {
00172           if (this.inputstream != null) this.inputstream.close();
00173           this.inputstream = null;
00174           if (tempFileName != null && copy)     {
00175                   File file = new File(tempFileName);
00176                   file.delete();
00177           }
00178   }
00179 
00180   public int hashCode() {
00181     return getFileCompleteName().hashCode();
00182   }
00183 
00184   public boolean equals(Object obj) {
00185     if (obj instanceof FileStoredOpenMISFile)  {
00186       return ((FileStoredOpenMISFile)obj).getFileCompleteName().equals(this.getFileCompleteName());
00187     }
00188     return false;
00189   }
00190 
00191 
00192 
00193 }

Generated on Mon Jan 14 17:29:45 2008 for OpenMobileIS by  doxygen 1.5.4