view dwt/browser/InputStream.d @ 345:5abc6f7f7a95

Fixups
author John Reimer <terminal.node@gmail.com>
date Tue, 28 Oct 2008 22:07:01 -0700
parents 96243e3ebcf0
children 2e591eb01162
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2003, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 * Port to the D programming language:
 *      John Reimer <terminal.node@gmail.com>
 *******************************************************************************/
module dwt.browser.InputStream;

import Math = tango.math.Math;
import dwt.dwthelper.utils;

//import dwt.internal.C;
import XPCOM = dwt.internal.mozilla.XPCOM;
//import dwt.internal.mozilla.XPCOMObject;
import dwt.internal.mozilla.nsID;
import dwt.internal.mozilla.nsIInputStream;
import dwt.internal.mozilla.nsISupports;
import dwt.internal.mozilla.Common;

class InputStream : nsIInputStream {
    //XPCOMObject inputStream;
    int refCount = 0;

    byte[] buffer;
    int index = 0;
    
this (byte[] buffer) {
    this.buffer = buffer;
    index = 0;
    //createCOMInterfaces ();
}

nsrefcnt AddRef () {
    refCount++;
    return refCount;
}
/+
void createCOMInterfaces () {
    /* Create each of the interfaces that this object implements */
    inputStream = new XPCOMObject (new int[] {2, 0, 0, 0, 1, 3, 4, 1}) {
        public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
        public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
        public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
        public int /*long*/ method3 (int /*long*/[] args) {return Close ();}
        public int /*long*/ method4 (int /*long*/[] args) {return Available (args[0]);}
        public int /*long*/ method5 (int /*long*/[] args) {return Read (args[0], (int)/*64*/args[1], args[2]);}
        public int /*long*/ method6 (int /*long*/[] args) {return ReadSegments (args[0], args[1], (int)/*64*/args[2], args[3]);}
        public int /*long*/ method7 (int /*long*/[] args) {return IsNonBlocking (args[0]);}
    };
}

void disposeCOMInterfaces () {
    if (inputStream !is null) {
        inputStream.dispose ();
        inputStream = null; 
    }
}

int /*long*/ getAddress () {
    return inputStream.getAddress ();
}
+/
nsresult QueryInterface (nsID* riid, void** ppvObject) {
    if (riid is null || ppvObject is null) return XPCOM.NS_ERROR_NO_INTERFACE;
    //nsID guid = new nsID ();
    //XPCOM.memmove (guid, riid, nsID.sizeof);
    
    if (*riid == nsISupports.IID) {
        *ppvObject = cast(void*)cast(nsISupports)this;
        AddRef ();
        return XPCOM.NS_OK;
    }
    if (*riid == nsIInputStream.IID) {
        *ppvObject = cast(void*)cast(nsIInputStream)this;
        AddRef ();
        return XPCOM.NS_OK;
    }   
    *ppvObject = null;
    return XPCOM.NS_ERROR_NO_INTERFACE;
}
            
nsrefcnt Release () {
    refCount--;
    //if (refCount is 0) disposeCOMInterfaces ();
    return refCount;
}
    
/* nsIInputStream implementation */

nsresult Close () {
    buffer = null;
    index = 0;
    return XPCOM.NS_OK;
}

nsresult Available (PRUint32* _retval) {
    PRUint32 available = buffer is null ? 0 : buffer.length - index;
    *_retval = available;
    //XPCOM.memmove (_retval, new int[] {available}, 4);
    return XPCOM.NS_OK;
}

nsresult Read(byte* aBuf, PRUint32 aCount, PRUint32* _retval) {
    int max = Math.min (aCount, buffer is null ? 0 : buffer.length - index);
    if (aBuf is null)
        assert(0);
    if (max > 0) {
        //byte[] src = new byte[max];
        //System.arraycopy (buffer, index, src, 0, max);
        //XPCOM.memmove (aBuf, src, max);
        aBuf[0..max] = buffer[index..$];
        index += max;
    }
    *_retval = max;
    return XPCOM.NS_OK;
}

nsresult ReadSegments (nsWriteSegmentFun aWriter, void* aClosure, PRUint32 aCount, PRUint32* _retval) {
    int max = Math.min (aCount, buffer is null ? 0 : buffer.length - index);
    PRUint32 cnt = max;
    while (cnt > 0) {
        PRUint32 aWriteCount;
        nsresult rc = aWriter (cast(nsIInputStream)this, aClosure, buffer.ptr, index, cnt, &aWriteCount);
        if (rc !is XPCOM.NS_OK) break;
        index += aWriteCount;
        cnt -= aWriteCount;
    }
    //XPCOM.memmove (_retval, new int[] {max - cnt}, 4);
    *_retval = (max - cnt);
    return XPCOM.NS_OK;
}

nsresult IsNonBlocking (PRUint32* _retval) {
    /* blocking */
    *_retval = 0;
    return XPCOM.NS_OK;
}       
}