view dwt/browser/InputStream.d @ 278:93409d9838c5

Commit more browser/xpcom updates, including still uncoverted source.
author John Reimer<terminal.node@gmail.com>
date Thu, 31 Jul 2008 19:17:51 -0700
parents
children 44258e0b6687
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 dwt.dwthelper.utils;

import dwt.internal.mozilla.nsError;
import dwt.internal.mozilla.nsID;
import dwt.internal.mozilla.nsIInputStream;
import dwt.internal.mozilla.nsISupports;

import Math = tango.math.Math;

/******************************************************************************

    COMMENTS: SWT implements this class as a replacement for the XPCOM 
    implementation; it may be possible to use the XPCOM one instead 
    (which also may be safer for D), but for now we'll follow SWT's example 
    and use this reimplementation of InputStream. It should be trivial to
    move over to strict XPCOM implementation should the need arise. It appears 
    that the Java SWT source uses many workarounds in order to use the XPCOM 
    interface. For D, I remove much of the Java layered callback
    approach since it is no longer necessary.  -JJR

******************************************************************************/

class InputStream : nsIInputStream
{
    int     _refCount = 0;
    int     _index     = 0;
    byte[]  _buffer;

    /**************************************************************************

    **************************************************************************/

    this (byte[] buffer) 
    {
        this._buffer = buffer;
        index = 0;
    }

    /**************************************************************************

    **************************************************************************/

    nsrefcnt AddRef () 
    {
        _refCount++;
        return _refCount;
    }

    /**************************************************************************

    **************************************************************************/

    nsresult QueryInterface ( inout nsID riid, void** ppvObject) 
    {
        if (riid is null || ppvObject is null) 
            return NS_ERROR_NO_INTERFACE;
        
        nsID guid = riid;
    
        if (guid == nsISupports.NS_ISUPPORTS_IID) 
        {
            *ppvObject = cast(void*)cast(nsISupports) this;
            this.AddRef ();
            return NS_OK;
        }

        if (guid == nsIInputStream.NS_IINPUTSTREAM_IID) 
        {
            *ppvObject = cast(void*)cast(nsIInputStream) this;
            this.AddRef ();
            return NS_OK;
        }   
    
        *ppvObject = null;
        return NS_ERROR_NO_INTERFACE;
    }

    /**************************************************************************

    **************************************************************************/

    nsrefcnt Release () 
    {
        _refCount--;
        if (_refCount is 0) 
            return 0 
           // delete this;
        return _refCount;
    }
    
    /**************************************************************************

    **************************************************************************/

    nsresult Close () 
    {
        _buffer = null;
        _index = 0;
        return NS_OK;
    }

    /**************************************************************************

    **************************************************************************/

    nsresult Available ( PRUint32* retval) 
    {
        PRUint32 available = buffer is null ? 0 : buffer.length - _index;
        *retval = available;
        return NS_OK;
    }

    /**************************************************************************

    **************************************************************************/

    nsresult Read(byte* aBuf, PRUint32 aCount, PRUint32* retval) 
    {
        int max = Math.min (aCount, _buffer is null ? 0 : _buffer.length - _index);
        
        if (max > 0) 
        {
            aBuf[0..max] = _buffer[_index..$];
            _index += max;
        }

        *retval = max;
        return 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 NS_OK) break;
            _index += aWriteCount;
            cnt -= aWriteCount;
        }

        *retval = (max - cnt);
        return NS_OK;
    }

    /**************************************************************************

    **************************************************************************/

    nsresult IsNonBlocking ( PRUint32* retval) 
    {
        *retval = 0;
        return NS_OK;
    } 
}