comparison dwt/browser/InputStream.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 1a8b3cb347e0
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
1 /*******************************************************************************
2 * Copyright (c) 2003, 2007 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 module dwt.browser.InputStream;
12
13 import dwt.dwthelper.utils;
14
15 import dwt.internal.C;
16 import dwt.internal.mozilla.XPCOM;
17 import dwt.internal.mozilla.XPCOMObject;
18 import dwt.internal.mozilla.nsID;
19 import dwt.internal.mozilla.nsIInputStream;
20 import dwt.internal.mozilla.nsISupports;
21
22 class InputStream {
23 XPCOMObject inputStream;
24 int refCount = 0;
25
26 byte[] buffer;
27 int index = 0;
28
29 InputStream (byte[] buffer) {
30 this.buffer = buffer;
31 index = 0;
32 createCOMInterfaces ();
33 }
34
35 int AddRef () {
36 refCount++;
37 return refCount;
38 }
39
40 void createCOMInterfaces () {
41 /* Create each of the interfaces that this object implements */
42 inputStream = new XPCOMObject (new int[] {2, 0, 0, 0, 1, 3, 4, 1}) {
43 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
44 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
45 public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
46 public int /*long*/ method3 (int /*long*/[] args) {return Close ();}
47 public int /*long*/ method4 (int /*long*/[] args) {return Available (args[0]);}
48 public int /*long*/ method5 (int /*long*/[] args) {return Read (args[0], (int)/*64*/args[1], args[2]);}
49 public int /*long*/ method6 (int /*long*/[] args) {return ReadSegments (args[0], args[1], (int)/*64*/args[2], args[3]);}
50 public int /*long*/ method7 (int /*long*/[] args) {return IsNonBlocking (args[0]);}
51 };
52 }
53
54 void disposeCOMInterfaces () {
55 if (inputStream !is null) {
56 inputStream.dispose ();
57 inputStream = null;
58 }
59 }
60
61 int /*long*/ getAddress () {
62 return inputStream.getAddress ();
63 }
64
65 int QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) {
66 if (riid is 0 || ppvObject is 0) return XPCOM.NS_ERROR_NO_INTERFACE;
67 nsID guid = new nsID ();
68 XPCOM.memmove (guid, riid, nsID.sizeof);
69
70 if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) {
71 XPCOM.memmove (ppvObject, new int /*long*/[] {inputStream.getAddress ()}, C.PTR_SIZEOF);
72 AddRef ();
73 return XPCOM.NS_OK;
74 }
75 if (guid.Equals (nsIInputStream.NS_IINPUTSTREAM_IID)) {
76 XPCOM.memmove (ppvObject, new int /*long*/[] {inputStream.getAddress ()}, C.PTR_SIZEOF);
77 AddRef ();
78 return XPCOM.NS_OK;
79 }
80 XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF);
81 return XPCOM.NS_ERROR_NO_INTERFACE;
82 }
83
84 int Release () {
85 refCount--;
86 if (refCount is 0) disposeCOMInterfaces ();
87 return refCount;
88 }
89
90 /* nsIInputStream implementation */
91
92 int Close () {
93 buffer = null;
94 index = 0;
95 return XPCOM.NS_OK;
96 }
97
98 int Available (int /*long*/ _retval) {
99 int available = buffer is null ? 0 : buffer.length - index;
100 XPCOM.memmove (_retval, new int[] {available}, 4);
101 return XPCOM.NS_OK;
102 }
103
104 int Read(int /*long*/ aBuf, int aCount, int /*long*/ _retval) {
105 int max = Math.min (aCount, buffer is null ? 0 : buffer.length - index);
106 if (max > 0) {
107 byte[] src = new byte[max];
108 System.arraycopy (buffer, index, src, 0, max);
109 XPCOM.memmove (aBuf, src, max);
110 index += max;
111 }
112 XPCOM.memmove(_retval, new int[] {max}, 4);
113 return XPCOM.NS_OK;
114 }
115
116 int ReadSegments (int /*long*/ aWriter, int /*long*/ aClosure, int aCount, int /*long*/ _retval) {
117 int max = Math.min (aCount, buffer is null ? 0 : buffer.length - index);
118 int cnt = max;
119 while (cnt > 0) {
120 int[] aWriteCount = new int[1];
121 int /*long*/ rc = XPCOM.Call (aWriter, getAddress (), aClosure, buffer, index, cnt, aWriteCount);
122 if (rc !is XPCOM.NS_OK) break;
123 index += aWriteCount[0];
124 cnt -= aWriteCount[0];
125 }
126 XPCOM.memmove (_retval, new int[] {max - cnt}, 4);
127 return XPCOM.NS_OK;
128 }
129
130 int IsNonBlocking (int /*long*/ _retval) {
131 /* blocking */
132 XPCOM.memmove (_retval, new int[] {0}, 4);
133 return XPCOM.NS_OK;
134 }
135 }