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

MemoryFileSystem.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.util.zip.*;
00032 import java.io.*;
00033 import java.util.*;
00034 
00042 public class MemoryFileSystem implements FileSystem {
00043   static final long serialVersionUID = 5521257935120563452L;
00044 
00045   private HashMap fileMap = new HashMap(20);
00046 
00047   public MemoryFileSystem() {
00048   }
00049 
00050 
00051   // Manage zip files
00052   public MemoryFileSystem (ZipInputStream zipFile) throws IOException {
00053     ZipEntry entry;
00054     while ( (entry = zipFile.getNextEntry()) != null) {
00055       String completeName = entry.getName();
00056       OpenMISFile file = createCyberFile(completeName, zipFile);
00057       this.addFile(file);
00058       zipFile.closeEntry();
00059     }
00060     zipFile.close();
00061   }
00062 
00063   public MemoryFileSystem (OpenMISFile filelist[])  {
00064   }
00065 
00069   protected OpenMISFile createCyberFile(String completeName, InputStream input) throws IOException  {
00070     return new MemoryFile(completeName, input);
00071   }
00072 
00073   public void addAll(FileSystem fileSystem) throws IOException {
00074     OpenMISFile[] fileList = fileSystem.getFileList();
00075     for (int i= 0; i<fileList.length; i++)  {
00076       this.addFile(fileList[i]);
00077     }
00078   }
00079 
00080 
00084   public OpenMISFile getFile(String completefileName) {
00085     return  (OpenMISFile)fileMap.get(completefileName);
00086   }
00087 
00092   public void addFile(OpenMISFile file) throws IOException  {
00093     String completeName = file.getFileCompleteName();
00094     fileMap.put(completeName,file);
00095   }
00096 
00097   public OpenMISFile[] getFileList() {
00098     Object[] array = fileMap.values().toArray();
00099     OpenMISFile[] retArray = new OpenMISFile[array.length];
00100     for (int i=0; i<array.length; i++)  {
00101       retArray[i] = (OpenMISFile)array[i];
00102     }
00103     return retArray;
00104   }
00105 
00110   public void saveToDisk(String beginPath) throws IOException {
00111 
00112     // validate beginPath format
00113     if (!beginPath.endsWith(File.separator))  {
00114       beginPath = beginPath+File.separator;
00115     }
00116     // create base dirctory
00117     File beginRep = new File(beginPath);
00118     if (!beginRep.exists())  {
00119       beginRep.mkdirs();
00120     }
00121     Iterator iter = fileMap.values().iterator();
00122     while (iter.hasNext())  {
00123       OpenMISFile file = (OpenMISFile) iter.next();
00124       File rep = new File(beginPath+file.getFilePath());
00125       rep.mkdirs();// create directory if not exist
00126 
00127       // save file
00128       FileOutputStream stream = new FileOutputStream(beginPath+file.getFileCompleteName());
00129       try {
00130         stream.write(file.getFileData());
00131       } finally {
00132         stream.close();
00133       }
00134     }
00135   }
00136 
00137         public int getFileCount()       {
00138                 return fileMap.size();
00139         }
00140 
00144   public void clear() {
00145     fileMap.clear();
00146   }
00147 
00148   public void removeFile(String completefileName) {
00149     fileMap.remove(completefileName);
00150   }
00151 
00152 
00153 }

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