1 32 33 package com.imagero.uio.buffer.fm; 34 35 import com.imagero.uio.RandomAccess; 36 import com.imagero.uio.RandomAccessFactory; 37 import com.imagero.uio.RandomAccessRO; 38 import com.imagero.uio.buffer.BufferManager; 39 import com.imagero.uio.buffer.MutableBufferManager; 40 import com.imagero.uio.io.IOutils; 41 42 import java.awt.event.ActionEvent ; 43 import java.awt.event.ActionListener ; 44 import java.io.File ; 45 import java.io.IOException ; 46 import java.util.Hashtable ; 47 import java.util.Vector ; 48 49 54 public class OpenFileManager { 55 56 static final String GET = "get"; 57 static final String OPEN = "open"; 58 static final String CLOSE = "close"; 59 60 static boolean debug; 61 62 public static boolean isDebug() { 63 return debug; 64 } 65 66 public static void setDebug(boolean debug) { 67 OpenFileManager.debug = debug; 68 } 69 70 Vector v = new Vector (); 71 Hashtable ros = new Hashtable (); 72 73 int maxOpenCount = 100; 74 75 ActionListener handler = new ActionListener () { 76 public void actionPerformed(ActionEvent e) { 77 String actionCommand = e.getActionCommand(); 78 Object manager = e.getSource(); 79 if (actionCommand == GET || actionCommand == OPEN) { 80 v.removeElement(manager); 81 v.addElement(manager); 82 } 83 else if(actionCommand == CLOSE) { 84 v.removeElement(manager); 85 } 86 } 87 }; 88 89 95 public RandomAccessRO createRO(File f) throws IOException { 96 RandomAccessRO ro = openFileRO(f); 97 return ro; 98 } 99 100 106 public RandomAccess create(File f) throws IOException { 107 RandomAccess ra = openFile(f); 108 return ra; 109 } 110 111 private RandomAccessRO openFileRO(File f) throws IOException { 112 BufferManager manager = createBufferManager(f, false); 113 RandomAccessRO ro = RandomAccessFactory.createBufferedRO(manager); 114 ros.put(ro, manager); 115 v.addElement(manager); 116 checkSize(); 117 118 return ro; 119 } 120 121 private RandomAccess openFile(File f) throws IOException { 122 BufferManager manager = createBufferManager(f, true); 123 RandomAccess ra = RandomAccessFactory.createBuffered((MutableBufferManager) manager); 124 ros.put(ra, manager); 125 v.addElement(manager); 126 checkSize(); 127 128 return ra; 129 } 130 131 private void checkSize() { 132 if (v.size() > maxOpenCount) { 133 int closeCount = v.size() - maxOpenCount; 134 for (int i = 0; i < v.size(); i++) { 135 BufferManager manager = (BufferManager) v.elementAt(i); 136 if (manager instanceof FMBufferManager) { 137 if (((FMBufferManager) manager).canClose()) { 138 manager.close(); 139 v.removeElementAt(0); 140 if (--closeCount == 0) { 141 break; 142 } 143 } 144 } 145 } 146 } 147 } 148 149 152 public void close(RandomAccessRO ro) { 153 Object mgr = ros.remove(ro); 154 if (mgr == null) { 155 throw new NullPointerException (); 156 } 157 IOutils.closeStream(ro); 158 v.removeElement(mgr); 159 if (mgr instanceof FMBufferManagerRO) { 160 ((FMBufferManagerRO) mgr)._close(); 161 } 162 else if (mgr instanceof FMBufferManager) { 163 ((FMBufferManager) mgr)._close(); 164 } 165 } 166 167 170 public int getMaxOpenCount() { 171 return maxOpenCount; 172 } 173 174 177 public void setMaxOpenCount(int maxOpenCount) { 178 this.maxOpenCount = maxOpenCount; 179 } 180 181 private BufferManager createBufferManager(File f, boolean writeable) throws IOException { 182 if (writeable) { 183 return new FMBufferManager(f, handler); 184 } 185 else { 186 return new FMBufferManagerRO(f, handler); 187 } 188 } 189 } 190 | Popular Tags |