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.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
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
00113 if (!beginPath.endsWith(File.separator)) {
00114 beginPath = beginPath+File.separator;
00115 }
00116
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();
00126
00127
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 }