00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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 }