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.file.FileUtilities;
00034
00042 public class MemoryFile implements OpenMISFile {
00043 static final long serialVersionUID = 5521257935120563452L;
00044 protected String fileName;
00045
00049 protected String filePath;
00050 protected byte[] fileData;
00051 protected long filedate;
00052
00053
00054 public MemoryFile() {
00055 }
00056
00062 protected MemoryFile(InputStream data) throws IOException {
00063 this.readStream(data);
00064 }
00065
00066 public MemoryFile(File file) throws IOException {
00067 fileName = file.getName();
00068 filePath = "/";
00069 this.readStream(new BufferedInputStream(new FileInputStream(file)));
00070 }
00071
00078 public MemoryFile(String completeName, InputStream data) throws IOException {
00079 this(data);
00080 completeName = FileUtilities.convertFileNameToSystem(completeName);
00081 fileName = FileUtilities.getNameFromCompleteFileName(completeName);
00082 filePath = FileUtilities.getPathFromCompleteFileName(completeName);
00083 }
00084
00085 public MemoryFile(String completeName, byte[] data) throws IOException {
00086 fileData = data;
00087 completeName = FileUtilities.convertFileNameToSystem(completeName);
00088 fileName = FileUtilities.getNameFromCompleteFileName(completeName);
00089 filePath = FileUtilities.getPathFromCompleteFileName(completeName);
00090 }
00091
00092 public MemoryFile(String name, String path, InputStream data) throws IOException {
00093 this(data);
00094 fileName = name;
00095 filePath = path;
00096 }
00097
00098 private void readStream(InputStream data) throws IOException {
00099 filedate = System.currentTimeMillis();
00100
00101 byte[] buffer = new byte[512];
00102 ByteArrayOutputStream stream = new ByteArrayOutputStream(buffer.length);
00103 int bytes_read;
00104 try {
00105 while(( bytes_read = data.read(buffer)) != -1)
00106 stream.write(buffer, 0, bytes_read);
00107 } finally {
00108 stream.flush();
00109 stream.close();
00110 data.close();
00111 }
00112 fileData = stream.toByteArray();
00113 }
00114
00115
00119 public String getFileName() {
00120 return fileName;
00121 }
00122
00126 public java.io.InputStream getFileDataStream() throws IOException {
00127 return new ByteArrayInputStream(fileData);
00128 }
00129
00133 public String getFilePath() {
00134 return filePath;
00135 }
00136
00137 public long getFileDate() {
00138 return filedate;
00139 }
00140
00141 public void setFileDate(java.util.Date date) {
00142 filedate = date.getTime();
00143 }
00144
00148 public String getFileCompleteName() {
00149 return filePath+fileName;
00150 }
00151
00152
00156 public long getFileLength() {
00157 return fileData.length;
00158 }
00159
00163 public byte[] getFileData() throws IOException {
00164 return fileData;
00165 }
00166
00167 public int hashCode() {
00168 return getFileCompleteName().hashCode();
00169 }
00170
00171 public boolean equals(Object obj) {
00172 if (obj instanceof MemoryFile) {
00173 return ((MemoryFile)obj).getFileCompleteName().equals(this.getFileCompleteName());
00174 }
00175 return false;
00176 }
00177
00178 public void setFileName(String fileName) {
00179 this.fileName = fileName;
00180 }
00181
00182 public void setFilePath(String filePath) {
00183 this.filePath = filePath;
00184 }
00185
00186
00187
00188 }