VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/io/nsMultiplexInputStream.cpp@ 44770

最後變更 在這個檔案從44770是 1,由 vboxsync 提交於 55 年 前

import

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 11.4 KB
 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is frightening to behold.
16 *
17 * The Initial Developer of the Original Code is
18 * Jonas Sicking.
19 * Portions created by the Initial Developer are Copyright (C) 2001
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Jonas Sicking <[email protected]>
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39/**
40 * The multiplex stream concatenates a list of input streams into a single
41 * stream.
42 */
43
44#include "nsMultiplexInputStream.h"
45#include "nsIMultiplexInputStream.h"
46#include "nsISeekableStream.h"
47#include "nsSupportsArray.h"
48#include "nsInt64.h"
49
50class nsMultiplexInputStream : public nsIMultiplexInputStream,
51 public nsISeekableStream
52{
53public:
54 nsMultiplexInputStream();
55
56 NS_DECL_ISUPPORTS
57 NS_DECL_NSIINPUTSTREAM
58 NS_DECL_NSIMULTIPLEXINPUTSTREAM
59 NS_DECL_NSISEEKABLESTREAM
60
61 static NS_METHOD Create(nsISupports *outer, REFNSIID iid, void **result);
62
63private:
64 ~nsMultiplexInputStream() {}
65
66
67 struct ReadSegmentsState {
68 nsIInputStream* mThisStream;
69 PRUint32 mOffset;
70 nsWriteSegmentFun mWriter;
71 void* mClosure;
72 PRBool mDone;
73 };
74
75 static NS_METHOD ReadSegCb(nsIInputStream* aIn, void* aClosure,
76 const char* aFromRawSegment, PRUint32 aToOffset,
77 PRUint32 aCount, PRUint32 *aWriteCount);
78
79 nsSupportsArray mStreams;
80 PRUint32 mCurrentStream;
81 PRBool mStartedReadingCurrent;
82};
83
84
85NS_IMPL_THREADSAFE_ISUPPORTS3(nsMultiplexInputStream,
86 nsIMultiplexInputStream,
87 nsIInputStream,
88 nsISeekableStream)
89
90nsMultiplexInputStream::nsMultiplexInputStream()
91 : mCurrentStream(0),
92 mStartedReadingCurrent(PR_FALSE)
93{
94}
95
96/* readonly attribute unsigned long count; */
97NS_IMETHODIMP
98nsMultiplexInputStream::GetCount(PRUint32 *aCount)
99{
100 mStreams.Count(aCount);
101 return NS_OK;
102}
103
104/* void appendStream (in nsIInputStream stream); */
105NS_IMETHODIMP
106nsMultiplexInputStream::AppendStream(nsIInputStream *aStream)
107{
108 return mStreams.AppendElement(aStream);
109}
110
111/* void insertStream (in nsIInputStream stream, in unsigned long index); */
112NS_IMETHODIMP
113nsMultiplexInputStream::InsertStream(nsIInputStream *aStream, PRUint32 aIndex)
114{
115 nsresult rv = mStreams.InsertElementAt(aStream, aIndex);
116 NS_ENSURE_SUCCESS(rv, rv);
117 if (mCurrentStream > aIndex ||
118 (mCurrentStream == aIndex && mStartedReadingCurrent))
119 ++mCurrentStream;
120 return rv;
121}
122
123/* void removeStream (in unsigned long index); */
124NS_IMETHODIMP
125nsMultiplexInputStream::RemoveStream(PRUint32 aIndex)
126{
127 nsresult rv = mStreams.RemoveElementAt(aIndex);
128 NS_ENSURE_SUCCESS(rv, rv);
129 if (mCurrentStream > aIndex)
130 --mCurrentStream;
131 else if (mCurrentStream == aIndex)
132 mStartedReadingCurrent = PR_FALSE;
133
134 return rv;
135}
136
137/* nsIInputStream getStream (in unsigned long index); */
138NS_IMETHODIMP
139nsMultiplexInputStream::GetStream(PRUint32 aIndex, nsIInputStream **_retval)
140{
141 return mStreams.QueryElementAt(aIndex,
142 NS_GET_IID(nsIInputStream),
143 (void**)_retval);
144}
145
146/* void close (); */
147NS_IMETHODIMP
148nsMultiplexInputStream::Close()
149{
150 PRUint32 len, i;
151 nsresult rv = NS_OK;
152
153 mStreams.Count(&len);
154 for (i = 0; i < len; ++i) {
155 nsCOMPtr<nsIInputStream> stream(do_QueryElementAt(&mStreams, i));
156 nsresult rv2 = stream->Close();
157 // We still want to close all streams, but we should return an error
158 if (NS_FAILED(rv2))
159 rv = rv2;
160 }
161 return rv;
162}
163
164/* unsigned long available (); */
165NS_IMETHODIMP
166nsMultiplexInputStream::Available(PRUint32 *_retval)
167{
168 nsresult rv;
169 PRUint32 i, len, avail = 0;
170
171 mStreams.Count(&len);
172 for (i = mCurrentStream; i < len; i++) {
173 nsCOMPtr<nsIInputStream> stream(do_QueryElementAt(&mStreams, i));
174
175 PRUint32 streamAvail;
176 rv = stream->Available(&streamAvail);
177 NS_ENSURE_SUCCESS(rv, rv);
178 avail += streamAvail;
179 }
180 *_retval = avail;
181 return NS_OK;
182}
183
184/* [noscript] unsigned long read (in charPtr buf, in unsigned long count); */
185NS_IMETHODIMP
186nsMultiplexInputStream::Read(char * aBuf, PRUint32 aCount, PRUint32 *_retval)
187{
188 nsresult rv = NS_OK;
189 PRUint32 len, read;
190
191 *_retval = 0;
192
193 mStreams.Count(&len);
194 while (mCurrentStream < len && aCount) {
195 nsCOMPtr<nsIInputStream> stream(do_QueryElementAt(&mStreams,
196 mCurrentStream));
197 rv = stream->Read(aBuf, aCount, &read);
198
199 // XXX some streams return NS_BASE_STREAM_CLOSED to indicate EOF.
200 if (rv == NS_BASE_STREAM_CLOSED) {
201 rv = NS_OK;
202 read = 0;
203 }
204 else if (NS_FAILED(rv))
205 break;
206
207 if (read == 0) {
208 ++mCurrentStream;
209 mStartedReadingCurrent = PR_FALSE;
210 }
211 else {
212 NS_ASSERTION(aCount >= read, "Read more than requested");
213 *_retval += read;
214 aCount -= read;
215 aBuf += read;
216 mStartedReadingCurrent = PR_TRUE;
217 }
218 }
219 return *_retval ? NS_OK : rv;
220}
221
222/* [noscript] unsigned long readSegments (in nsWriteSegmentFun writer,
223 * in voidPtr closure,
224 * in unsigned long count); */
225NS_IMETHODIMP
226nsMultiplexInputStream::ReadSegments(nsWriteSegmentFun aWriter, void *aClosure,
227 PRUint32 aCount, PRUint32 *_retval)
228{
229 NS_ASSERTION(aWriter, "missing aWriter");
230
231 nsresult rv = NS_OK;
232 ReadSegmentsState state;
233 state.mThisStream = this;
234 state.mOffset = 0;
235 state.mWriter = aWriter;
236 state.mClosure = aClosure;
237 state.mDone = PR_FALSE;
238
239 PRUint32 len;
240 mStreams.Count(&len);
241 while (mCurrentStream < len && aCount) {
242 nsCOMPtr<nsIInputStream> stream(do_QueryElementAt(&mStreams,
243 mCurrentStream));
244 PRUint32 read;
245 rv = stream->ReadSegments(ReadSegCb, &state, aCount, &read);
246
247 // XXX some streams return NS_BASE_STREAM_CLOSED to indicate EOF.
248 if (rv == NS_BASE_STREAM_CLOSED) {
249 rv = NS_OK;
250 read = 0;
251 }
252
253 // if |aWriter| decided to stop reading segments...
254 if (state.mDone || NS_FAILED(rv))
255 break;
256
257 // if stream is empty, then advance to the next stream.
258 if (read == 0) {
259 ++mCurrentStream;
260 mStartedReadingCurrent = PR_FALSE;
261 }
262 else {
263 NS_ASSERTION(aCount >= read, "Read more than requested");
264 state.mOffset += read;
265 aCount -= read;
266 mStartedReadingCurrent = PR_TRUE;
267 }
268 }
269
270 // if we successfully read some data, then this call succeeded.
271 *_retval = state.mOffset;
272 return state.mOffset ? NS_OK : rv;
273}
274
275NS_METHOD
276nsMultiplexInputStream::ReadSegCb(nsIInputStream* aIn, void* aClosure,
277 const char* aFromRawSegment,
278 PRUint32 aToOffset, PRUint32 aCount,
279 PRUint32 *aWriteCount)
280{
281 nsresult rv;
282 ReadSegmentsState* state = (ReadSegmentsState*)aClosure;
283 rv = (state->mWriter)(state->mThisStream,
284 state->mClosure,
285 aFromRawSegment,
286 aToOffset + state->mOffset,
287 aCount,
288 aWriteCount);
289 if (NS_FAILED(rv))
290 state->mDone = PR_TRUE;
291 return rv;
292}
293
294/* readonly attribute boolean nonBlocking; */
295NS_IMETHODIMP
296nsMultiplexInputStream::IsNonBlocking(PRBool *aNonBlocking)
297{
298 nsresult rv;
299 PRUint32 i, len;
300
301 mStreams.Count(&len);
302 for (i = 0; i < len; ++i) {
303 nsCOMPtr<nsIInputStream> stream(do_QueryElementAt(&mStreams, i));
304 rv = stream->IsNonBlocking(aNonBlocking);
305 NS_ENSURE_SUCCESS(rv, rv);
306 // If one is non-blocking the entire stream becomes non-blocking
307 if (*aNonBlocking)
308 return NS_OK;
309 }
310 return NS_OK;
311}
312
313/* void seek (in PRInt32 whence, in PRInt32 offset); */
314NS_IMETHODIMP
315nsMultiplexInputStream::Seek(PRInt32 aWhence, PRInt64 aOffset)
316{
317 nsresult rv;
318
319 // rewinding to start is easy, and should be the most common case
320 if (aWhence == NS_SEEK_SET && aOffset == 0)
321 {
322 PRUint32 i, last;
323 last = mStartedReadingCurrent ? mCurrentStream+1 : mCurrentStream;
324 for (i = 0; i < last; ++i) {
325 nsCOMPtr<nsISeekableStream> stream(do_QueryElementAt(&mStreams, i));
326 NS_ENSURE_TRUE(stream, NS_ERROR_NO_INTERFACE);
327
328 rv = stream->Seek(NS_SEEK_SET, 0);
329 NS_ENSURE_SUCCESS(rv, rv);
330 }
331 mCurrentStream = 0;
332 mStartedReadingCurrent = PR_FALSE;
333 return NS_OK;
334 }
335
336 // other Seeks not implemented yet
337 return NS_ERROR_NOT_IMPLEMENTED;
338}
339
340/* PRUint32 tell (); */
341NS_IMETHODIMP
342nsMultiplexInputStream::Tell(PRInt64 *_retval)
343{
344 nsresult rv;
345 nsInt64 ret64 = 0;
346 PRUint32 i, last;
347 last = mStartedReadingCurrent ? mCurrentStream+1 : mCurrentStream;
348 for (i = 0; i < last; ++i) {
349 nsCOMPtr<nsISeekableStream> stream(do_QueryElementAt(&mStreams, i));
350 NS_ENSURE_TRUE(stream, NS_ERROR_NO_INTERFACE);
351
352 PRInt64 pos;
353 rv = stream->Tell(&pos);
354 NS_ENSURE_SUCCESS(rv, rv);
355 ret64 += pos;
356 }
357 *_retval = ret64;
358
359 return NS_OK;
360}
361
362/* void setEOF (); */
363NS_IMETHODIMP
364nsMultiplexInputStream::SetEOF()
365{
366 return NS_ERROR_NOT_IMPLEMENTED;
367}
368
369NS_METHOD
370nsMultiplexInputStreamConstructor(nsISupports *outer,
371 REFNSIID iid,
372 void **result)
373{
374 *result = nsnull;
375
376 if (outer)
377 return NS_ERROR_NO_AGGREGATION;
378
379 nsMultiplexInputStream *inst;
380 NS_NEWXPCOM(inst, nsMultiplexInputStream);
381 if (!inst)
382 return NS_ERROR_OUT_OF_MEMORY;
383
384 NS_ADDREF(inst);
385 nsresult rv = inst->QueryInterface(iid, result);
386 NS_RELEASE(inst);
387
388 return rv;
389}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette