comparison dwt/browser/InputStream.d @ 278:93409d9838c5

Commit more browser/xpcom updates, including still uncoverted source.
author John Reimer<terminal.node@gmail.com>
date Thu, 31 Jul 2008 19:17:51 -0700
parents
children 44258e0b6687
comparison
equal deleted inserted replaced
277:687f261028b8 278:93409d9838c5
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 * Port to the D programming language:
11 * John Reimer <terminal.node@gmail.com>
12 *******************************************************************************/
13
14 module dwt.browser.InputStream;
15
16 import dwt.dwthelper.utils;
17
18 import dwt.internal.mozilla.nsError;
19 import dwt.internal.mozilla.nsID;
20 import dwt.internal.mozilla.nsIInputStream;
21 import dwt.internal.mozilla.nsISupports;
22
23 import Math = tango.math.Math;
24
25 /******************************************************************************
26
27 COMMENTS: SWT implements this class as a replacement for the XPCOM
28 implementation; it may be possible to use the XPCOM one instead
29 (which also may be safer for D), but for now we'll follow SWT's example
30 and use this reimplementation of InputStream. It should be trivial to
31 move over to strict XPCOM implementation should the need arise. It appears
32 that the Java SWT source uses many workarounds in order to use the XPCOM
33 interface. For D, I remove much of the Java layered callback
34 approach since it is no longer necessary. -JJR
35
36 ******************************************************************************/
37
38 class InputStream : nsIInputStream
39 {
40 int _refCount = 0;
41 int _index = 0;
42 byte[] _buffer;
43
44 /**************************************************************************
45
46 **************************************************************************/
47
48 this (byte[] buffer)
49 {
50 this._buffer = buffer;
51 index = 0;
52 }
53
54 /**************************************************************************
55
56 **************************************************************************/
57
58 nsrefcnt AddRef ()
59 {
60 _refCount++;
61 return _refCount;
62 }
63
64 /**************************************************************************
65
66 **************************************************************************/
67
68 nsresult QueryInterface ( inout nsID riid, void** ppvObject)
69 {
70 if (riid is null || ppvObject is null)
71 return NS_ERROR_NO_INTERFACE;
72
73 nsID guid = riid;
74
75 if (guid == nsISupports.NS_ISUPPORTS_IID)
76 {
77 *ppvObject = cast(void*)cast(nsISupports) this;
78 this.AddRef ();
79 return NS_OK;
80 }
81
82 if (guid == nsIInputStream.NS_IINPUTSTREAM_IID)
83 {
84 *ppvObject = cast(void*)cast(nsIInputStream) this;
85 this.AddRef ();
86 return NS_OK;
87 }
88
89 *ppvObject = null;
90 return NS_ERROR_NO_INTERFACE;
91 }
92
93 /**************************************************************************
94
95 **************************************************************************/
96
97 nsrefcnt Release ()
98 {
99 _refCount--;
100 if (_refCount is 0)
101 return 0
102 // delete this;
103 return _refCount;
104 }
105
106 /**************************************************************************
107
108 **************************************************************************/
109
110 nsresult Close ()
111 {
112 _buffer = null;
113 _index = 0;
114 return NS_OK;
115 }
116
117 /**************************************************************************
118
119 **************************************************************************/
120
121 nsresult Available ( PRUint32* retval)
122 {
123 PRUint32 available = buffer is null ? 0 : buffer.length - _index;
124 *retval = available;
125 return NS_OK;
126 }
127
128 /**************************************************************************
129
130 **************************************************************************/
131
132 nsresult Read(byte* aBuf, PRUint32 aCount, PRUint32* retval)
133 {
134 int max = Math.min (aCount, _buffer is null ? 0 : _buffer.length - _index);
135
136 if (max > 0)
137 {
138 aBuf[0..max] = _buffer[_index..$];
139 _index += max;
140 }
141
142 *retval = max;
143 return NS_OK;
144 }
145
146 /**************************************************************************
147
148 **************************************************************************/
149
150 nsresult ReadSegments (nsWriteSegmentFun aWriter, void* aClosure, PRUint32 aCount, PRUint32* retval)
151 {
152 int max = Math.min (aCount, buffer is null ? 0 : buffer.length - _index);
153 PRUint32 cnt = max;
154
155 while (cnt > 0)
156 {
157 PRUint32 aWriteCount;
158 nsresult rc = aWriter ( cast(nsIInputStream)this, aClosure, buffer.ptr, _index, cnt, &aWriteCount);
159 if (rc !is NS_OK) break;
160 _index += aWriteCount;
161 cnt -= aWriteCount;
162 }
163
164 *retval = (max - cnt);
165 return NS_OK;
166 }
167
168 /**************************************************************************
169
170 **************************************************************************/
171
172 nsresult IsNonBlocking ( PRUint32* retval)
173 {
174 *retval = 0;
175 return NS_OK;
176 }
177 }