serious memory leak in the producer subsystem fixed
[mir.git] / source / mir / generator / tal / CachingFileLoader.java
index 41256ab..c9f042b 100755 (executable)
-/*\r
- * Copyright (C) 2001, 2002 The Mir-coders group\r
- *\r
- * This file is part of Mir.\r
- *\r
- * Mir is free software; you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; either version 2 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * Mir is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with Mir; if not, write to the Free Software\r
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
- *\r
- * In addition, as a special exception, The Mir-coders gives permission to link\r
- * the code of this program with  any library licensed under the Apache Software License,\r
- * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library\r
- * (or with modified versions of the above that use the same license as the above),\r
- * and distribute linked combinations including the two.  You must obey the\r
- * GNU General Public License in all respects for all of the code used other than\r
- * the above mentioned libraries.  If you modify this file, you may extend this\r
- * exception to your version of the file, but you are not obligated to do so.\r
- * If you do not wish to do so, delete this exception statement from your version.\r
- */\r
-package mir.generator.tal;\r
-\r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.FileNotFoundException;\r
-import java.io.InputStream;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Vector;\r
-\r
-public class CachingFileLoader {\r
-  private Map cachedObjects;\r
-  private List history;\r
-  private int capacity;\r
-  private CachedFileObjectFactory factory;\r
-\r
-  public CachingFileLoader(int aCapacity, CachedFileObjectFactory aFactory) {\r
-    capacity = aCapacity;\r
-    cachedObjects = new HashMap();\r
-    history = new Vector();\r
-    factory = aFactory;\r
-  }\r
-\r
-  private void cacheObject(String aFilename, long aLastModified, Object anObject) {\r
-    synchronized (this) {\r
-      history.remove(aFilename);\r
-      history.add(aFilename);\r
-      while (history.size()>capacity && history.size()>0) {\r
-        history.remove(0);\r
-      }\r
-      cachedObjects.put(aFilename, new CachedObject(aFilename, aLastModified, anObject));\r
-    }\r
-  }\r
-\r
-  private Object constructObject(String aFilename) throws FileNotFoundException {\r
-    File file = new File(aFilename);\r
-    FileInputStream inputStream = new FileInputStream(file);\r
-    try {\r
-      Object result = factory.constructObject(inputStream);\r
-      cacheObject(aFilename, file.lastModified(), result);\r
-\r
-      return result;\r
-    }\r
-    finally {\r
-      try {\r
-        inputStream.close();\r
-      }\r
-      catch (Throwable t) {\r
-      }\r
-    }\r
-  }\r
-\r
-  private void revatilize(String aFilename) {\r
-    synchronized (this) {\r
-      history.remove(aFilename);\r
-      history.add(aFilename);\r
-    }\r
-  }\r
-\r
-  private class CachedObject {\r
-    private String filename;\r
-    private long lastModified;\r
-    private Object object;\r
-\r
-    public CachedObject(String aFilename, long aLastModified, Object anObject) {\r
-      filename = aFilename;\r
-      lastModified = aLastModified;\r
-      object = anObject;\r
-    }\r
-\r
-    public Object getObject() {\r
-      return object;\r
-    }\r
-\r
-    public String getFilename() {\r
-      return filename;\r
-    }\r
-\r
-    public long getLastModified() {\r
-      return lastModified;\r
-    }\r
-  }\r
-\r
-  public Object retrieveFile(String aFilename) throws FileNotFoundException {\r
-    synchronized (this) {\r
-      if (!cachedObjects.containsKey(aFilename)) {\r
-        return constructObject(aFilename);\r
-      }\r
-\r
-      CachedObject cachedObject = (CachedObject) cachedObjects.get(aFilename);\r
-      File file = new File(aFilename);\r
-      if (file.lastModified() != cachedObject.getLastModified()) {\r
-        return constructObject(aFilename);\r
-      }\r
-\r
-      revatilize(aFilename);\r
-\r
-      return cachedObject.getObject();\r
-    }\r
-  }\r
-\r
-  public interface CachedFileObjectFactory {\r
-    Object constructObject(InputStream aStream);\r
-  }\r
+/*
+ * Copyright (C) 2001, 2002 The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with  any library licensed under the Apache Software License,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * and distribute linked combinations including the two.  You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries.  If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
+ */
+
+package mir.generator.tal;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class CachingFileLoader {
+  private Map cachedObjects;
+  private List history;
+  private int capacity;
+  private CachedFileObjectFactory factory;
+
+  public CachingFileLoader(int aCapacity, CachedFileObjectFactory aFactory) {
+    capacity = aCapacity;
+    cachedObjects = new HashMap();
+    history = new ArrayList();
+    factory = aFactory;
+  }
+
+  private void cacheObject(String aFilename, long aLastModified, Object anObject) {
+    synchronized (this) {
+      history.remove(aFilename);
+      history.add(aFilename);
+      while (history.size()>capacity && history.size()>0) {
+        history.remove(0);
+      }
+      cachedObjects.put(aFilename, new CachedObject(aFilename, aLastModified, anObject));
+    }
+  }
+
+  private Object constructObject(String aFilename) throws FileNotFoundException {
+    File file = new File(aFilename);
+    BufferedInputStream inputStream = new BufferedInputStream(
+      new FileInputStream(file), 8192);
+    try {
+      Object result = factory.constructObject(inputStream);
+      cacheObject(aFilename, file.lastModified(), result);
+
+      return result;
+    }
+    finally {
+      try {
+        inputStream.close();
+      }
+      catch (Throwable t) {
+      }
+    }
+  }
+
+  private void revatilize(String aFilename) {
+    synchronized (this) {
+      history.remove(aFilename);
+      history.add(aFilename);
+    }
+  }
+
+  private class CachedObject {
+    private String filename;
+    private long lastModified;
+    private Object object;
+
+    public CachedObject(String aFilename, long aLastModified, Object anObject) {
+      filename = aFilename;
+      lastModified = aLastModified;
+      object = anObject;
+    }
+
+    public Object getObject() {
+      return object;
+    }
+
+    public String getFilename() {
+      return filename;
+    }
+
+    public long getLastModified() {
+      return lastModified;
+    }
+  }
+
+  public Object retrieveFile(String aFilename) throws FileNotFoundException {
+    synchronized (this) {
+      if (!cachedObjects.containsKey(aFilename)) {
+        return constructObject(aFilename);
+      }
+
+      CachedObject cachedObject = (CachedObject) cachedObjects.get(aFilename);
+      File file = new File(aFilename);
+      if (file.lastModified() != cachedObject.getLastModified()) {
+        return constructObject(aFilename);
+      }
+
+      revatilize(aFilename);
+
+      return cachedObject.getObject();
+    }
+  }
+
+  public interface CachedFileObjectFactory {
+    Object constructObject(InputStream aStream);
+  }
 }
\ No newline at end of file