michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsISupports.idl" michael@0: michael@0: interface nsIInputStream; michael@0: michael@0: %{C++ michael@0: /** michael@0: * The signature of the writer function passed to ReadSegments. This michael@0: * is the "consumer" of data that gets read from the stream's buffer. michael@0: * michael@0: * @param aInStream stream being read michael@0: * @param aClosure opaque parameter passed to ReadSegments michael@0: * @param aFromSegment pointer to memory owned by the input stream. This is michael@0: * where the writer function should start consuming data. michael@0: * @param aToOffset amount of data already consumed by this writer during this michael@0: * ReadSegments call. This is also the sum of the aWriteCount michael@0: * returns from this writer over the previous invocations of michael@0: * the writer by this ReadSegments call. michael@0: * @param aCount Number of bytes available to be read starting at aFromSegment michael@0: * @param [out] aWriteCount number of bytes read by this writer function call michael@0: * michael@0: * Implementers should return the following: michael@0: * michael@0: * @return NS_OK and (*aWriteCount > 0) if consumed some data michael@0: * @return if not interested in consuming any data michael@0: * michael@0: * Errors are never passed to the caller of ReadSegments. michael@0: * michael@0: * NOTE: returning NS_OK and (*aWriteCount = 0) has undefined behavior. michael@0: */ michael@0: typedef NS_CALLBACK(nsWriteSegmentFun)(nsIInputStream *aInStream, michael@0: void *aClosure, michael@0: const char *aFromSegment, michael@0: uint32_t aToOffset, michael@0: uint32_t aCount, michael@0: uint32_t *aWriteCount); michael@0: %} michael@0: michael@0: native nsWriteSegmentFun(nsWriteSegmentFun); michael@0: michael@0: /** michael@0: * nsIInputStream michael@0: * michael@0: * An interface describing a readable stream of data. An input stream may be michael@0: * "blocking" or "non-blocking" (see the IsNonBlocking method). A blocking michael@0: * input stream may suspend the calling thread in order to satisfy a call to michael@0: * Close, Available, Read, or ReadSegments. A non-blocking input stream, on michael@0: * the other hand, must not block the calling thread of execution. michael@0: * michael@0: * NOTE: blocking input streams are often read on a background thread to avoid michael@0: * locking up the main application thread. For this reason, it is generally michael@0: * the case that a blocking input stream should be implemented using thread- michael@0: * safe AddRef and Release. michael@0: */ michael@0: [scriptable, uuid(53cdbc97-c2d7-4e30-b2c3-45b2ee79db18)] michael@0: interface nsIInputStream : nsISupports michael@0: { michael@0: /** michael@0: * Close the stream. This method causes subsequent calls to Read and michael@0: * ReadSegments to return 0 bytes read to indicate end-of-file. Any michael@0: * subsequent calls to Available should throw NS_BASE_STREAM_CLOSED. michael@0: */ michael@0: void close(); michael@0: michael@0: /** michael@0: * Determine number of bytes available in the stream. A non-blocking michael@0: * stream that does not yet have any data to read should return 0 bytes michael@0: * from this method (i.e., it must not throw the NS_BASE_STREAM_WOULD_BLOCK michael@0: * exception). michael@0: * michael@0: * In addition to the number of bytes available in the stream, this method michael@0: * also informs the caller of the current status of the stream. A stream michael@0: * that is closed will throw an exception when this method is called. That michael@0: * enables the caller to know the condition of the stream before attempting michael@0: * to read from it. If a stream is at end-of-file, but not closed, then michael@0: * this method returns 0 bytes available. (Note: some nsIInputStream michael@0: * implementations automatically close when eof is reached; some do not). michael@0: * michael@0: * @return number of bytes currently available in the stream. michael@0: * michael@0: * @throws NS_BASE_STREAM_CLOSED if the stream is closed normally. michael@0: * @throws if the stream is closed due to some error michael@0: * condition michael@0: */ michael@0: unsigned long long available(); michael@0: michael@0: /** michael@0: * Read data from the stream. michael@0: * michael@0: * @param aBuf the buffer into which the data is to be read michael@0: * @param aCount the maximum number of bytes to be read michael@0: * michael@0: * @return number of bytes read (may be less than aCount). michael@0: * @return 0 if reached end-of-file michael@0: * michael@0: * @throws NS_BASE_STREAM_WOULD_BLOCK if reading from the input stream would michael@0: * block the calling thread (non-blocking mode only) michael@0: * @throws on failure michael@0: * michael@0: * NOTE: this method should not throw NS_BASE_STREAM_CLOSED. michael@0: */ michael@0: [noscript] unsigned long read(in charPtr aBuf, in unsigned long aCount); michael@0: michael@0: /** michael@0: * Low-level read method that provides access to the stream's underlying michael@0: * buffer. The writer function may be called multiple times for segmented michael@0: * buffers. ReadSegments is expected to keep calling the writer until michael@0: * either there is nothing left to read or the writer returns an error. michael@0: * ReadSegments should not call the writer with zero bytes to consume. michael@0: * michael@0: * @param aWriter the "consumer" of the data to be read michael@0: * @param aClosure opaque parameter passed to writer michael@0: * @param aCount the maximum number of bytes to be read michael@0: * michael@0: * @return number of bytes read (may be less than aCount) michael@0: * @return 0 if reached end-of-file (or if aWriter refused to consume data) michael@0: * michael@0: * @throws NS_BASE_STREAM_WOULD_BLOCK if reading from the input stream would michael@0: * block the calling thread (non-blocking mode only) michael@0: * @throws NS_ERROR_NOT_IMPLEMENTED if the stream has no underlying buffer michael@0: * @throws on failure michael@0: * michael@0: * NOTE: this function may be unimplemented if a stream has no underlying michael@0: * buffer (e.g., socket input stream). michael@0: * michael@0: * NOTE: this method should not throw NS_BASE_STREAM_CLOSED. michael@0: */ michael@0: [noscript] unsigned long readSegments(in nsWriteSegmentFun aWriter, michael@0: in voidPtr aClosure, michael@0: in unsigned long aCount); michael@0: michael@0: /** michael@0: * @return true if stream is non-blocking michael@0: * michael@0: * NOTE: reading from a blocking input stream will block the calling thread michael@0: * until at least one byte of data can be extracted from the stream. michael@0: * michael@0: * NOTE: a non-blocking input stream may implement nsIAsyncInputStream to michael@0: * provide consumers with a way to wait for the stream to have more data michael@0: * once its read method is unable to return any data without blocking. michael@0: */ michael@0: boolean isNonBlocking(); michael@0: };