Bug 438394 ? land workaround patch for unkillable hangs when loading VerifiedDownload...
[wine-gecko.git] / xpcom / io / nsStringStream.cpp
blob4a437efc1a33de77b3c64e5b00b8f93c044baa14
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:set ts=4 sts=4 sw=4 cin et: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * mcmullen@netscape.com (original author)
25 * warren@netscape.com
26 * alecf@netscape.com
27 * scc@mozilla.org
28 * david.gardiner@unisa.edu.au
29 * fur@netscape.com
30 * norris@netscape.com
31 * pinkerton@netscape.com
32 * davidm@netscape.com
33 * sfraser@netscape.com
34 * darin@netscape.com
35 * bzbarsky@mit.edu
37 * Alternatively, the contents of this file may be used under the terms of
38 * either of the GNU General Public License Version 2 or later (the "GPL"),
39 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
40 * in which case the provisions of the GPL or the LGPL are applicable instead
41 * of those above. If you wish to allow use of your version of this file only
42 * under the terms of either the GPL or the LGPL, and not to allow others to
43 * use your version of this file under the terms of the MPL, indicate your
44 * decision by deleting the provisions above and replace them with the notice
45 * and other provisions required by the GPL or the LGPL. If you do not delete
46 * the provisions above, a recipient may use your version of this file under
47 * the terms of any one of the MPL, the GPL or the LGPL.
49 * ***** END LICENSE BLOCK ***** */
51 /**
52 * Based on original code from nsIStringStream.cpp
55 #include "nsStringStream.h"
56 #include "nsStreamUtils.h"
57 #include "nsReadableUtils.h"
58 #include "nsISeekableStream.h"
59 #include "nsISupportsPrimitives.h"
60 #include "nsInt64.h"
61 #include "nsCRT.h"
62 #include "prerror.h"
63 #include "plstr.h"
64 #include "nsIClassInfoImpl.h"
66 //-----------------------------------------------------------------------------
67 // nsIStringInputStream implementation
68 //-----------------------------------------------------------------------------
70 class nsStringInputStream : public nsIStringInputStream
71 , public nsISeekableStream
72 , public nsISupportsCString
74 public:
75 NS_DECL_ISUPPORTS
76 NS_DECL_NSIINPUTSTREAM
77 NS_DECL_NSISTRINGINPUTSTREAM
78 NS_DECL_NSISEEKABLESTREAM
79 NS_DECL_NSISUPPORTSPRIMITIVE
80 NS_DECL_NSISUPPORTSCSTRING
82 nsStringInputStream()
83 : mData(nsnull)
84 , mOffset(0)
85 , mLength(0)
86 , mOwned(PR_FALSE)
89 private:
90 ~nsStringInputStream()
92 if (mData)
93 Clear();
96 PRInt32 LengthRemaining() const
98 return mLength - mOffset;
101 void Clear()
103 NS_ASSERTION(mData || !mOwned, "bad state");
104 if (mOwned)
105 NS_Free(const_cast<char*>(mData));
107 // We're about to get a new string; reset the offset.
108 mOffset = 0;
111 const char* mData;
112 PRUint32 mOffset;
113 PRUint32 mLength;
114 PRPackedBool mOwned;
117 // This class needs to support threadsafe refcounting since people often
118 // allocate a string stream, and then read it from a background thread.
119 NS_IMPL_THREADSAFE_ADDREF(nsStringInputStream)
120 NS_IMPL_THREADSAFE_RELEASE(nsStringInputStream)
122 NS_IMPL_QUERY_INTERFACE4_CI(nsStringInputStream,
123 nsIStringInputStream,
124 nsIInputStream,
125 nsISupportsCString,
126 nsISeekableStream)
127 NS_IMPL_CI_INTERFACE_GETTER4(nsStringInputStream,
128 nsIStringInputStream,
129 nsIInputStream,
130 nsISupportsCString,
131 nsISeekableStream)
133 /////////
134 // nsISupportsCString implementation
135 /////////
137 NS_IMETHODIMP
138 nsStringInputStream::GetType(PRUint16 *type)
140 *type = TYPE_CSTRING;
141 return NS_OK;
144 NS_IMETHODIMP
145 nsStringInputStream::GetData(nsACString &data)
147 // The stream doesn't have any data when it is closed. We could fake it
148 // and return an empty string here, but it seems better to keep this return
149 // value consistent with the behavior of the other 'getter' methods.
150 NS_ENSURE_TRUE(mData, NS_BASE_STREAM_CLOSED);
152 data.Assign(mData, mLength);
153 return NS_OK;
156 NS_IMETHODIMP
157 nsStringInputStream::SetData(const nsACString &data)
159 nsACString::const_iterator iter;
160 data.BeginReading(iter);
161 return SetData(iter.get(), iter.size_forward());
164 NS_IMETHODIMP
165 nsStringInputStream::ToString(char **result)
167 // NOTE: This method may result in data loss, so we do not implement it.
168 return NS_ERROR_NOT_IMPLEMENTED;
171 /////////
172 // nsIStringInputStream implementation
173 /////////
175 NS_IMETHODIMP
176 nsStringInputStream::SetData(const char *data, PRInt32 dataLen)
178 NS_ENSURE_ARG_POINTER(data);
180 if (dataLen < 0)
181 dataLen = strlen(data);
183 // NOTE: We do not use nsCRT::strndup here because that does not handle
184 // null bytes in the middle of the given data.
186 char *copy = static_cast<char *>(NS_Alloc(dataLen));
187 if (!copy)
188 return NS_ERROR_OUT_OF_MEMORY;
189 memcpy(copy, data, dataLen);
191 return AdoptData(copy, dataLen);
194 NS_IMETHODIMP
195 nsStringInputStream::AdoptData(char *data, PRInt32 dataLen)
197 NS_ENSURE_ARG_POINTER(data);
199 if (dataLen < 0)
200 dataLen = strlen(data);
202 Clear();
204 mData = data;
205 mLength = dataLen;
206 mOwned = PR_TRUE;
207 return NS_OK;
210 NS_IMETHODIMP
211 nsStringInputStream::ShareData(const char *data, PRInt32 dataLen)
213 NS_ENSURE_ARG_POINTER(data);
215 if (dataLen < 0)
216 dataLen = strlen(data);
218 Clear();
220 mData = data;
221 mLength = dataLen;
222 mOwned = PR_FALSE;
223 return NS_OK;
226 /////////
227 // nsIInputStream implementation
228 /////////
230 NS_IMETHODIMP
231 nsStringInputStream::Close()
233 Clear();
234 mData = nsnull;
235 mLength = 0;
236 mOwned = PR_FALSE;
237 return NS_OK;
240 NS_IMETHODIMP
241 nsStringInputStream::Available(PRUint32 *aLength)
243 NS_ASSERTION(aLength, "null ptr");
245 if (!mData)
246 return NS_BASE_STREAM_CLOSED;
248 *aLength = LengthRemaining();
249 return NS_OK;
252 NS_IMETHODIMP
253 nsStringInputStream::Read(char* aBuf, PRUint32 aCount, PRUint32 *aReadCount)
255 NS_ASSERTION(aBuf, "null ptr");
256 return ReadSegments(NS_CopySegmentToBuffer, aBuf, aCount, aReadCount);
259 NS_IMETHODIMP
260 nsStringInputStream::ReadSegments(nsWriteSegmentFun writer, void *closure,
261 PRUint32 aCount, PRUint32 *result)
263 NS_ASSERTION(result, "null ptr");
264 NS_ASSERTION(mLength >= mOffset, "bad stream state");
266 // We may be at end-of-file
267 PRUint32 maxCount = LengthRemaining();
268 if (maxCount == 0) {
269 *result = 0;
270 return NS_OK;
272 NS_ASSERTION(mData, "must have data if maxCount != 0");
274 if (aCount > maxCount)
275 aCount = maxCount;
276 nsresult rv = writer(this, closure, mData + mOffset, 0, aCount, result);
277 if (NS_SUCCEEDED(rv)) {
278 NS_ASSERTION(*result <= aCount,
279 "writer should not write more than we asked it to write");
280 mOffset += *result;
283 // errors returned from the writer end here!
284 return NS_OK;
287 NS_IMETHODIMP
288 nsStringInputStream::IsNonBlocking(PRBool *aNonBlocking)
290 *aNonBlocking = PR_TRUE;
291 return NS_OK;
294 /////////
295 // nsISeekableStream implementation
296 /////////
298 NS_IMETHODIMP
299 nsStringInputStream::Seek(PRInt32 whence, PRInt64 offset)
301 if (!mData)
302 return NS_BASE_STREAM_CLOSED;
304 // Compute new stream position. The given offset may be a negative value.
306 PRInt64 newPos = offset;
307 switch (whence) {
308 case NS_SEEK_SET:
309 break;
310 case NS_SEEK_CUR:
311 newPos += (PRInt32) mOffset;
312 break;
313 case NS_SEEK_END:
314 newPos += (PRInt32) mLength;
315 break;
316 default:
317 NS_ERROR("invalid whence");
318 return NS_ERROR_INVALID_ARG;
321 // mLength is never larger than PR_INT32_MAX due to the way it is assigned.
323 NS_ENSURE_ARG(newPos >= 0);
324 NS_ENSURE_ARG(newPos <= (PRInt32) mLength);
326 mOffset = (PRInt32) newPos;
327 return NS_OK;
330 NS_IMETHODIMP
331 nsStringInputStream::Tell(PRInt64* outWhere)
333 if (!mData)
334 return NS_BASE_STREAM_CLOSED;
336 *outWhere = mOffset;
337 return NS_OK;
340 NS_IMETHODIMP
341 nsStringInputStream::SetEOF()
343 if (!mData)
344 return NS_BASE_STREAM_CLOSED;
346 mLength = mOffset;
347 return NS_OK;
350 NS_COM nsresult
351 NS_NewByteInputStream(nsIInputStream** aStreamResult,
352 const char* aStringToRead, PRInt32 aLength,
353 nsAssignmentType aAssignment)
355 NS_PRECONDITION(aStreamResult, "null out ptr");
357 nsStringInputStream* stream = new nsStringInputStream();
358 if (! stream)
359 return NS_ERROR_OUT_OF_MEMORY;
361 NS_ADDREF(stream);
363 nsresult rv;
364 switch (aAssignment) {
365 case NS_ASSIGNMENT_COPY:
366 rv = stream->SetData(aStringToRead, aLength);
367 break;
368 case NS_ASSIGNMENT_DEPEND:
369 rv = stream->ShareData(aStringToRead, aLength);
370 break;
371 case NS_ASSIGNMENT_ADOPT:
372 rv = stream->AdoptData(const_cast<char*>(aStringToRead), aLength);
373 break;
374 default:
375 NS_ERROR("invalid assignment type");
376 rv = NS_ERROR_INVALID_ARG;
379 if (NS_FAILED(rv)) {
380 NS_RELEASE(stream);
381 return rv;
384 *aStreamResult = stream;
385 return NS_OK;
388 NS_COM nsresult
389 NS_NewStringInputStream(nsIInputStream** aStreamResult,
390 const nsAString& aStringToRead)
392 char* data = ToNewCString(aStringToRead); // truncates high-order bytes
393 if (!data)
394 return NS_ERROR_OUT_OF_MEMORY;
396 nsresult rv = NS_NewByteInputStream(aStreamResult, data,
397 aStringToRead.Length(),
398 NS_ASSIGNMENT_ADOPT);
399 if (NS_FAILED(rv))
400 NS_Free(data);
401 return rv;
404 NS_COM nsresult
405 NS_NewCStringInputStream(nsIInputStream** aStreamResult,
406 const nsACString& aStringToRead)
408 nsACString::const_iterator data;
409 aStringToRead.BeginReading(data);
411 return NS_NewByteInputStream(aStreamResult, data.get(), data.size_forward(),
412 NS_ASSIGNMENT_COPY);
415 // factory method for constructing a nsStringInputStream object
416 NS_METHOD
417 nsStringInputStreamConstructor(nsISupports *outer, REFNSIID iid, void **result)
419 *result = nsnull;
421 NS_ENSURE_TRUE(!outer, NS_ERROR_NO_AGGREGATION);
423 nsStringInputStream *inst = new nsStringInputStream();
424 if (!inst)
425 return NS_ERROR_OUT_OF_MEMORY;
427 NS_ADDREF(inst);
428 nsresult rv = inst->QueryInterface(iid, result);
429 NS_RELEASE(inst);
431 return rv;