xpcom/io/nsStreamUtils.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #ifndef nsStreamUtils_h__
michael@0 6 #define nsStreamUtils_h__
michael@0 7
michael@0 8 #include "nsCOMPtr.h"
michael@0 9 #include "nsStringFwd.h"
michael@0 10 #include "nsIInputStream.h"
michael@0 11 #include "nsTArray.h"
michael@0 12
michael@0 13 class nsIOutputStream;
michael@0 14 class nsIInputStreamCallback;
michael@0 15 class nsIOutputStreamCallback;
michael@0 16 class nsIEventTarget;
michael@0 17
michael@0 18 /**
michael@0 19 * A "one-shot" proxy of the OnInputStreamReady callback. The resulting
michael@0 20 * proxy object's OnInputStreamReady function may only be called once! The
michael@0 21 * proxy object ensures that the real notify object will be free'd on the
michael@0 22 * thread corresponding to the given event target regardless of what thread
michael@0 23 * the proxy object is destroyed on.
michael@0 24 *
michael@0 25 * This function is designed to be used to implement AsyncWait when the
michael@0 26 * aTarget parameter is non-null.
michael@0 27 */
michael@0 28 extern already_AddRefed<nsIInputStreamCallback>
michael@0 29 NS_NewInputStreamReadyEvent(nsIInputStreamCallback *aNotify,
michael@0 30 nsIEventTarget *aTarget);
michael@0 31
michael@0 32 /**
michael@0 33 * A "one-shot" proxy of the OnOutputStreamReady callback. The resulting
michael@0 34 * proxy object's OnOutputStreamReady function may only be called once! The
michael@0 35 * proxy object ensures that the real notify object will be free'd on the
michael@0 36 * thread corresponding to the given event target regardless of what thread
michael@0 37 * the proxy object is destroyed on.
michael@0 38 *
michael@0 39 * This function is designed to be used to implement AsyncWait when the
michael@0 40 * aTarget parameter is non-null.
michael@0 41 */
michael@0 42 extern already_AddRefed<nsIOutputStreamCallback>
michael@0 43 NS_NewOutputStreamReadyEvent(nsIOutputStreamCallback *aNotify,
michael@0 44 nsIEventTarget *aTarget);
michael@0 45
michael@0 46 /* ------------------------------------------------------------------------- */
michael@0 47
michael@0 48 enum nsAsyncCopyMode {
michael@0 49 NS_ASYNCCOPY_VIA_READSEGMENTS,
michael@0 50 NS_ASYNCCOPY_VIA_WRITESEGMENTS
michael@0 51 };
michael@0 52
michael@0 53 /**
michael@0 54 * This function is called when a new chunk of data has been copied. The
michael@0 55 * reported count is the size of the current chunk.
michael@0 56 */
michael@0 57 typedef void (* nsAsyncCopyProgressFun)(void *closure, uint32_t count);
michael@0 58
michael@0 59 /**
michael@0 60 * This function is called when the async copy process completes. The reported
michael@0 61 * status is NS_OK on success and some error code on failure.
michael@0 62 */
michael@0 63 typedef void (* nsAsyncCopyCallbackFun)(void *closure, nsresult status);
michael@0 64
michael@0 65 /**
michael@0 66 * This function asynchronously copies data from the source to the sink. All
michael@0 67 * data transfer occurs on the thread corresponding to the given event target.
michael@0 68 * A null event target is not permitted.
michael@0 69 *
michael@0 70 * The copier handles blocking or non-blocking streams transparently. If a
michael@0 71 * stream operation returns NS_BASE_STREAM_WOULD_BLOCK, then the stream will
michael@0 72 * be QI'd to nsIAsync{In,Out}putStream and its AsyncWait method will be used
michael@0 73 * to determine when to resume copying.
michael@0 74 *
michael@0 75 * Source and sink are closed by default when copying finishes or when error
michael@0 76 * occurs. Caller can prevent closing source or sink by setting aCloseSource
michael@0 77 * or aCloseSink to false.
michael@0 78 *
michael@0 79 * Caller can obtain aCopierCtx to be able to cancel copying.
michael@0 80 */
michael@0 81 extern nsresult
michael@0 82 NS_AsyncCopy(nsIInputStream *aSource,
michael@0 83 nsIOutputStream *aSink,
michael@0 84 nsIEventTarget *aTarget,
michael@0 85 nsAsyncCopyMode aMode = NS_ASYNCCOPY_VIA_READSEGMENTS,
michael@0 86 uint32_t aChunkSize = 4096,
michael@0 87 nsAsyncCopyCallbackFun aCallbackFun = nullptr,
michael@0 88 void *aCallbackClosure = nullptr,
michael@0 89 bool aCloseSource = true,
michael@0 90 bool aCloseSink = true,
michael@0 91 nsISupports **aCopierCtx = nullptr,
michael@0 92 nsAsyncCopyProgressFun aProgressCallbackFun = nullptr);
michael@0 93
michael@0 94 /**
michael@0 95 * This function cancels copying started by function NS_AsyncCopy.
michael@0 96 *
michael@0 97 * @param aCopierCtx
michael@0 98 * Copier context returned by NS_AsyncCopy.
michael@0 99 * @param aReason
michael@0 100 * A failure code indicating why the operation is being canceled.
michael@0 101 * It is an error to pass a success code.
michael@0 102 */
michael@0 103 extern nsresult
michael@0 104 NS_CancelAsyncCopy(nsISupports *aCopierCtx, nsresult aReason);
michael@0 105
michael@0 106 /**
michael@0 107 * This function copies all of the available data from the stream (up to at
michael@0 108 * most aMaxCount bytes) into the given buffer. The buffer is truncated at
michael@0 109 * the start of the function.
michael@0 110 *
michael@0 111 * If an error occurs while reading from the stream or while attempting to
michael@0 112 * resize the buffer, then the corresponding error code is returned from this
michael@0 113 * function, and any data that has already been read will be returned in the
michael@0 114 * output buffer. This allows one to use this function with a non-blocking
michael@0 115 * input stream that may return NS_BASE_STREAM_WOULD_BLOCK if it only has
michael@0 116 * partial data available.
michael@0 117 *
michael@0 118 * @param aSource
michael@0 119 * The input stream to read.
michael@0 120 * @param aMaxCount
michael@0 121 * The maximum number of bytes to consume from the stream. Pass the
michael@0 122 * value UINT32_MAX to consume the entire stream. The number of
michael@0 123 * bytes actually read is given by the length of aBuffer upon return.
michael@0 124 * @param aBuffer
michael@0 125 * The string object that will contain the stream data upon return.
michael@0 126 * Note: The data copied to the string may contain null bytes and may
michael@0 127 * contain non-ASCII values.
michael@0 128 */
michael@0 129 extern nsresult
michael@0 130 NS_ConsumeStream(nsIInputStream *aSource, uint32_t aMaxCount,
michael@0 131 nsACString &aBuffer);
michael@0 132
michael@0 133 /**
michael@0 134 * This function tests whether or not the input stream is buffered. A buffered
michael@0 135 * input stream is one that implements readSegments. The test for this is to
michael@0 136 * 1/ check whether the input stream implements nsIBufferedInputStream;
michael@0 137 * 2/ if not, call readSegments, without actually consuming any data from the
michael@0 138 * stream, to verify that it functions.
michael@0 139 *
michael@0 140 * NOTE: If the stream is non-blocking and has no data available yet, then this
michael@0 141 * test will fail. In that case, we return false even though the test is not
michael@0 142 * really conclusive.
michael@0 143 *
michael@0 144 * PERFORMANCE NOTE: If the stream does not implement nsIBufferedInputStream,
michael@0 145 * calling readSegments may cause I/O. Therefore, you should avoid calling
michael@0 146 * this function from the main thread.
michael@0 147 *
michael@0 148 * @param aInputStream
michael@0 149 * The input stream to test.
michael@0 150 */
michael@0 151 extern bool
michael@0 152 NS_InputStreamIsBuffered(nsIInputStream *aInputStream);
michael@0 153
michael@0 154 /**
michael@0 155 * This function tests whether or not the output stream is buffered. A
michael@0 156 * buffered output stream is one that implements writeSegments. The test for
michael@0 157 * this is to:
michael@0 158 * 1/ check whether the output stream implements nsIBufferedOutputStream;
michael@0 159 * 2/ if not, call writeSegments, without actually writing any data into
michael@0 160 * the stream, to verify that it functions.
michael@0 161 *
michael@0 162 * NOTE: If the stream is non-blocking and has no available space yet, then
michael@0 163 * this test will fail. In that case, we return false even though the test is
michael@0 164 * not really conclusive.
michael@0 165 *
michael@0 166 * PERFORMANCE NOTE: If the stream does not implement nsIBufferedOutputStream,
michael@0 167 * calling writeSegments may cause I/O. Therefore, you should avoid calling
michael@0 168 * this function from the main thread.
michael@0 169 *
michael@0 170 * @param aOutputStream
michael@0 171 * The output stream to test.
michael@0 172 */
michael@0 173 extern bool
michael@0 174 NS_OutputStreamIsBuffered(nsIOutputStream *aOutputStream);
michael@0 175
michael@0 176 /**
michael@0 177 * This function is intended to be passed to nsIInputStream::ReadSegments to
michael@0 178 * copy data from the nsIInputStream into a nsIOutputStream passed as the
michael@0 179 * aClosure parameter to the ReadSegments function.
michael@0 180 *
michael@0 181 * @see nsIInputStream.idl for a description of this function's parameters.
michael@0 182 */
michael@0 183 extern NS_METHOD
michael@0 184 NS_CopySegmentToStream(nsIInputStream *aInputStream, void *aClosure,
michael@0 185 const char *aFromSegment, uint32_t aToOffset,
michael@0 186 uint32_t aCount, uint32_t *aWriteCount);
michael@0 187
michael@0 188 /**
michael@0 189 * This function is intended to be passed to nsIInputStream::ReadSegments to
michael@0 190 * copy data from the nsIInputStream into a character buffer passed as the
michael@0 191 * aClosure parameter to the ReadSegments function. The character buffer
michael@0 192 * must be at least as large as the aCount parameter passed to ReadSegments.
michael@0 193 *
michael@0 194 * @see nsIInputStream.idl for a description of this function's parameters.
michael@0 195 */
michael@0 196 extern NS_METHOD
michael@0 197 NS_CopySegmentToBuffer(nsIInputStream *aInputStream, void *aClosure,
michael@0 198 const char *aFromSegment, uint32_t aToOffset,
michael@0 199 uint32_t aCount, uint32_t *aWriteCount);
michael@0 200
michael@0 201 /**
michael@0 202 * This function is intended to be passed to nsIOutputStream::WriteSegments to
michael@0 203 * copy data into the nsIOutputStream from a character buffer passed as the
michael@0 204 * aClosure parameter to the WriteSegments function.
michael@0 205 *
michael@0 206 * @see nsIOutputStream.idl for a description of this function's parameters.
michael@0 207 */
michael@0 208 extern NS_METHOD
michael@0 209 NS_CopySegmentToBuffer(nsIOutputStream *aOutputStream, void *aClosure,
michael@0 210 char *aToSegment, uint32_t aFromOffset,
michael@0 211 uint32_t aCount, uint32_t *aReadCount);
michael@0 212
michael@0 213 /**
michael@0 214 * This function is intended to be passed to nsIInputStream::ReadSegments to
michael@0 215 * discard data from the nsIInputStream. This can be used to efficiently read
michael@0 216 * data from the stream without actually copying any bytes.
michael@0 217 *
michael@0 218 * @see nsIInputStream.idl for a description of this function's parameters.
michael@0 219 */
michael@0 220 extern NS_METHOD
michael@0 221 NS_DiscardSegment(nsIInputStream *aInputStream, void *aClosure,
michael@0 222 const char *aFromSegment, uint32_t aToOffset,
michael@0 223 uint32_t aCount, uint32_t *aWriteCount);
michael@0 224
michael@0 225 /**
michael@0 226 * This function is intended to be passed to nsIInputStream::ReadSegments to
michael@0 227 * adjust the aInputStream parameter passed to a consumer's WriteSegmentFun.
michael@0 228 * The aClosure parameter must be a pointer to a nsWriteSegmentThunk object.
michael@0 229 * The mStream and mClosure members of that object will be passed to the mFun
michael@0 230 * function, with the remainder of the parameters being what are passed to
michael@0 231 * NS_WriteSegmentThunk.
michael@0 232 *
michael@0 233 * This function comes in handy when implementing ReadSegments in terms of an
michael@0 234 * inner stream's ReadSegments.
michael@0 235 */
michael@0 236 extern NS_METHOD
michael@0 237 NS_WriteSegmentThunk(nsIInputStream *aInputStream, void *aClosure,
michael@0 238 const char *aFromSegment, uint32_t aToOffset,
michael@0 239 uint32_t aCount, uint32_t *aWriteCount);
michael@0 240
michael@0 241 struct nsWriteSegmentThunk {
michael@0 242 nsIInputStream *mStream;
michael@0 243 nsWriteSegmentFun mFun;
michael@0 244 void *mClosure;
michael@0 245 };
michael@0 246
michael@0 247 /**
michael@0 248 * Read data from aInput and store in aDest. A non-zero aKeep will keep that
michael@0 249 * many bytes from aDest (from the end). New data is appended after the kept
michael@0 250 * bytes (if any). aDest's new length on returning from this function is
michael@0 251 * aKeep + aNewBytes and is guaranteed to be less than or equal to aDest's
michael@0 252 * current capacity.
michael@0 253 * @param aDest the array to fill
michael@0 254 * @param aInput the stream to read from
michael@0 255 * @param aKeep number of bytes to keep (0 <= aKeep <= aDest.Length())
michael@0 256 * @param aNewBytes (out) number of bytes read from aInput or zero if Read()
michael@0 257 * failed
michael@0 258 * @return the result from aInput->Read(...)
michael@0 259 */
michael@0 260 extern NS_METHOD
michael@0 261 NS_FillArray(FallibleTArray<char> &aDest, nsIInputStream *aInput,
michael@0 262 uint32_t aKeep, uint32_t *aNewBytes);
michael@0 263
michael@0 264 #endif // !nsStreamUtils_h__

mercurial