Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

MemoryFile.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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 
00057   protected MemoryFile(InputStream data) throws IOException {
00058     filedate = System.currentTimeMillis();
00059 
00060     byte[] buffer = new byte[512];
00061     ByteArrayOutputStream stream = new ByteArrayOutputStream(buffer.length);
00062     int bytes_read;
00063     try {
00064       while(( bytes_read = data.read(buffer)) != -1)  // Copy bytes to zipfile
00065         stream.write(buffer, 0, bytes_read);
00066     } finally {
00067       stream.flush();
00068       stream.close();
00069     }
00070     fileData =  stream.toByteArray();
00071   }
00072 
00073   public MemoryFile(String completeName, InputStream data) throws IOException {
00074     this(data);
00075     completeName = FileUtilities.convertFileNameToSystem(completeName);
00076     fileName = FileUtilities.getNameFromCompleteFileName(completeName);
00077     filePath = FileUtilities.getPathFromCompleteFileName(completeName);
00078   }
00079 
00080   public MemoryFile(String completeName, byte[] data) throws IOException {
00081     fileData = data;
00082     completeName = FileUtilities.convertFileNameToSystem(completeName);
00083     fileName = FileUtilities.getNameFromCompleteFileName(completeName);
00084     filePath = FileUtilities.getPathFromCompleteFileName(completeName);
00085   }
00086 
00087   public MemoryFile(String name, String path, InputStream data) throws IOException {
00088     this(data);
00089     fileName = name;
00090     filePath = path;
00091   }
00092 
00093 
00097   public String getFileName() {
00098     return fileName;
00099   }
00100 
00104   public java.io.InputStream getFileDataStream()  {
00105     return new ByteArrayInputStream(fileData);
00106   }
00107 
00111   public String getFilePath() {
00112     return filePath;
00113   }
00114 
00115   public long getFileDate() {
00116     return filedate;
00117   }
00118 
00119   public void setFileDate(java.util.Date date)  {
00120     filedate = date.getTime();
00121   }
00122 
00126   public String getFileCompleteName() {
00127     return filePath+fileName;
00128   }
00129 
00130 
00134   public int getFileLength()  {
00135     return fileData.length;
00136   }
00137 
00141   public byte[] getFileData() {
00142     return fileData;
00143   }
00144 
00145   public int hashCode() {
00146     return getFileCompleteName().hashCode();
00147   }
00148 
00149   public boolean equals(Object obj) {
00150     if (obj instanceof MemoryFile)  {
00151       return ((MemoryFile)obj).getFileCompleteName().equals(this.getFileCompleteName());
00152     }
00153     return false;
00154   }
00155 
00156 
00157 
00158 }

Generated on Wed Dec 14 21:05:34 2005 for OpenMobileIS by  doxygen 1.4.4