fixed serious livelock bug in Directory.java
[org.ibex.core.git] / src / org / ibex / js / Directory.java
1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] 
2 package org.ibex.js; 
3
4 import org.ibex.util.*; 
5 import java.util.*;
6 import java.io.*;
7
8 // FEATURE: support for move
9 // FEATURE: support for bytestreams
10 // FEATURE: cache directories so we can do equality checking on them?
11 // FEATURE: autoconvert "true" to true and "0.3" to 0.3 on readback
12
13 /** 
14  * A crude mechanism for using a filesystem as object storage.
15  *
16  *  This object represents a directory; writing a string, number, or
17  *  boolean to any of its properties will create a file with the
18  *  (encoded) property name as its filename and the "stringified"
19  *  value as its contents.
20  *
21  *  Writing 'null' to one of this object's properties will
22  *  [recursively if necessary] delete the corresponding directory
23  *  entry.
24  *  
25  *  Writing any other object to one of this object's properties will
26  *  create a new Directory object and copy the other object's keys()
27  *  into the new Directory.  This means that assigning one directory
28  *  to a property of another directory will <i>copy</i> the directory,
29  *  not move it.  There is currently no way to move directories.
30  *
31  *  If an object is written to a property that already has an entry,
32  *  the old one is deleted (equivalent to writing 'null') first.
33  * 
34  *  WARNING: when instantiating a Directory object with a file
35  *  argument that points to a non-directory File, this class will
36  *  delete that file and create a directory!
37  */
38 public class Directory extends JS {
39
40     File f;
41
42     /** 
43      *  Create the directory object.  Existing directories will be
44      *  preserved; if a file is present it will be obliterated.
45      */ 
46     public Directory(File f) throws IOException {
47         this.f = f;
48         if (!f.exists()) new Directory(new File(f.getParent()));
49         if (!f.isDirectory()) destroy(f);
50         f.mkdirs();
51     }
52
53     private static void destroy(File f) throws IOException {
54         if (!f.exists()) return;
55         if (f.isDirectory()) {
56             String[] entries = f.list();
57             for(int i=0; i<entries.length; i++) destroy(new File(f.getAbsolutePath() + File.separatorChar + entries[i]));
58         }
59         f.delete();
60     }
61
62     public void put(Object key0, Object val) throws JSExn {
63         try {
64             if (key0 == null) return;
65             String key = toString(key0);
66             File f2 = new File(f.getAbsolutePath() + File.separatorChar + FileNameEncoder.encode(key));
67             destroy(f2);
68             if (val == null) return;
69             if (val instanceof JS) {
70                 Directory d2 = new Directory(f2);
71                 Enumeration e = ((JS)val).keys();
72                 while(e.hasMoreElements()) {
73                     String k = (String)e.nextElement();
74                     Object v = ((JS)val).get(k);
75                     d2.put(k, v);
76                 }
77             } else {
78                 OutputStream out = new FileOutputStream(f2);
79                 Writer w = new OutputStreamWriter(out);
80                 w.write(toString(val));
81                 w.flush();
82                 out.close();
83             }
84         } catch (IOException ioe) {
85             throw new JSExn.IO(ioe);
86         }
87     }
88
89     public Object get(Object key0) throws JSExn {
90         try {
91             if (key0 == null) return null;
92             String key = toString(key0);
93             File f2 = new File(f.getAbsolutePath() + File.separatorChar + FileNameEncoder.encode(key));
94             if (!f2.exists()) return null;
95             if (f2.isDirectory()) return new Directory(f2);
96             char[] chars = new char[((int)f2.length()) * 2];
97             int numchars = 0;
98             Reader r = new InputStreamReader(new FileInputStream(f2));
99             while(true) {
100                 int numread = r.read(chars, numchars, chars.length - numchars);
101                 if (numread == -1) return new String(chars, 0, numchars);
102                 numchars += numread;
103             }
104         } catch (IOException ioe) {
105             throw new JSExn.IO(ioe);
106         }
107     }
108
109     public Enumeration keys() {
110         final String[] elements = f.list();
111         return new Enumeration() {
112                 int i = 0;
113                 public boolean hasMoreElements() { return i < elements.length; }
114                 public Object nextElement() { return FileNameEncoder.decode(elements[i++]); }
115             };
116     }
117 }