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 module org.eclipse.swt.browser.InputStream;
14 
15 import java.lang.all;
16 
17 import XPCOM = org.eclipse.swt.internal.mozilla.XPCOM;
18 
19 import org.eclipse.swt.internal.mozilla.nsID;
20 import org.eclipse.swt.internal.mozilla.nsIInputStream;
21 import org.eclipse.swt.internal.mozilla.nsISupports;
22 import org.eclipse.swt.internal.mozilla.Common;
23 
24 class InputStream : nsIInputStream {
25     //XPCOMObject inputStream;
26     int refCount = 0;
27 
28     byte[] buffer;
29     int index = 0;
30     
31 this (byte[] buffer) {
32     this.buffer = buffer;
33     index = 0;
34 }
35 
36 extern(System)
37 nsrefcnt AddRef () {
38     refCount++;
39     return refCount;
40 }
41 
42 extern(System)
43 nsresult QueryInterface (in nsID* riid, void** ppvObject) {
44     if (riid is null || ppvObject is null) return XPCOM.NS_ERROR_NO_INTERFACE;
45     //nsID guid = new nsID ();
46     //XPCOM.memmove (guid, riid, nsID.sizeof);
47     
48     if (*riid == nsISupports.IID) {
49         *ppvObject = cast(void*)cast(nsISupports)this;
50         AddRef ();
51         return XPCOM.NS_OK;
52     }
53     if (*riid == nsIInputStream.IID) {
54         *ppvObject = cast(void*)cast(nsIInputStream)this;
55         AddRef ();
56         return XPCOM.NS_OK;
57     }   
58     *ppvObject = null;
59     return XPCOM.NS_ERROR_NO_INTERFACE;
60 }
61 
62 extern(System)
63 nsrefcnt Release () {
64     refCount--;
65     //if (refCount is 0) disposeCOMInterfaces ();
66     return refCount;
67 }
68 
69 /* nsIInputStream implementation */
70 
71 extern(System)
72 nsresult Close () {
73     buffer = null;
74     index = 0;
75     return XPCOM.NS_OK;
76 }
77 
78 extern(System)
79 nsresult Available (PRUint32* _retval) {
80     PRUint32 available = buffer is null ? 0 : buffer.length - index;
81     *_retval = available;
82     //XPCOM.memmove (_retval, new int[] {available}, 4);
83     return XPCOM.NS_OK;
84 }
85 
86 extern(System)
87 nsresult Read(byte* aBuf, PRUint32 aCount, PRUint32* _retval) {
88     int max = Math.min (aCount, buffer is null ? 0 : buffer.length - index);
89     if (aBuf is null)
90         assert(0);
91     if (max > 0) {
92         //byte[] src = new byte[max];
93         //System.arraycopy (buffer, index, src, 0, max);
94         //XPCOM.memmove (aBuf, src, max);
95         aBuf[0..max] = buffer[index..$];
96         index += max;
97     }
98     *_retval = max;
99     return XPCOM.NS_OK;
100 }
101 
102 extern(System)
103 nsresult ReadSegments (nsWriteSegmentFun aWriter, void* aClosure, PRUint32 aCount, PRUint32* _retval) {
104     int max = Math.min (aCount, buffer is null ? 0 : buffer.length - index);
105     PRUint32 cnt = max;
106     while (cnt > 0) {
107         PRUint32 aWriteCount;
108         nsresult rc = aWriter (cast(nsIInputStream)this, aClosure, buffer.ptr, index, cnt, &aWriteCount);
109         if (rc !is XPCOM.NS_OK) break;
110         index += aWriteCount;
111         cnt -= aWriteCount;
112     }
113     //XPCOM.memmove (_retval, new int[] {max - cnt}, 4);
114     *_retval = (max - cnt);
115     return XPCOM.NS_OK;
116 }
117 
118 extern(System)
119 nsresult IsNonBlocking (PRBool* _retval) {
120     /* blocking */
121     *_retval = 0;
122     return XPCOM.NS_OK;
123 }       
124 }