comparison dwt/browser/InputStream.d @ 125:5583f8eeee6c

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