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 nsIOutputStream; michael@0: interface nsIInputStream; michael@0: michael@0: %{C++ michael@0: /** michael@0: * The signature for the reader function passed to WriteSegments. This michael@0: * is the "provider" of data that gets written into the stream's buffer. michael@0: * michael@0: * @param aOutStream stream being written to michael@0: * @param aClosure opaque parameter passed to WriteSegments michael@0: * @param aToSegment pointer to memory owned by the output stream michael@0: * @param aFromOffset amount already written (since WriteSegments was called) michael@0: * @param aCount length of toSegment michael@0: * @param aReadCount number of bytes written michael@0: * michael@0: * Implementers should return the following: michael@0: * michael@0: * @throws if not interested in providing any data michael@0: * michael@0: * Errors are never passed to the caller of WriteSegments. michael@0: */ michael@0: typedef NS_CALLBACK(nsReadSegmentFun)(nsIOutputStream *aOutStream, michael@0: void *aClosure, michael@0: char *aToSegment, michael@0: uint32_t aFromOffset, michael@0: uint32_t aCount, michael@0: uint32_t *aReadCount); michael@0: %} michael@0: michael@0: native nsReadSegmentFun(nsReadSegmentFun); michael@0: michael@0: /** michael@0: * nsIOutputStream michael@0: * michael@0: * An interface describing a writable stream of data. An output stream may be michael@0: * "blocking" or "non-blocking" (see the IsNonBlocking method). A blocking michael@0: * output stream may suspend the calling thread in order to satisfy a call to michael@0: * Close, Flush, Write, WriteFrom, or WriteSegments. A non-blocking output michael@0: * stream, on the other hand, must not block the calling thread of execution. michael@0: * michael@0: * NOTE: blocking output streams are often written to on a background thread to michael@0: * avoid locking up the main application thread. For this reason, it is michael@0: * generally the case that a blocking output stream should be implemented using michael@0: * thread- safe AddRef and Release. michael@0: */ michael@0: [scriptable, uuid(0d0acd2a-61b4-11d4-9877-00c04fa0cf4a)] michael@0: interface nsIOutputStream : nsISupports michael@0: { michael@0: /** michael@0: * Close the stream. Forces the output stream to flush any buffered data. michael@0: * michael@0: * @throws NS_BASE_STREAM_WOULD_BLOCK if unable to flush without blocking michael@0: * the calling thread (non-blocking mode only) michael@0: */ michael@0: void close(); michael@0: michael@0: /** michael@0: * Flush the stream. michael@0: * michael@0: * @throws NS_BASE_STREAM_WOULD_BLOCK if unable to flush without blocking michael@0: * the calling thread (non-blocking mode only) michael@0: */ michael@0: void flush(); michael@0: michael@0: /** michael@0: * Write data into the stream. michael@0: * michael@0: * @param aBuf the buffer containing the data to be written michael@0: * @param aCount the maximum number of bytes to be written michael@0: * michael@0: * @return number of bytes written (may be less than aCount) michael@0: * michael@0: * @throws NS_BASE_STREAM_WOULD_BLOCK if writing to the output stream would michael@0: * block the calling thread (non-blocking mode only) michael@0: * @throws on failure michael@0: */ michael@0: unsigned long write(in string aBuf, in unsigned long aCount); michael@0: michael@0: /** michael@0: * Writes data into the stream from an input stream. michael@0: * michael@0: * @param aFromStream the stream containing the data to be written michael@0: * @param aCount the maximum number of bytes to be written michael@0: * michael@0: * @return number of bytes written (may be less than aCount) michael@0: * michael@0: * @throws NS_BASE_STREAM_WOULD_BLOCK if writing to the output stream would michael@0: * block the calling thread (non-blocking mode only) michael@0: * @throws on failure michael@0: * michael@0: * NOTE: This method is defined by this interface in order to allow the michael@0: * output stream to efficiently copy the data from the input stream into michael@0: * its internal buffer (if any). If this method was provided as an external michael@0: * facility, a separate char* buffer would need to be used in order to call michael@0: * the output stream's other Write method. michael@0: */ michael@0: unsigned long writeFrom(in nsIInputStream aFromStream, michael@0: in unsigned long aCount); michael@0: michael@0: /** michael@0: * Low-level write method that has access to the stream's underlying buffer. michael@0: * The reader function may be called multiple times for segmented buffers. michael@0: * WriteSegments is expected to keep calling the reader until either there michael@0: * is nothing left to write or the reader returns an error. WriteSegments michael@0: * should not call the reader with zero bytes to provide. michael@0: * michael@0: * @param aReader the "provider" of the data to be written michael@0: * @param aClosure opaque parameter passed to reader michael@0: * @param aCount the maximum number of bytes to be written michael@0: * michael@0: * @return number of bytes written (may be less than aCount) michael@0: * michael@0: * @throws NS_BASE_STREAM_WOULD_BLOCK if writing to the output 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 output stream). michael@0: */ michael@0: [noscript] unsigned long writeSegments(in nsReadSegmentFun aReader, 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: writing to a blocking output stream will block the calling thread michael@0: * until all given data can be consumed by the stream. michael@0: * michael@0: * NOTE: a non-blocking output stream may implement nsIAsyncOutputStream to michael@0: * provide consumers with a way to wait for the stream to accept more data michael@0: * once its write method is unable to accept any data without blocking. michael@0: */ michael@0: boolean isNonBlocking(); michael@0: };