Bug 1857386 [wpt PR 42383] - Update wpt metadata, a=testonly
[gecko.git] / xpcom / io / nsIOutputStream.idl
blobffe9a5ffedf7f120a3d0c7bbc269c07c64e3f7d8
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 nsIOutputStream;
9 interface nsIInputStream;
11 %{C++
12 /**
13 * The signature for the reader function passed to WriteSegments. This
14 * is the "provider" of data that gets written into the stream's buffer.
16 * @param aOutStream stream being written to
17 * @param aClosure opaque parameter passed to WriteSegments
18 * @param aToSegment pointer to memory owned by the output stream
19 * @param aFromOffset amount already written (since WriteSegments was called)
20 * @param aCount length of toSegment
21 * @param aReadCount number of bytes written
23 * Implementers should return the following:
25 * @throws <any-error> if not interested in providing any data
27 * Errors are never passed to the caller of WriteSegments.
29 typedef nsresult (*nsReadSegmentFun)(nsIOutputStream *aOutStream,
30 void *aClosure,
31 char *aToSegment,
32 uint32_t aFromOffset,
33 uint32_t aCount,
34 uint32_t *aReadCount);
37 native nsReadSegmentFun(nsReadSegmentFun);
39 /**
40 * nsIOutputStream
42 * An interface describing a writable stream of data. An output stream may be
43 * "blocking" or "non-blocking" (see the IsNonBlocking method). A blocking
44 * output stream may suspend the calling thread in order to satisfy a call to
45 * Close, Flush, Write, WriteFrom, or WriteSegments. A non-blocking output
46 * stream, on the other hand, must not block the calling thread of execution.
48 * NOTE: blocking output streams are often written to on a background thread to
49 * avoid locking up the main application thread. For this reason, it is
50 * generally the case that a blocking output stream should be implemented using
51 * thread- safe AddRef and Release.
53 [scriptable, builtinclass, uuid(0d0acd2a-61b4-11d4-9877-00c04fa0cf4a)]
54 interface nsIOutputStream : nsISupports
56 /**
57 * Close the stream. Forces the output stream to flush any buffered data.
58 * Any subsequent calls to StreamStatus should throw NS_BASE_STREAM_CLOSED.
59 * Succeeds without effect if already closed.
61 * @throws NS_BASE_STREAM_WOULD_BLOCK if unable to flush without blocking
62 * the calling thread (non-blocking mode only)
64 void close();
66 /**
67 * Flush the stream.
69 * @throws NS_BASE_STREAM_WOULD_BLOCK if unable to flush without blocking
70 * the calling thread (non-blocking mode only)
72 void flush();
74 /**
75 * Check the current status of the stream. A stream that is closed will
76 * throw an exception when this method is called. That enables the caller
77 * to know the condition of the stream before attempting to write into it.
79 * This method will not throw NS_BASE_STREAM_WOULD_BLOCK, even if the stream
80 * is a non-blocking stream with no available space. A non-blocking stream
81 * which has not been closed, but has no available room should return NS_OK.
83 * NOTE: This method should not block the calling thread (e.g. to query the
84 * state of a file descriptor), even when called on a blocking stream.
86 * @throws NS_BASE_STREAM_CLOSED if the stream closed normally
87 * @throws <other-error> if the stream closed with a different status
89 void streamStatus();
91 /**
92 * Write data into the stream.
94 * @param aBuf the buffer containing the data to be written
95 * @param aCount the maximum number of bytes to be written
97 * @return number of bytes written (may be less than aCount)
99 * @throws NS_BASE_STREAM_WOULD_BLOCK if writing to the output stream would
100 * block the calling thread (non-blocking mode only)
101 * @throws <other-error> on failure
103 unsigned long write(in string aBuf, in unsigned long aCount);
106 * Writes data into the stream from an input stream.
108 * @param aFromStream the stream containing the data to be written
109 * @param aCount the maximum number of bytes to be written
111 * @return number of bytes written (may be less than aCount)
113 * @throws NS_BASE_STREAM_WOULD_BLOCK if writing to the output stream would
114 * block the calling thread (non-blocking mode only). This failure
115 * means no bytes were transferred.
116 * @throws <other-error> on failure
118 * NOTE: This method is defined by this interface in order to allow the
119 * output stream to efficiently copy the data from the input stream into
120 * its internal buffer (if any). If this method was provided as an external
121 * facility, a separate char* buffer would need to be used in order to call
122 * the output stream's other Write method.
124 unsigned long writeFrom(in nsIInputStream aFromStream,
125 in unsigned long aCount);
128 * Low-level write method that has access to the stream's underlying buffer.
129 * The reader function may be called multiple times for segmented buffers.
130 * WriteSegments is expected to keep calling the reader until either there
131 * is nothing left to write or the reader returns an error. WriteSegments
132 * should not call the reader with zero bytes to provide.
134 * @param aReader the "provider" of the data to be written
135 * @param aClosure opaque parameter passed to reader
136 * @param aCount the maximum number of bytes to be written
138 * @return number of bytes written (may be less than aCount)
140 * @throws NS_BASE_STREAM_WOULD_BLOCK if writing to the output stream would
141 * block the calling thread (non-blocking mode only). This failure
142 * means no bytes were transferred.
143 * @throws NS_ERROR_NOT_IMPLEMENTED if the stream has no underlying buffer
144 * @throws <other-error> on failure
146 * NOTE: this function may be unimplemented if a stream has no underlying
147 * buffer (e.g., socket output stream).
149 [noscript] unsigned long writeSegments(in nsReadSegmentFun aReader,
150 in voidPtr aClosure,
151 in unsigned long aCount);
154 * @return true if stream is non-blocking
156 * NOTE: writing to a blocking output stream will block the calling thread
157 * until all given data can be consumed by the stream.
159 * NOTE: a non-blocking output stream may implement nsIAsyncOutputStream to
160 * provide consumers with a way to wait for the stream to accept more data
161 * once its write method is unable to accept any data without blocking.
163 boolean isNonBlocking();