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.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 filedate = System.currentTimeMillis(); 00064 00065 byte[] buffer = new byte[512]; 00066 ByteArrayOutputStream stream = new ByteArrayOutputStream(buffer.length); 00067 int bytes_read; 00068 try { 00069 while(( bytes_read = data.read(buffer)) != -1) // Copy bytes to zipfile 00070 stream.write(buffer, 0, bytes_read); 00071 } finally { 00072 stream.flush(); 00073 stream.close(); 00074 data.close(); 00075 } 00076 fileData = stream.toByteArray(); 00077 } 00078 00085 public MemoryFile(String completeName, InputStream data) throws IOException { 00086 this(data); 00087 completeName = FileUtilities.convertFileNameToSystem(completeName); 00088 fileName = FileUtilities.getNameFromCompleteFileName(completeName); 00089 filePath = FileUtilities.getPathFromCompleteFileName(completeName); 00090 } 00091 00092 public MemoryFile(String completeName, byte[] data) throws IOException { 00093 fileData = data; 00094 completeName = FileUtilities.convertFileNameToSystem(completeName); 00095 fileName = FileUtilities.getNameFromCompleteFileName(completeName); 00096 filePath = FileUtilities.getPathFromCompleteFileName(completeName); 00097 } 00098 00099 public MemoryFile(String name, String path, InputStream data) throws IOException { 00100 this(data); 00101 fileName = name; 00102 filePath = path; 00103 } 00104 00105 00109 public String getFileName() { 00110 return fileName; 00111 } 00112 00116 public java.io.InputStream getFileDataStream() { 00117 return new ByteArrayInputStream(fileData); 00118 } 00119 00123 public String getFilePath() { 00124 return filePath; 00125 } 00126 00127 public long getFileDate() { 00128 return filedate; 00129 } 00130 00131 public void setFileDate(java.util.Date date) { 00132 filedate = date.getTime(); 00133 } 00134 00138 public String getFileCompleteName() { 00139 return filePath+fileName; 00140 } 00141 00142 00146 public int getFileLength() { 00147 return fileData.length; 00148 } 00149 00153 public byte[] getFileData() { 00154 return fileData; 00155 } 00156 00157 public int hashCode() { 00158 return getFileCompleteName().hashCode(); 00159 } 00160 00161 public boolean equals(Object obj) { 00162 if (obj instanceof MemoryFile) { 00163 return ((MemoryFile)obj).getFileCompleteName().equals(this.getFileCompleteName()); 00164 } 00165 return false; 00166 } 00167 00168 00169 00170 }