Mozilla Cross-Reference mozilla-central
mozilla/ xpcom/ io/ nsIInputStream.idl
Hg Log
Hg Blame
Diff file
Raw file
view using tree:

Inheritance diagram for nsIInputStream:

Inheritance graph

Collaboration diagram for nsIInputStream:

Collaboration graph

[ nsIInputStream Interface Reference | Graph Legend ]

1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "nsISupports.idl"
7 
8 interface nsIInputStream;
9 
10 %{C++
11 /**
12  * The signature of the writer function passed to ReadSegments. This
13  * is the "consumer" of data that gets read from the stream's buffer.
14  *
15  * @param aInStream stream being read
16  * @param aClosure opaque parameter passed to ReadSegments
17  * @param aFromSegment pointer to memory owned by the input stream.  This is
18  *                     where the writer function should start consuming data.
19  * @param aToOffset amount of data already consumed by this writer during this
20  *                  ReadSegments call.  This is also the sum of the aWriteCount
21  *                  returns from this writer over the previous invocations of
22  *                  the writer by this ReadSegments call.
23  * @param aCount Number of bytes available to be read starting at aFromSegment
24  * @param [out] aWriteCount number of bytes read by this writer function call
25  *
26  * Implementers should return the following:
27  *
28  * @return NS_OK and (*aWriteCount > 0) if consumed some data
29  * @return <any-error> if not interested in consuming any data
30  *
31  * Errors are never passed to the caller of ReadSegments.
32  *
33  * NOTE: returning NS_OK and (*aWriteCount = 0) has undefined behavior.
34  */
35 typedef NS_CALLBACK(nsWriteSegmentFun)(nsIInputStream *aInStream,
36                                        void *aClosure,
37                                        const char *aFromSegment,
38                                        uint32_t aToOffset,
39                                        uint32_t aCount,
40                                        uint32_t *aWriteCount);
41 %}
42 
43 native nsWriteSegmentFun(nsWriteSegmentFun);
44 
45 /**
46  * nsIInputStream
47  *
48  * An interface describing a readable stream of data.  An input stream may be
49  * "blocking" or "non-blocking" (see the IsNonBlocking method).  A blocking
50  * input stream may suspend the calling thread in order to satisfy a call to
51  * Close, Available, Read, or ReadSegments.  A non-blocking input stream, on
52  * the other hand, must not block the calling thread of execution.
53  *
54  * NOTE: blocking input streams are often read on a background thread to avoid
55  * locking up the main application thread.  For this reason, it is generally
56  * the case that a blocking input stream should be implemented using thread-
57  * safe AddRef and Release.
58  */
59 [scriptable, uuid(53cdbc97-c2d7-4e30-b2c3-45b2ee79db18)]
60 interface nsIInputStream : nsISupports
61 {
62     /** 
63      * Close the stream.  This method causes subsequent calls to Read and
64      * ReadSegments to return 0 bytes read to indicate end-of-file.  Any
65      * subsequent calls to Available should throw NS_BASE_STREAM_CLOSED.
66      */
67     void close();
68 
69     /**
70      * Determine number of bytes available in the stream.  A non-blocking
71      * stream that does not yet have any data to read should return 0 bytes
72      * from this method (i.e., it must not throw the NS_BASE_STREAM_WOULD_BLOCK
73      * exception).
74      * 
75      * In addition to the number of bytes available in the stream, this method
76      * also informs the caller of the current status of the stream.  A stream
77      * that is closed will throw an exception when this method is called.  That
78      * enables the caller to know the condition of the stream before attempting
79      * to read from it.  If a stream is at end-of-file, but not closed, then
80      * this method returns 0 bytes available.  (Note: some nsIInputStream
81      * implementations automatically close when eof is reached; some do not).
82      *
83      * @return number of bytes currently available in the stream.
84      *
85      * @throws NS_BASE_STREAM_CLOSED if the stream is closed normally.
86      * @throws <other-error> if the stream is closed due to some error
87      *   condition
88      */
89     unsigned long long available();
90 
91     /** 
92      * Read data from the stream.
93      *
94      * @param aBuf the buffer into which the data is to be read
95      * @param aCount the maximum number of bytes to be read
96      *
97      * @return number of bytes read (may be less than aCount).
98      * @return 0 if reached end-of-file
99      *
100      * @throws NS_BASE_STREAM_WOULD_BLOCK if reading from the input stream would
101      *   block the calling thread (non-blocking mode only)
102      * @throws <other-error> on failure
103      *
104      * NOTE: this method should not throw NS_BASE_STREAM_CLOSED.
105      */
106     [noscript] unsigned long read(in charPtr aBuf, in unsigned long aCount);
107 
108     /**
109      * Low-level read method that provides access to the stream's underlying
110      * buffer.  The writer function may be called multiple times for segmented
111      * buffers.  ReadSegments is expected to keep calling the writer until
112      * either there is nothing left to read or the writer returns an error.
113      * ReadSegments should not call the writer with zero bytes to consume.
114      *
115      * @param aWriter the "consumer" of the data to be read
116      * @param aClosure opaque parameter passed to writer 
117      * @param aCount the maximum number of bytes to be read
118      *
119      * @return number of bytes read (may be less than aCount)
120      * @return 0 if reached end-of-file (or if aWriter refused to consume data)
121      *
122      * @throws NS_BASE_STREAM_WOULD_BLOCK if reading from the input stream would
123      *   block the calling thread (non-blocking mode only)
124      * @throws NS_ERROR_NOT_IMPLEMENTED if the stream has no underlying buffer
125      * @throws <other-error> on failure
126      *
127      * NOTE: this function may be unimplemented if a stream has no underlying
128      * buffer (e.g., socket input stream).
129      *
130      * NOTE: this method should not throw NS_BASE_STREAM_CLOSED.
131      */
132     [noscript] unsigned long readSegments(in nsWriteSegmentFun aWriter,
133                                           in voidPtr aClosure,
134                                           in unsigned long aCount);
135 
136     /**
137      * @return true if stream is non-blocking
138      *
139      * NOTE: reading from a blocking input stream will block the calling thread
140      * until at least one byte of data can be extracted from the stream.
141      *
142      * NOTE: a non-blocking input stream may implement nsIAsyncInputStream to
143      * provide consumers with a way to wait for the stream to have more data
144      * once its read method is unable to return any data without blocking.
145      */
146     boolean isNonBlocking();
147 };
148 
view http://hg.mozilla.org/mozilla-central/rev/ /xpcom/io/nsIInputStream.idl