xpcom/io/nsIInputStream.idl

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/io/nsIInputStream.idl	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,147 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsISupports.idl"
    1.10 +
    1.11 +interface nsIInputStream;
    1.12 +
    1.13 +%{C++
    1.14 +/**
    1.15 + * The signature of the writer function passed to ReadSegments. This
    1.16 + * is the "consumer" of data that gets read from the stream's buffer.
    1.17 + *
    1.18 + * @param aInStream stream being read
    1.19 + * @param aClosure opaque parameter passed to ReadSegments
    1.20 + * @param aFromSegment pointer to memory owned by the input stream.  This is
    1.21 + *                     where the writer function should start consuming data.
    1.22 + * @param aToOffset amount of data already consumed by this writer during this
    1.23 + *                  ReadSegments call.  This is also the sum of the aWriteCount
    1.24 + *                  returns from this writer over the previous invocations of
    1.25 + *                  the writer by this ReadSegments call.
    1.26 + * @param aCount Number of bytes available to be read starting at aFromSegment
    1.27 + * @param [out] aWriteCount number of bytes read by this writer function call
    1.28 + *
    1.29 + * Implementers should return the following:
    1.30 + *
    1.31 + * @return NS_OK and (*aWriteCount > 0) if consumed some data
    1.32 + * @return <any-error> if not interested in consuming any data
    1.33 + *
    1.34 + * Errors are never passed to the caller of ReadSegments.
    1.35 + *
    1.36 + * NOTE: returning NS_OK and (*aWriteCount = 0) has undefined behavior.
    1.37 + */
    1.38 +typedef NS_CALLBACK(nsWriteSegmentFun)(nsIInputStream *aInStream,
    1.39 +                                       void *aClosure,
    1.40 +                                       const char *aFromSegment,
    1.41 +                                       uint32_t aToOffset,
    1.42 +                                       uint32_t aCount,
    1.43 +                                       uint32_t *aWriteCount);
    1.44 +%}
    1.45 +
    1.46 +native nsWriteSegmentFun(nsWriteSegmentFun);
    1.47 +
    1.48 +/**
    1.49 + * nsIInputStream
    1.50 + *
    1.51 + * An interface describing a readable stream of data.  An input stream may be
    1.52 + * "blocking" or "non-blocking" (see the IsNonBlocking method).  A blocking
    1.53 + * input stream may suspend the calling thread in order to satisfy a call to
    1.54 + * Close, Available, Read, or ReadSegments.  A non-blocking input stream, on
    1.55 + * the other hand, must not block the calling thread of execution.
    1.56 + *
    1.57 + * NOTE: blocking input streams are often read on a background thread to avoid
    1.58 + * locking up the main application thread.  For this reason, it is generally
    1.59 + * the case that a blocking input stream should be implemented using thread-
    1.60 + * safe AddRef and Release.
    1.61 + */
    1.62 +[scriptable, uuid(53cdbc97-c2d7-4e30-b2c3-45b2ee79db18)]
    1.63 +interface nsIInputStream : nsISupports
    1.64 +{
    1.65 +    /** 
    1.66 +     * Close the stream.  This method causes subsequent calls to Read and
    1.67 +     * ReadSegments to return 0 bytes read to indicate end-of-file.  Any
    1.68 +     * subsequent calls to Available should throw NS_BASE_STREAM_CLOSED.
    1.69 +     */
    1.70 +    void close();
    1.71 +
    1.72 +    /**
    1.73 +     * Determine number of bytes available in the stream.  A non-blocking
    1.74 +     * stream that does not yet have any data to read should return 0 bytes
    1.75 +     * from this method (i.e., it must not throw the NS_BASE_STREAM_WOULD_BLOCK
    1.76 +     * exception).
    1.77 +     * 
    1.78 +     * In addition to the number of bytes available in the stream, this method
    1.79 +     * also informs the caller of the current status of the stream.  A stream
    1.80 +     * that is closed will throw an exception when this method is called.  That
    1.81 +     * enables the caller to know the condition of the stream before attempting
    1.82 +     * to read from it.  If a stream is at end-of-file, but not closed, then
    1.83 +     * this method returns 0 bytes available.  (Note: some nsIInputStream
    1.84 +     * implementations automatically close when eof is reached; some do not).
    1.85 +     *
    1.86 +     * @return number of bytes currently available in the stream.
    1.87 +     *
    1.88 +     * @throws NS_BASE_STREAM_CLOSED if the stream is closed normally.
    1.89 +     * @throws <other-error> if the stream is closed due to some error
    1.90 +     *   condition
    1.91 +     */
    1.92 +    unsigned long long available();
    1.93 +
    1.94 +    /** 
    1.95 +     * Read data from the stream.
    1.96 +     *
    1.97 +     * @param aBuf the buffer into which the data is to be read
    1.98 +     * @param aCount the maximum number of bytes to be read
    1.99 +     *
   1.100 +     * @return number of bytes read (may be less than aCount).
   1.101 +     * @return 0 if reached end-of-file
   1.102 +     *
   1.103 +     * @throws NS_BASE_STREAM_WOULD_BLOCK if reading from the input stream would
   1.104 +     *   block the calling thread (non-blocking mode only)
   1.105 +     * @throws <other-error> on failure
   1.106 +     *
   1.107 +     * NOTE: this method should not throw NS_BASE_STREAM_CLOSED.
   1.108 +     */
   1.109 +    [noscript] unsigned long read(in charPtr aBuf, in unsigned long aCount);
   1.110 +
   1.111 +    /**
   1.112 +     * Low-level read method that provides access to the stream's underlying
   1.113 +     * buffer.  The writer function may be called multiple times for segmented
   1.114 +     * buffers.  ReadSegments is expected to keep calling the writer until
   1.115 +     * either there is nothing left to read or the writer returns an error.
   1.116 +     * ReadSegments should not call the writer with zero bytes to consume.
   1.117 +     *
   1.118 +     * @param aWriter the "consumer" of the data to be read
   1.119 +     * @param aClosure opaque parameter passed to writer 
   1.120 +     * @param aCount the maximum number of bytes to be read
   1.121 +     *
   1.122 +     * @return number of bytes read (may be less than aCount)
   1.123 +     * @return 0 if reached end-of-file (or if aWriter refused to consume data)
   1.124 +     *
   1.125 +     * @throws NS_BASE_STREAM_WOULD_BLOCK if reading from the input stream would
   1.126 +     *   block the calling thread (non-blocking mode only)
   1.127 +     * @throws NS_ERROR_NOT_IMPLEMENTED if the stream has no underlying buffer
   1.128 +     * @throws <other-error> on failure
   1.129 +     *
   1.130 +     * NOTE: this function may be unimplemented if a stream has no underlying
   1.131 +     * buffer (e.g., socket input stream).
   1.132 +     *
   1.133 +     * NOTE: this method should not throw NS_BASE_STREAM_CLOSED.
   1.134 +     */
   1.135 +    [noscript] unsigned long readSegments(in nsWriteSegmentFun aWriter,
   1.136 +                                          in voidPtr aClosure,
   1.137 +                                          in unsigned long aCount);
   1.138 +
   1.139 +    /**
   1.140 +     * @return true if stream is non-blocking
   1.141 +     *
   1.142 +     * NOTE: reading from a blocking input stream will block the calling thread
   1.143 +     * until at least one byte of data can be extracted from the stream.
   1.144 +     *
   1.145 +     * NOTE: a non-blocking input stream may implement nsIAsyncInputStream to
   1.146 +     * provide consumers with a way to wait for the stream to have more data
   1.147 +     * once its read method is unable to return any data without blocking.
   1.148 +     */
   1.149 +    boolean isNonBlocking();
   1.150 +};

mercurial