KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > browser > InputStream


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 package org.eclipse.swt.browser;
12
13 import org.eclipse.swt.internal.C;
14 import org.eclipse.swt.internal.mozilla.*;
15
16 class InputStream {
17     XPCOMObject inputStream;
18     int refCount = 0;
19
20     byte[] buffer;
21     int index = 0;
22     
23 public InputStream (byte[] buffer) {
24     this.buffer = buffer;
25     index = 0;
26     createCOMInterfaces ();
27 }
28
29 int AddRef () {
30     refCount++;
31     return refCount;
32 }
33
34 void createCOMInterfaces () {
35     /* Create each of the interfaces that this object implements */
36     inputStream = new XPCOMObject (new int[] {2, 0, 0, 0, 1, 3, 4, 1}) {
37         public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
38         public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
39         public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
40         public int /*long*/ method3 (int /*long*/[] args) {return Close ();}
41         public int /*long*/ method4 (int /*long*/[] args) {return Available (args[0]);}
42         public int /*long*/ method5 (int /*long*/[] args) {return Read (args[0], args[1], args[2]);}
43         public int /*long*/ method6 (int /*long*/[] args) {return ReadSegments (args[0], args[1], args[2], args[3]);}
44         public int /*long*/ method7 (int /*long*/[] args) {return IsNonBlocking (args[0]);}
45     };
46 }
47
48 void disposeCOMInterfaces () {
49     if (inputStream != null) {
50         inputStream.dispose ();
51         inputStream = null;
52     }
53 }
54
55 int /*long*/ getAddress () {
56     return inputStream.getAddress ();
57 }
58
59 int /*long*/ QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) {
60     if (riid == 0 || ppvObject == 0) return XPCOM.NS_ERROR_NO_INTERFACE;
61     nsID guid = new nsID ();
62     XPCOM.memmove (guid, riid, nsID.sizeof);
63     
64     if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) {
65         XPCOM.memmove (ppvObject, new int /*long*/[] {inputStream.getAddress ()}, C.PTR_SIZEOF);
66         AddRef ();
67         return XPCOM.NS_OK;
68     }
69     if (guid.Equals (nsIInputStream.NS_IINPUTSTREAM_IID)) {
70         XPCOM.memmove (ppvObject, new int /*long*/[] {inputStream.getAddress ()}, C.PTR_SIZEOF);
71         AddRef ();
72         return XPCOM.NS_OK;
73     }
74     XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF);
75     return XPCOM.NS_ERROR_NO_INTERFACE;
76 }
77             
78 int Release () {
79     refCount--;
80     if (refCount == 0) disposeCOMInterfaces ();
81     return refCount;
82 }
83     
84 /* nsIInputStream implementation */
85
86 int /*long*/ Close () {
87     buffer = null;
88     index = 0;
89     return XPCOM.NS_OK;
90 }
91
92 int /*long*/ Available (int /*long*/ _retval) {
93     int available = buffer == null ? 0 : buffer.length - index;
94     XPCOM.memmove (_retval, new int[] {available}, 4);
95     return XPCOM.NS_OK;
96 }
97
98 int /*long*/ Read(int /*long*/ aBuf, int /*long*/ aCount, int /*long*/ _retval) {
99     int max = Math.min ((int)/*64*/aCount, buffer == null ? 0 : buffer.length - index);
100     if (max > 0) {
101         byte[] src = new byte[max];
102         System.arraycopy (buffer, index, src, 0, max);
103         XPCOM.memmove (aBuf, src, max);
104         index += max;
105     }
106     XPCOM.memmove(_retval, new int[] {max}, 4);
107     return XPCOM.NS_OK;
108 }
109
110 int /*long*/ ReadSegments (int /*long*/ aWriter, int /*long*/ aClosure, int /*long*/ aCount, int /*long*/ _retval) {
111     int max = Math.min ((int)/*64*/aCount, buffer == null ? 0 : buffer.length - index);
112     int cnt = max;
113     while (cnt > 0) {
114         int[] aWriteCount = new int[1];
115         int /*long*/ rc = XPCOM.Call (aWriter, getAddress (), aClosure, buffer, index, cnt, aWriteCount);
116         if (rc != XPCOM.NS_OK) break;
117         index += aWriteCount[0];
118         cnt -= aWriteCount[0];
119     }
120     XPCOM.memmove (_retval, new int[] {max - cnt}, 4);
121     return XPCOM.NS_OK;
122 }
123
124 int /*long*/ IsNonBlocking (int /*long*/ _retval) {
125     /* blocking */
126     XPCOM.memmove (_retval, new int[] {0}, 4);
127     return XPCOM.NS_OK;
128 }
129 }
130
Popular Tags