source: branches/0.17.x/abcl/src/org/armedbear/lisp/ZipCache.java

Last change on this file was 11998, checked in by ehuelsmann, 16 years ago

Add a cache for opened '.zip' files.

Before this change, consecutive calls to
loadCompiledFunction() would open the same
zip file over and over.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.5 KB
Line 
1/*
2 * ZipCache.java
3 *
4 * Copyright (C) 2009 Erik Huelsmann
5 *
6 * $Id: ZipCache.java 11998 2009-06-06 17:46:59Z ehuelsmann $
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 *
22 * As a special exception, the copyright holders of this library give you
23 * permission to link this library with independent modules to produce an
24 * executable, regardless of the license terms of these independent
25 * modules, and to copy and distribute the resulting executable under
26 * terms of your choice, provided that you also meet, for each linked
27 * independent module, the terms and conditions of the license of that
28 * module.  An independent module is a module which is not derived from
29 * or based on this library.  If you modify this library, you may extend
30 * this exception to your version of the library, but you are not
31 * obligated to do so.  If you do not wish to do so, delete this
32 * exception statement from your version.
33 */
34
35package org.armedbear.lisp;
36
37import java.io.IOException;
38import java.util.HashMap;
39import java.util.Map;
40import java.util.zip.ZipFile;
41
42/**
43 *
44 * @author Erik
45 */
46class ZipCache {
47
48    static Map<String, Entry> zips = new HashMap<String, Entry>();
49   
50    synchronized static ZipFile getZip(String name) throws IOException {
51        Entry zip = zips.get(name);
52       
53        if (zip == null)
54            zips.put(name, zip = new Entry(new ZipFile(name)));
55
56        zip.refcount++;
57        return zip.value;
58    }
59   
60    synchronized static void removeZip(String name) throws IOException {
61        Entry zip = zips.get(name);
62
63        if (zip == null)
64            return;
65       
66        zip.refcount--;
67        if (zip.refcount == 0) {
68            zip.value.close();
69            zips.remove(name);
70        }
71    }
72
73    static class Entry {
74        ZipFile value;
75        int refcount = 0;
76
77        Entry(ZipFile v) {
78            value = v;
79        }
80    }
81
82}
Note: See TracBrowser for help on using the repository browser.