From 68d5439acb83a212fb346a4e2d82260d7febc127 Mon Sep 17 00:00:00 2001 From: megacz Date: Fri, 30 Jan 2004 06:47:42 +0000 Subject: [PATCH] 2002/05/29 23:54:51 darcs-hash:20040130064742-2ba56-740c8300dd4dcc39fb2903dbd50f2efb6e138b5d.gz --- src/org/xwt/ByteStream.java | 146 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 src/org/xwt/ByteStream.java diff --git a/src/org/xwt/ByteStream.java b/src/org/xwt/ByteStream.java new file mode 100644 index 0000000..cd3982a --- /dev/null +++ b/src/org/xwt/ByteStream.java @@ -0,0 +1,146 @@ +// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL] +package org.xwt; +import java.io.*; +import java.util.*; +import org.xwt.util.*; +import org.mozilla.javascript.*; + +/** A ByteStream encapsulates a source of a stream of bytes -- + * currently either a local file or a Base64-encoded XMLRPC/SOAP element */ + +public class ByteStream extends JSObject { + + private byte[] bytes = null; + private File file = null; + + private ByteStream() { setSeal(true); } + + public String toString() { return "ByteStream, source=" + file.getAbsolutePath(); } + public String getClassName() { return "ByteStream"; } + + ByteStream(String filename) { + this(); + this.file = new File(filename); + } + + ByteStream(byte[] bytes) { + this(); + this.bytes = bytes; + } + + public Object get(String name, Scriptable start) { + if (name == null) return null; + else if (name.equals("getUTF")) return getUTF; + else if (name.equals("getDOM")) return getDOM; + else if (name.equals("fileName")) return file == null ? null : file.getAbsolutePath(); + else return null; + } + + /** Helper class for defining functions. */ + private abstract class JSFunction extends JSObject implements Function { + JSFunction() { this.setSeal(true); } + public Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) { return null; } + } + + private class XMLHelper extends XML { + Vector obStack = new Vector(); + + public void startElement(String name, String[] keys, Object[] vals, int line, int col) throws SAXException { + JSObject o = new JSObject(); + o.put("$name", null, name); + for(int i=0; i= 1 ? (Scriptable)obStack.elementAt(0) : null; + } + } + + public InputStream getInputStream() throws IOException { + return file == null ? + (InputStream)(new ByteArrayInputStream(bytes)) : + (InputStream)(new FileInputStream(file)); + } + + public void writeTo(OutputStream os) throws IOException { + InputStream is = getInputStream(); + byte[] buf = new byte[1024]; + while(true) { + int numread = is.read(buf, 0, 1024); + if (numread == -1) break; + if (Log.on) Log.log(this, "wrote " + numread + " bytes"); + os.write(buf, 0, numread); + } + os.flush(); + + // we have to close this because flush() doesn't work on Win32-GCJ + os.close(); + } + + private final JSFunction getDOM = new JSFunction() { + public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException { + if (args.length != 0) return null; + return new XMLHelper().doParse(); + } + }; + + private final JSFunction getUTF = new JSFunction() { + public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException { + if (args.length != 0) return null; + try { + CharArrayWriter caw = new CharArrayWriter(); + InputStream is = getInputStream(); + BufferedReader r = new BufferedReader(new InputStreamReader(is)); + char[] buf = new char[1024]; + while(true) { + int numread = r.read(buf, 0, 1024); + if (numread == -1) break; + caw.write(buf, 0, numread); + } + return caw.toString(); + } catch (IOException e) { + if (Log.on) Log.log(ByteStream.class, "IO Exception while reading from file"); + if (Log.on) Log.log(ByteStream.class, e); + throw new JavaScriptException("error while reading from ByteStream"); + } + } + }; + +} + -- 1.7.10.4