X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FDirectory.java;h=0a77f41f4a4c79593977f559004fe62eaddda265;hb=a6fb49b55117ea4cf330b412d2a2fff403fcd053;hp=17adab7e464290a874537303b00b39f35409aa8e;hpb=d111fd7138333e05149dc2bfd486f65d73af60ec;p=org.ibex.js.git diff --git a/src/org/ibex/js/Directory.java b/src/org/ibex/js/Directory.java index 17adab7..0a77f41 100644 --- a/src/org/ibex/js/Directory.java +++ b/src/org/ibex/js/Directory.java @@ -1,8 +1,11 @@ -// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] +// Copyright 2000-2005 the Contributors, as shown in the revision logs. +// Licensed under the Apache Public Source License 2.0 ("the License"). +// You may not use this file except in compliance with the License. + package org.ibex.js; import org.ibex.util.*; -import java.util.*; +import org.ibex.io.*; import java.io.*; // FEATURE: support for move @@ -35,7 +38,7 @@ import java.io.*; * argument that points to a non-directory File, this class will * delete that file and create a directory! */ -public class Directory extends JS { +public class Directory extends JS.Immutable { File f; @@ -43,6 +46,7 @@ public class Directory extends JS { * Create the directory object. Existing directories will be * preserved; if a file is present it will be obliterated. */ + public Directory(File f) throws IOException { this.f = f; if (!f.exists()) new Directory(new File(f.getParent())); @@ -59,70 +63,64 @@ public class Directory extends JS { f.delete(); } - public void put(Object key0, Object val) throws JSExn { + public void put(JS key0, JS val) throws JSExn { try { if (key0 == null) return; - String key = toString(key0); - File f2 = new File(f.getAbsolutePath() + File.separatorChar + FileNameEncoder.encode(key)); + String key = JSU.toString(key0); + File f2 = new File(f.getAbsolutePath() + File.separatorChar + Encode.toFilename(key)); destroy(f2); if (val == null) return; - if (val instanceof JS) { + if (val instanceof org.ibex.io.Fountain) { + Stream stream = ((org.ibex.io.Fountain)val).getStream(); + Stream out = new Stream(null, new FileOutputStream(f2)); + stream.transcribe(out); + out.close(); + } else if (val instanceof JSPrimitive) { + OutputStream out = new FileOutputStream(f2); + Writer w = new OutputStreamWriter(out); + w.write(JSU.toString(val)); + w.flush(); + out.close(); + } else { Directory d2 = new Directory(f2); - Enumeration e = ((JS)val).keys(); - while(e.hasMoreElements()) { - String k = (String)e.nextElement(); - Object v = ((JS)val).get(k); + JS.Enumeration e = val.keys(); + while(e.hasNext()) { + JS k = e.next(); + JS v = val.get(k); d2.put(k, v); } - } else { - OutputStream out = new FileOutputStream(f2); - try { - Writer w = new OutputStreamWriter(out); - w.write(toString(val)); - w.flush(); - } finally { - out.close(); - } } } catch (IOException ioe) { throw new JSExn.IO(ioe); } } - public Object get(Object key0) throws JSExn { - FileInputStream fis = null; + public JS get(JS key0) throws JSExn { try { if (key0 == null) return null; - String key = toString(key0); - File f2 = new File(f.getAbsolutePath() + File.separatorChar + FileNameEncoder.encode(key)); + String key = JSU.toString(key0); + File f2 = new File(f.getAbsolutePath() + File.separatorChar + Encode.toFilename(key)); if (!f2.exists()) return null; if (f2.isDirectory()) return new Directory(f2); - char[] chars = new char[((int)f2.length()) * 2]; + char[] chars = new char[((int)f2.length()) * 4 + 10]; int numchars = 0; - fis = new FileInputStream(f2); - Reader r = new InputStreamReader(fis); + Reader r = new InputStreamReader(new FileInputStream(f2)); while(true) { int numread = r.read(chars, numchars, chars.length - numchars); - if (numread == -1) return new String(chars, 0, numchars); + if (numread == -1) return JSU.S(new String(chars, 0, numchars)); numchars += numread; } } catch (IOException ioe) { throw new JSExn.IO(ioe); - } finally { - try { - if (fis != null) fis.close(); - } catch (IOException ioe) { - throw new JSExn.IO(ioe); - } } } - public Enumeration keys() { + public JS.Enumeration keys() { final String[] elements = f.list(); - return new Enumeration() { + return new JS.Enumeration(null) { int i = 0; - public boolean hasMoreElements() { return i < elements.length; } - public Object nextElement() { return FileNameEncoder.decode(elements[i++]); } + public boolean _hasNext() { return i < elements.length; } + public JS _next() { return JSU.S(Encode.fromFilename(elements[i++])); } }; } }