1 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 36 inputStream = new XPCOMObject (new int[] {2, 0, 0, 0, 1, 3, 4, 1}) { 37 public int method0 (int [] args) {return QueryInterface (args[0], args[1]);} 38 public int method1 (int [] args) {return AddRef ();} 39 public int method2 (int [] args) {return Release ();} 40 public int method3 (int [] args) {return Close ();} 41 public int method4 (int [] args) {return Available (args[0]);} 42 public int method5 (int [] args) {return Read (args[0], args[1], args[2]);} 43 public int method6 (int [] args) {return ReadSegments (args[0], args[1], args[2], args[3]);} 44 public int method7 (int [] 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 getAddress () { 56 return inputStream.getAddress (); 57 } 58 59 int QueryInterface (int riid, int 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 [] {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 [] {inputStream.getAddress ()}, C.PTR_SIZEOF); 71 AddRef (); 72 return XPCOM.NS_OK; 73 } 74 XPCOM.memmove (ppvObject, new int [] {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 85 86 int Close () { 87 buffer = null; 88 index = 0; 89 return XPCOM.NS_OK; 90 } 91 92 int Available (int _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 Read(int aBuf, int aCount, int _retval) { 99 int max = Math.min ((int)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 ReadSegments (int aWriter, int aClosure, int aCount, int _retval) { 111 int max = Math.min ((int)aCount, buffer == null ? 0 : buffer.length - index); 112 int cnt = max; 113 while (cnt > 0) { 114 int[] aWriteCount = new int[1]; 115 int 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 IsNonBlocking (int _retval) { 125 126 XPCOM.memmove (_retval, new int[] {0}, 4); 127 return XPCOM.NS_OK; 128 } 129 } 130 | Popular Tags |