MemoryFile.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.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)  // Copy bytes to zipfile
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 }

Generated on Mon Jan 11 21:19:15 2010 for OpenMobileIS by  doxygen 1.5.4