VirtualBox

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

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

import

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 16.7 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 Mozilla Communicator client code, released
16 * March 31, 1998.
17 *
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998-1999
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 * Pierre Phaneuf <[email protected]>
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40/*
41 * The storage stream provides an internal buffer that can be filled by a
42 * client using a single output stream. One or more independent input streams
43 * can be created to read the data out non-destructively. The implementation
44 * uses a segmented buffer internally to avoid realloc'ing of large buffers,
45 * with the attendant performance loss and heap fragmentation.
46 */
47
48#include "nsStorageStream.h"
49#include "nsSegmentedBuffer.h"
50#include "nsCOMPtr.h"
51#include "prbit.h"
52#include "nsIInputStream.h"
53#include "nsISeekableStream.h"
54#include "prlog.h"
55#include "nsInt64.h"
56#if defined(PR_LOGGING)
57//
58// Log module for StorageStream logging...
59//
60// To enable logging (see prlog.h for full details):
61//
62// set NSPR_LOG_MODULES=StorageStreamLog:5
63// set NSPR_LOG_FILE=nspr.log
64//
65// this enables PR_LOG_DEBUG level information and places all output in
66// the file nspr.log
67//
68PRLogModuleInfo* StorageStreamLog = nsnull;
69
70#endif /* PR_LOGGING */
71
72nsStorageStream::nsStorageStream()
73 : mSegmentedBuffer(0), mSegmentSize(0), mWriteInProgress(PR_FALSE),
74 mLastSegmentNum(-1), mWriteCursor(0), mSegmentEnd(0), mLogicalLength(0)
75{
76#if defined(PR_LOGGING)
77 //
78 // Initialize the global PRLogModule for socket transport logging
79 // if necessary...
80 //
81 if (nsnull == StorageStreamLog) {
82 StorageStreamLog = PR_NewLogModule("StorageStreamLog");
83 }
84#endif /* PR_LOGGING */
85
86 PR_LOG(StorageStreamLog, PR_LOG_DEBUG,
87 ("Creating nsStorageStream [%x].\n", this));
88}
89
90nsStorageStream::~nsStorageStream()
91{
92 if (mSegmentedBuffer)
93 delete mSegmentedBuffer;
94}
95
96NS_IMPL_THREADSAFE_ISUPPORTS2(nsStorageStream,
97 nsIStorageStream,
98 nsIOutputStream)
99
100NS_IMETHODIMP
101nsStorageStream::Init(PRUint32 segmentSize, PRUint32 maxSize,
102 nsIMemory *segmentAllocator)
103{
104 mSegmentedBuffer = new nsSegmentedBuffer();
105 if (!mSegmentedBuffer)
106 return NS_ERROR_OUT_OF_MEMORY;
107
108 mSegmentSize = segmentSize;
109 mSegmentSizeLog2 = PR_FloorLog2(segmentSize);
110
111 // Segment size must be a power of two
112 if (mSegmentSize != ((PRUint32)1 << mSegmentSizeLog2))
113 return NS_ERROR_INVALID_ARG;
114
115 return mSegmentedBuffer->Init(segmentSize, maxSize, segmentAllocator);
116}
117
118NS_IMETHODIMP
119nsStorageStream::GetOutputStream(PRInt32 aStartingOffset,
120 nsIOutputStream * *aOutputStream)
121{
122 NS_ENSURE_ARG(aOutputStream);
123 if (mWriteInProgress)
124 return NS_ERROR_NOT_AVAILABLE;
125
126 nsresult rv = Seek(aStartingOffset);
127 if (NS_FAILED(rv)) return rv;
128
129 // Enlarge the last segment in the buffer so that it is the same size as
130 // all the other segments in the buffer. (It may have been realloc'ed
131 // smaller in the Close() method.)
132 if (mLastSegmentNum >= 0)
133 mSegmentedBuffer->ReallocLastSegment(mSegmentSize);
134
135 // Need to re-Seek, since realloc might have changed segment base pointer
136 rv = Seek(aStartingOffset);
137 if (NS_FAILED(rv)) return rv;
138
139 NS_ADDREF(this);
140 *aOutputStream = NS_STATIC_CAST(nsIOutputStream*, this);
141 mWriteInProgress = PR_TRUE;
142 return NS_OK;
143}
144
145NS_IMETHODIMP
146nsStorageStream::Close()
147{
148 mWriteInProgress = PR_FALSE;
149
150 PRInt32 segmentOffset = SegOffset(mLogicalLength);
151
152 // Shrink the final segment in the segmented buffer to the minimum size
153 // needed to contain the data, so as to conserve memory.
154 if (segmentOffset)
155 mSegmentedBuffer->ReallocLastSegment(segmentOffset);
156
157 mWriteCursor = 0;
158 mSegmentEnd = 0;
159
160 PR_LOG(StorageStreamLog, PR_LOG_DEBUG,
161 ("nsStorageStream [%x] Close mWriteCursor=%x mSegmentEnd=%x\n",
162 this, mWriteCursor, mSegmentEnd));
163
164 return NS_OK;
165}
166
167NS_IMETHODIMP
168nsStorageStream::Flush()
169{
170 return NS_OK;
171}
172
173NS_IMETHODIMP
174nsStorageStream::Write(const char *aBuffer, PRUint32 aCount, PRUint32 *aNumWritten)
175{
176 const char* readCursor;
177 PRUint32 count, availableInSegment, remaining;
178 nsresult rv = NS_OK;
179
180 NS_ENSURE_ARG_POINTER(aNumWritten);
181 NS_ENSURE_ARG(aBuffer);
182
183 PR_LOG(StorageStreamLog, PR_LOG_DEBUG,
184 ("nsStorageStream [%x] Write mWriteCursor=%x mSegmentEnd=%x aCount=%d\n",
185 this, mWriteCursor, mSegmentEnd, aCount));
186
187 remaining = aCount;
188 readCursor = aBuffer;
189 while (remaining) {
190 availableInSegment = mSegmentEnd - mWriteCursor;
191 if (!availableInSegment) {
192 mWriteCursor = mSegmentedBuffer->AppendNewSegment();
193 if (!mWriteCursor) {
194 mSegmentEnd = 0;
195 rv = NS_ERROR_OUT_OF_MEMORY;
196 goto out;
197 }
198 mLastSegmentNum++;
199 mSegmentEnd = mWriteCursor + mSegmentSize;
200 availableInSegment = mSegmentEnd - mWriteCursor;
201 PR_LOG(StorageStreamLog, PR_LOG_DEBUG,
202 ("nsStorageStream [%x] Write (new seg) mWriteCursor=%x mSegmentEnd=%x\n",
203 this, mWriteCursor, mSegmentEnd));
204 }
205
206 count = PR_MIN(availableInSegment, remaining);
207 memcpy(mWriteCursor, readCursor, count);
208 remaining -= count;
209 readCursor += count;
210 mWriteCursor += count;
211 PR_LOG(StorageStreamLog, PR_LOG_DEBUG,
212 ("nsStorageStream [%x] Writing mWriteCursor=%x mSegmentEnd=%x count=%d\n",
213 this, mWriteCursor, mSegmentEnd, count));
214 };
215
216 out:
217 *aNumWritten = aCount - remaining;
218 mLogicalLength += *aNumWritten;
219
220 PR_LOG(StorageStreamLog, PR_LOG_DEBUG,
221 ("nsStorageStream [%x] Wrote mWriteCursor=%x mSegmentEnd=%x numWritten=%d\n",
222 this, mWriteCursor, mSegmentEnd, *aNumWritten));
223 return rv;
224}
225
226NS_IMETHODIMP
227nsStorageStream::WriteFrom(nsIInputStream *inStr, PRUint32 count, PRUint32 *_retval)
228{
229 NS_NOTREACHED("WriteFrom");
230 return NS_ERROR_NOT_IMPLEMENTED;
231}
232
233NS_IMETHODIMP
234nsStorageStream::WriteSegments(nsReadSegmentFun reader, void * closure, PRUint32 count, PRUint32 *_retval)
235{
236 NS_NOTREACHED("WriteSegments");
237 return NS_ERROR_NOT_IMPLEMENTED;
238}
239
240NS_IMETHODIMP
241nsStorageStream::IsNonBlocking(PRBool *aNonBlocking)
242{
243 *aNonBlocking = PR_TRUE;
244 return NS_OK;
245}
246
247NS_IMETHODIMP
248nsStorageStream::GetLength(PRUint32 *aLength)
249{
250 NS_ENSURE_ARG(aLength);
251 *aLength = mLogicalLength;
252 return NS_OK;
253}
254
255// Truncate the buffer by deleting the end segments
256NS_IMETHODIMP
257nsStorageStream::SetLength(PRUint32 aLength)
258{
259 if (mWriteInProgress)
260 return NS_ERROR_NOT_AVAILABLE;
261
262 if (aLength > mLogicalLength)
263 return NS_ERROR_INVALID_ARG;
264
265 PRInt32 newLastSegmentNum = SegNum(aLength);
266 PRInt32 segmentOffset = SegOffset(aLength);
267 if (segmentOffset == 0)
268 newLastSegmentNum--;
269
270 while (newLastSegmentNum < mLastSegmentNum) {
271 mSegmentedBuffer->DeleteLastSegment();
272 mLastSegmentNum--;
273 }
274
275 mLogicalLength = aLength;
276 return NS_OK;
277}
278
279NS_IMETHODIMP
280nsStorageStream::GetWriteInProgress(PRBool *aWriteInProgress)
281{
282 NS_ENSURE_ARG(aWriteInProgress);
283
284 *aWriteInProgress = mWriteInProgress;
285 return NS_OK;
286}
287
288NS_METHOD
289nsStorageStream::Seek(PRInt32 aPosition)
290{
291 // An argument of -1 means "seek to end of stream"
292 if (aPosition == -1)
293 aPosition = mLogicalLength;
294
295 // Seeking beyond the buffer end is illegal
296 if ((PRUint32)aPosition > mLogicalLength)
297 return NS_ERROR_INVALID_ARG;
298
299 // Seeking backwards in the write stream results in truncation
300 SetLength(aPosition);
301
302 // Special handling for seek to start-of-buffer
303 if (aPosition == 0) {
304 mWriteCursor = 0;
305 mSegmentEnd = 0;
306 PR_LOG(StorageStreamLog, PR_LOG_DEBUG,
307 ("nsStorageStream [%x] Seek mWriteCursor=%x mSegmentEnd=%x\n",
308 this, mWriteCursor, mSegmentEnd));
309 return NS_OK;
310 }
311
312 // Segment may have changed, so reset pointers
313 mWriteCursor = mSegmentedBuffer->GetSegment(mLastSegmentNum);
314 NS_ASSERTION(mWriteCursor, "null mWriteCursor");
315 mSegmentEnd = mWriteCursor + mSegmentSize;
316 PRInt32 segmentOffset = SegOffset(aPosition);
317 mWriteCursor += segmentOffset;
318
319 PR_LOG(StorageStreamLog, PR_LOG_DEBUG,
320 ("nsStorageStream [%x] Seek mWriteCursor=%x mSegmentEnd=%x\n",
321 this, mWriteCursor, mSegmentEnd));
322 return NS_OK;
323}
324
325////////////////////////////////////////////////////////////////////////////////
326
327// There can be many nsStorageInputStreams for a single nsStorageStream
328class nsStorageInputStream : public nsIInputStream
329 , public nsISeekableStream
330{
331public:
332 nsStorageInputStream(nsStorageStream *aStorageStream, PRUint32 aSegmentSize)
333 : mStorageStream(aStorageStream), mReadCursor(0),
334 mSegmentEnd(0), mSegmentNum(0),
335 mSegmentSize(aSegmentSize), mLogicalCursor(0)
336 {
337 NS_ADDREF(mStorageStream);
338 }
339
340 NS_DECL_ISUPPORTS
341 NS_DECL_NSIINPUTSTREAM
342 NS_DECL_NSISEEKABLESTREAM
343
344private:
345 ~nsStorageInputStream()
346 {
347 NS_IF_RELEASE(mStorageStream);
348 }
349
350protected:
351 NS_METHOD Seek(PRUint32 aPosition);
352
353 friend class nsStorageStream;
354
355private:
356 nsStorageStream* mStorageStream;
357 const char* mReadCursor; // Next memory location to read byte, or NULL
358 const char* mSegmentEnd; // One byte past end of current buffer segment
359 PRUint32 mSegmentNum; // Segment number containing read cursor
360 PRUint32 mSegmentSize; // All segments, except the last, are of this size
361 PRUint32 mLogicalCursor; // Logical offset into stream
362
363 PRUint32 SegNum(PRUint32 aPosition) {return aPosition >> mStorageStream->mSegmentSizeLog2;}
364 PRUint32 SegOffset(PRUint32 aPosition) {return aPosition & (mSegmentSize - 1);}
365};
366
367NS_IMPL_THREADSAFE_ISUPPORTS2(nsStorageInputStream,
368 nsIInputStream,
369 nsISeekableStream)
370
371NS_IMETHODIMP
372nsStorageStream::NewInputStream(PRInt32 aStartingOffset, nsIInputStream* *aInputStream)
373{
374 nsStorageInputStream *inputStream = new nsStorageInputStream(this, mSegmentSize);
375 if (!inputStream)
376 return NS_ERROR_OUT_OF_MEMORY;
377
378 NS_ADDREF(inputStream);
379
380 nsresult rv = inputStream->Seek(aStartingOffset);
381 if (NS_FAILED(rv)) {
382 NS_RELEASE(inputStream);
383 return rv;
384 }
385
386 *aInputStream = inputStream;
387 return NS_OK;
388}
389
390NS_IMETHODIMP
391nsStorageInputStream::Close()
392{
393 return NS_OK;
394}
395
396NS_IMETHODIMP
397nsStorageInputStream::Available(PRUint32 *aAvailable)
398{
399 *aAvailable = mStorageStream->mLogicalLength - mLogicalCursor;
400 return NS_OK;
401}
402
403NS_IMETHODIMP
404nsStorageInputStream::Read(char* aBuffer, PRUint32 aCount, PRUint32 *aNumRead)
405{
406 char* writeCursor;
407 PRUint32 count, availableInSegment, remainingCapacity;
408
409 remainingCapacity = aCount;
410 writeCursor = aBuffer;
411 while (remainingCapacity) {
412 availableInSegment = mSegmentEnd - mReadCursor;
413 if (!availableInSegment) {
414 PRUint32 available = mStorageStream->mLogicalLength - mLogicalCursor;
415 if (!available)
416 goto out;
417
418 mReadCursor = mStorageStream->mSegmentedBuffer->GetSegment(++mSegmentNum);
419 mSegmentEnd = mReadCursor + PR_MIN(mSegmentSize, available);
420 }
421
422 count = PR_MIN(availableInSegment, remainingCapacity);
423 memcpy(writeCursor, mReadCursor, count);
424 remainingCapacity -= count;
425 mReadCursor += count;
426 writeCursor += count;
427 mLogicalCursor += count;
428 };
429
430 out:
431 *aNumRead = aCount - remainingCapacity;
432
433 PRBool isWriteInProgress = PR_FALSE;
434 if (NS_FAILED(mStorageStream->GetWriteInProgress(&isWriteInProgress)))
435 isWriteInProgress = PR_FALSE;
436
437 if (*aNumRead == 0 && isWriteInProgress)
438 return NS_BASE_STREAM_WOULD_BLOCK;
439 else
440 return NS_OK;
441}
442
443NS_IMETHODIMP
444nsStorageInputStream::ReadSegments(nsWriteSegmentFun writer, void * closure, PRUint32 aCount, PRUint32 *aNumRead)
445{
446 PRUint32 count, availableInSegment, remainingCapacity, bytesConsumed;
447 nsresult rv;
448
449 remainingCapacity = aCount;
450 while (remainingCapacity) {
451 availableInSegment = mSegmentEnd - mReadCursor;
452 if (!availableInSegment) {
453 PRUint32 available = mStorageStream->mLogicalLength - mLogicalCursor;
454 if (!available)
455 goto out;
456
457 mReadCursor = mStorageStream->mSegmentedBuffer->GetSegment(++mSegmentNum);
458 mSegmentEnd = mReadCursor + PR_MIN(mSegmentSize, available);
459 availableInSegment = mSegmentEnd - mReadCursor;
460 }
461
462 count = PR_MIN(availableInSegment, remainingCapacity);
463 rv = writer(this, closure, mReadCursor, mLogicalCursor, count, &bytesConsumed);
464 if (NS_FAILED(rv) || (bytesConsumed == 0))
465 break;
466 remainingCapacity -= bytesConsumed;
467 mReadCursor += bytesConsumed;
468 mLogicalCursor += bytesConsumed;
469 };
470
471 out:
472 *aNumRead = aCount - remainingCapacity;
473
474 PRBool isWriteInProgress = PR_FALSE;
475 if (NS_FAILED(mStorageStream->GetWriteInProgress(&isWriteInProgress)))
476 isWriteInProgress = PR_FALSE;
477
478 if (*aNumRead == 0 && isWriteInProgress)
479 return NS_BASE_STREAM_WOULD_BLOCK;
480 else
481 return NS_OK;
482}
483
484NS_IMETHODIMP
485nsStorageInputStream::IsNonBlocking(PRBool *aNonBlocking)
486{
487 *aNonBlocking = PR_TRUE;
488 return NS_OK;
489}
490
491NS_IMETHODIMP
492nsStorageInputStream::Seek(PRInt32 aWhence, PRInt64 aOffset)
493{
494 nsInt64 pos = aOffset;
495
496 switch (aWhence) {
497 case NS_SEEK_SET:
498 break;
499 case NS_SEEK_CUR:
500 pos += mLogicalCursor;
501 break;
502 case NS_SEEK_END:
503 pos += mStorageStream->mLogicalLength;
504 break;
505 default:
506 NS_NOTREACHED("unexpected whence value");
507 return NS_ERROR_UNEXPECTED;
508 }
509 nsInt64 logicalCursor(mLogicalCursor);
510 if (pos == logicalCursor)
511 return NS_OK;
512
513 return Seek(pos);
514}
515
516NS_IMETHODIMP
517nsStorageInputStream::Tell(PRInt64 *aResult)
518{
519 LL_UI2L(*aResult, mLogicalCursor);
520 return NS_OK;
521}
522
523NS_IMETHODIMP
524nsStorageInputStream::SetEOF()
525{
526 NS_NOTREACHED("nsStorageInputStream::SetEOF");
527 return NS_ERROR_NOT_IMPLEMENTED;
528}
529
530NS_METHOD
531nsStorageInputStream::Seek(PRUint32 aPosition)
532{
533 PRUint32 length = mStorageStream->mLogicalLength;
534 if (aPosition >= length)
535 return NS_ERROR_INVALID_ARG;
536
537 mSegmentNum = SegNum(aPosition);
538 PRUint32 segmentOffset = SegOffset(aPosition);
539 mReadCursor = mStorageStream->mSegmentedBuffer->GetSegment(mSegmentNum) +
540 segmentOffset;
541 PRUint32 available = length - aPosition;
542 mSegmentEnd = mReadCursor + PR_MIN(mSegmentSize - segmentOffset, available);
543 mLogicalCursor = aPosition;
544 return NS_OK;
545}
546
547NS_COM nsresult
548NS_NewStorageStream(PRUint32 segmentSize, PRUint32 maxSize, nsIStorageStream **result)
549{
550 NS_ENSURE_ARG(result);
551
552 nsStorageStream* storageStream = new nsStorageStream();
553 if (!storageStream) return NS_ERROR_OUT_OF_MEMORY;
554
555 NS_ADDREF(storageStream);
556 nsresult rv = storageStream->Init(segmentSize, maxSize, nsnull);
557 if (NS_FAILED(rv)) {
558 NS_RELEASE(storageStream);
559 return rv;
560 }
561 *result = storageStream;
562 return NS_OK;
563}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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