1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
9 /** A ByteStream encapsulates a <i>source</i> of a stream of bytes --
10 * currently either a local file or a Base64-encoded XMLRPC/SOAP element */
11 public class ByteStream extends JS.Obj {
13 private byte[] bytes = null;
14 private File file = null;
16 private ByteStream() { setSeal(true); }
17 ByteStream(String filename) { this(); this.file = new File(filename); }
18 ByteStream(byte[] bytes) { this(); this.bytes = bytes; }
20 public String toString() { return "ByteStream, source=" + (file == null ? "memory" : file.getAbsolutePath()); }
22 public Object get(Object name) {
23 if (name.equals("getUTF")) return getUTF;
24 else if (name.equals("getDOM")) return getDOM;
25 else if (name.equals("fileName")) return file == null ? null : file.getAbsolutePath();
29 private class XMLHelper extends XML {
30 Vector obStack = new Vector();
32 public XMLHelper() { super(BUFFER_SIZE); }
34 public void startElement(XML.Element c) throws XML.SchemaException {
36 o.put("$name", c.localName);
37 for(int i=0; i<c.len; i++) o.put(c.keys[i], c.vals[i]);
38 o.put("$numchildren", new Integer(0));
39 obStack.addElement(o);
42 public void endElement(XML.Element c) throws XML.SchemaException {
43 if (obStack.size() == 1) return;
44 JS me = (JS)obStack.lastElement();
45 obStack.setSize(obStack.size() - 1);
46 JS parent = (JS)obStack.lastElement();
47 int numchildren = ((Integer)parent.get("$numchildren")).intValue();
48 parent.put("$numchildren", new Integer(numchildren + 1));
49 parent.put(new Integer(numchildren), me);
52 public void characters(char[] ch, int start, int length) throws XML.SchemaException {
53 String s = new String(ch, start, length);
54 JS parent = (JS)obStack.lastElement();
55 int numchildren = ((Integer)parent.get("$numchildren")).intValue();
56 Object lastChild = parent.get(new Integer(numchildren - 1));
57 if (lastChild instanceof String) {
58 parent.put(new Integer(numchildren - 1), lastChild + s);
60 parent.put("$numchildren", new Integer(numchildren + 1));
61 parent.put(new Integer(numchildren), s);
65 public void whitespace(char[] ch, int start, int length) {}
67 public JS doParse() throws JS.Exn {
69 InputStream is = getInputStream();
70 BufferedReader r = new BufferedReader(new InputStreamReader(is));
72 } catch (XML.XMLException e) {
73 throw new JS.Exn("error parsing XML: " + e.toString());
74 } catch (IOException e) {
75 if (Log.on) Log.log(ByteStream.class, "IO Exception while reading from file");
76 if (Log.on) Log.log(ByteStream.class, e);
77 throw new JS.Exn("error reading from ByteStream");
79 return obStack.size() >= 1 ? (JS)obStack.elementAt(0) : null;
83 public InputStream getInputStream() throws IOException {
85 (InputStream)(new ByteArrayInputStream(bytes)) :
86 (InputStream)(new FileInputStream(file));
89 public void writeTo(OutputStream os) throws IOException {
90 InputStream is = getInputStream();
91 byte[] buf = new byte[1024];
93 int numread = is.read(buf, 0, 1024);
94 if (numread == -1) break;
95 if (Log.on) Log.log(this, "wrote " + numread + " bytes");
96 os.write(buf, 0, numread);
100 // we have to close this because flush() doesn't work on Win32-GCJ
104 private final JS.Callable getDOM = new JS.Callable() {
105 public Object call(JS.Array args) throws JS.Exn {
106 if (args.length() != 0) return null;
107 return new XMLHelper().doParse();
111 private final JS.Callable getUTF = new JS.Callable() {
112 public Object call(JS.Array args) throws JS.Exn {
113 if (args.length() != 0) return null;
115 CharArrayWriter caw = new CharArrayWriter();
116 InputStream is = getInputStream();
117 BufferedReader r = new BufferedReader(new InputStreamReader(is));
118 char[] buf = new char[1024];
120 int numread = r.read(buf, 0, 1024);
121 if (numread == -1) break;
122 caw.write(buf, 0, numread);
124 return caw.toString();
125 } catch (IOException e) {
126 if (Log.on) Log.log(ByteStream.class, "IO Exception while reading from file");
127 if (Log.on) Log.log(ByteStream.class, e);
128 throw new JS.Exn("error while reading from ByteStream");