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/. */
6 #include
"nsISupports.idl"
8 interface nsIInputStream
;
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.
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
26 * Implementers should return the following:
28 * @return NS_OK and (*aWriteCount > 0) if consumed some data
29 * @return <any-error> if not interested in consuming any data
31 * Errors are never passed to the caller of ReadSegments.
33 * NOTE: returning NS_OK and (*aWriteCount = 0) has undefined behavior.
35 typedef nsresult
(*nsWriteSegmentFun
)(nsIInputStream
*aInStream
,
37 const char *aFromSegment
,
40 uint32_t
*aWriteCount
);
43 native nsWriteSegmentFun
(nsWriteSegmentFun
);
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.
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.
59 [scriptable
, builtinclass
, uuid(53cdbc97
-c2d7
-4e30
-b2c3
-45b2ee79db18
)]
60 interface nsIInputStream
: nsISupports
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 or StreamStatus should throw
66 * NS_BASE_STREAM_CLOSED.
68 * Succeeds (without side effects) if already closed.
73 * Determine number of bytes available in the stream. A non-blocking
74 * stream that does not yet have any data to read should return 0 bytes
75 * from this method (i.e., it must not throw the NS_BASE_STREAM_WOULD_BLOCK
78 * In addition to the number of bytes available in the stream, this method
79 * also informs the caller of the current status of the stream. A stream
80 * that is closed will throw an exception when this method is called. That
81 * enables the caller to know the condition of the stream before attempting
82 * to read from it. If a stream is at end-of-file, but not closed, then
83 * this method returns 0 bytes available. (Note: some nsIInputStream
84 * implementations automatically close when eof is reached; some do not).
86 * NOTE: Streams implementing nsIAsyncInputStream must automatically close
87 * when eof is reached, as otherwise it is impossible to distinguish between
88 * a stream waiting for more data and a stream at EOF using Available().
90 * @return number of bytes currently available in the stream.
92 * @throws NS_BASE_STREAM_CLOSED if the stream is closed normally.
93 * @throws <other-error> if the stream is closed due to some error
96 unsigned long long available
();
99 * Check the current status of the stream. A stream that is closed will
100 * throw an exception when this method is called. That enables the caller
101 * to know the condition of the stream before attempting to read from it.
103 * This method will not throw NS_BASE_STREAM_WOULD_BLOCK, even if the stream
104 * is an non-blocking stream with no data. A non-blocking stream that does
105 * not yet have any data to read should return NS_OK.
107 * NOTE: Unlike available, his method should not block the calling thread
108 * (e.g. to query the state of a file descriptor), even when called on a
111 * @throws NS_BASE_STREAM_CLOSED if the stream closed normally
112 * @throws <other-error> if the stream closed with a different status
117 * Read data from the stream.
119 * @param aBuf the buffer into which the data is to be read
120 * @param aCount the maximum number of bytes to be read
122 * @return number of bytes read (may be less than aCount).
123 * @return 0 if reached end-of-file
125 * @throws NS_BASE_STREAM_WOULD_BLOCK if reading from the input stream would
126 * block the calling thread (non-blocking mode only)
127 * @throws <other-error> on failure
129 * NOTE: this method should not throw NS_BASE_STREAM_CLOSED.
131 [noscript
] unsigned long read
(in charPtr aBuf
, in unsigned long aCount
);
134 * Low-level read method that provides access to the stream's underlying
135 * buffer. The writer function may be called multiple times for segmented
136 * buffers. ReadSegments is expected to keep calling the writer until
137 * either there is nothing left to read or the writer returns an error.
138 * ReadSegments should not call the writer with zero bytes to consume.
140 * @param aWriter the "consumer" of the data to be read
141 * @param aClosure opaque parameter passed to writer
142 * @param aCount the maximum number of bytes to be read
144 * @return number of bytes read (may be less than aCount)
145 * @return 0 if reached end-of-file (or if aWriter refused to consume data)
147 * @throws NS_BASE_STREAM_WOULD_BLOCK if reading from the input stream would
148 * block the calling thread (non-blocking mode only)
149 * @throws NS_ERROR_NOT_IMPLEMENTED if the stream has no underlying buffer
150 * @throws <other-error> on failure
152 * NOTE: this function may be unimplemented if a stream has no underlying
153 * buffer (e.g., socket input stream).
155 * NOTE: this method should not throw NS_BASE_STREAM_CLOSED.
157 [noscript
] unsigned long readSegments
(in nsWriteSegmentFun aWriter
,
159 in unsigned long aCount
);
162 * @return true if stream is non-blocking
164 * NOTE: reading from a blocking input stream will block the calling thread
165 * until at least one byte of data can be extracted from the stream.
167 * NOTE: a non-blocking input stream may implement nsIAsyncInputStream to
168 * provide consumers with a way to wait for the stream to have more data
169 * once its read method is unable to return any data without blocking.
171 boolean isNonBlocking
();