2006-01-02 02:30:32 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim:set ts=4 sts=4 sw=4 cin et: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-11-16 19:14:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The storage stream provides an internal buffer that can be filled by a
|
|
|
|
* client using a single output stream. One or more independent input streams
|
|
|
|
* can be created to read the data out non-destructively. The implementation
|
|
|
|
* uses a segmented buffer internally to avoid realloc'ing of large buffers,
|
|
|
|
* with the attendant performance loss and heap fragmentation.
|
|
|
|
*/
|
|
|
|
|
2011-06-02 12:56:50 +00:00
|
|
|
#include "nsAlgorithm.h"
|
1999-11-16 19:14:08 +00:00
|
|
|
#include "nsStorageStream.h"
|
|
|
|
#include "nsSegmentedBuffer.h"
|
2006-01-02 02:30:32 +00:00
|
|
|
#include "nsStreamUtils.h"
|
1999-11-16 19:14:08 +00:00
|
|
|
#include "nsCOMPtr.h"
|
2000-08-22 07:03:33 +00:00
|
|
|
#include "nsIInputStream.h"
|
2002-01-25 02:13:00 +00:00
|
|
|
#include "nsISeekableStream.h"
|
2000-01-21 23:16:35 +00:00
|
|
|
#include "prlog.h"
|
2012-06-05 23:51:58 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2012-10-26 13:32:10 +00:00
|
|
|
#include "mozilla/Likely.h"
|
2013-11-13 00:22:38 +00:00
|
|
|
#include "mozilla/MathAlgorithms.h"
|
2006-01-02 02:30:32 +00:00
|
|
|
|
2000-01-21 23:16:35 +00:00
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
//
|
|
|
|
// Log module for StorageStream logging...
|
|
|
|
//
|
|
|
|
// To enable logging (see prlog.h for full details):
|
|
|
|
//
|
|
|
|
// set NSPR_LOG_MODULES=StorageStreamLog:5
|
|
|
|
// set NSPR_LOG_FILE=nspr.log
|
|
|
|
//
|
|
|
|
// this enables PR_LOG_DEBUG level information and places all output in
|
|
|
|
// the file nspr.log
|
|
|
|
//
|
2012-10-29 23:32:10 +00:00
|
|
|
static PRLogModuleInfo*
|
|
|
|
GetStorageStreamLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo *sLog;
|
|
|
|
if (!sLog)
|
|
|
|
sLog = PR_NewLogModule("nsStorageStream");
|
|
|
|
return sLog;
|
|
|
|
}
|
2006-01-02 02:30:32 +00:00
|
|
|
#endif
|
2013-11-19 02:34:00 +00:00
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif
|
2012-10-29 23:32:10 +00:00
|
|
|
#define LOG(args) PR_LOG(GetStorageStreamLog(), PR_LOG_DEBUG, args)
|
2000-01-21 23:16:35 +00:00
|
|
|
|
1999-11-16 19:14:08 +00:00
|
|
|
nsStorageStream::nsStorageStream()
|
2011-10-17 14:59:28 +00:00
|
|
|
: mSegmentedBuffer(0), mSegmentSize(0), mWriteInProgress(false),
|
1999-11-18 06:59:28 +00:00
|
|
|
mLastSegmentNum(-1), mWriteCursor(0), mSegmentEnd(0), mLogicalLength(0)
|
1999-11-16 19:14:08 +00:00
|
|
|
{
|
2006-01-02 02:30:32 +00:00
|
|
|
LOG(("Creating nsStorageStream [%p].\n", this));
|
1999-11-16 19:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsStorageStream::~nsStorageStream()
|
|
|
|
{
|
2011-05-17 14:01:36 +00:00
|
|
|
delete mSegmentedBuffer;
|
1999-11-16 19:14:08 +00:00
|
|
|
}
|
|
|
|
|
2013-07-19 02:31:26 +00:00
|
|
|
NS_IMPL_ISUPPORTS2(nsStorageStream,
|
|
|
|
nsIStorageStream,
|
|
|
|
nsIOutputStream)
|
1999-11-16 19:14:08 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsStorageStream::Init(uint32_t segmentSize, uint32_t maxSize,
|
2000-06-03 09:46:12 +00:00
|
|
|
nsIMemory *segmentAllocator)
|
1999-11-16 19:14:08 +00:00
|
|
|
{
|
|
|
|
mSegmentedBuffer = new nsSegmentedBuffer();
|
|
|
|
if (!mSegmentedBuffer)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
mSegmentSize = segmentSize;
|
2013-11-13 00:22:38 +00:00
|
|
|
mSegmentSizeLog2 = mozilla::FloorLog2(segmentSize);
|
1999-11-16 19:14:08 +00:00
|
|
|
|
|
|
|
// Segment size must be a power of two
|
2012-08-22 15:56:38 +00:00
|
|
|
if (mSegmentSize != ((uint32_t)1 << mSegmentSizeLog2))
|
1999-11-16 19:14:08 +00:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
return mSegmentedBuffer->Init(segmentSize, maxSize, segmentAllocator);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsStorageStream::GetOutputStream(int32_t aStartingOffset,
|
1999-11-16 19:14:08 +00:00
|
|
|
nsIOutputStream * *aOutputStream)
|
|
|
|
{
|
2013-11-19 21:27:37 +00:00
|
|
|
if (NS_WARN_IF(!aOutputStream))
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
if (NS_WARN_IF(!mSegmentedBuffer))
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2005-04-20 15:37:38 +00:00
|
|
|
|
1999-11-16 19:14:08 +00:00
|
|
|
if (mWriteInProgress)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
nsresult rv = Seek(aStartingOffset);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Enlarge the last segment in the buffer so that it is the same size as
|
|
|
|
// all the other segments in the buffer. (It may have been realloc'ed
|
|
|
|
// smaller in the Close() method.)
|
|
|
|
if (mLastSegmentNum >= 0)
|
2011-11-08 07:41:54 +00:00
|
|
|
if (mSegmentedBuffer->ReallocLastSegment(mSegmentSize)) {
|
|
|
|
// Need to re-Seek, since realloc changed segment base pointer
|
|
|
|
rv = Seek(aStartingOffset);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
1999-12-08 06:28:44 +00:00
|
|
|
|
1999-11-16 19:14:08 +00:00
|
|
|
NS_ADDREF(this);
|
2007-07-08 07:08:04 +00:00
|
|
|
*aOutputStream = static_cast<nsIOutputStream*>(this);
|
2011-10-17 14:59:28 +00:00
|
|
|
mWriteInProgress = true;
|
1999-11-16 19:14:08 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStorageStream::Close()
|
|
|
|
{
|
2013-11-19 21:27:37 +00:00
|
|
|
if (NS_WARN_IF(!mSegmentedBuffer))
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2005-04-20 15:37:38 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mWriteInProgress = false;
|
1999-11-16 19:14:08 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t segmentOffset = SegOffset(mLogicalLength);
|
1999-11-16 19:14:08 +00:00
|
|
|
|
|
|
|
// Shrink the final segment in the segmented buffer to the minimum size
|
|
|
|
// needed to contain the data, so as to conserve memory.
|
|
|
|
if (segmentOffset)
|
|
|
|
mSegmentedBuffer->ReallocLastSegment(segmentOffset);
|
|
|
|
|
|
|
|
mWriteCursor = 0;
|
|
|
|
mSegmentEnd = 0;
|
|
|
|
|
2006-01-02 02:30:32 +00:00
|
|
|
LOG(("nsStorageStream [%p] Close mWriteCursor=%x mSegmentEnd=%x\n",
|
|
|
|
this, mWriteCursor, mSegmentEnd));
|
2000-01-21 23:16:35 +00:00
|
|
|
|
1999-11-16 19:14:08 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStorageStream::Flush()
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsStorageStream::Write(const char *aBuffer, uint32_t aCount, uint32_t *aNumWritten)
|
1999-11-16 19:14:08 +00:00
|
|
|
{
|
2013-11-19 21:27:37 +00:00
|
|
|
if (NS_WARN_IF(!aNumWritten) || NS_WARN_IF(!aBuffer))
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
if (NS_WARN_IF(!mSegmentedBuffer))
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2005-04-20 15:37:38 +00:00
|
|
|
|
1999-11-16 19:14:08 +00:00
|
|
|
const char* readCursor;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t count, availableInSegment, remaining;
|
1999-11-16 19:14:08 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2006-01-02 02:30:32 +00:00
|
|
|
LOG(("nsStorageStream [%p] Write mWriteCursor=%x mSegmentEnd=%x aCount=%d\n",
|
|
|
|
this, mWriteCursor, mSegmentEnd, aCount));
|
2000-01-21 23:16:35 +00:00
|
|
|
|
1999-11-16 19:14:08 +00:00
|
|
|
remaining = aCount;
|
|
|
|
readCursor = aBuffer;
|
2007-03-09 12:28:55 +00:00
|
|
|
// If no segments have been created yet, create one even if we don't have
|
|
|
|
// to write any data; this enables creating an input stream which reads from
|
|
|
|
// the very end of the data for any amount of data in the stream (i.e.
|
|
|
|
// this stream contains N bytes of data and newInputStream(N) is called),
|
|
|
|
// even for N=0 (with the caveat that we require .write("", 0) be called to
|
|
|
|
// initialize internal buffers).
|
2011-09-29 06:19:26 +00:00
|
|
|
bool firstTime = mSegmentedBuffer->GetSegmentCount() == 0;
|
2012-10-26 13:32:10 +00:00
|
|
|
while (remaining || MOZ_UNLIKELY(firstTime)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
firstTime = false;
|
1999-11-16 19:14:08 +00:00
|
|
|
availableInSegment = mSegmentEnd - mWriteCursor;
|
|
|
|
if (!availableInSegment) {
|
|
|
|
mWriteCursor = mSegmentedBuffer->AppendNewSegment();
|
|
|
|
if (!mWriteCursor) {
|
2000-01-21 23:16:35 +00:00
|
|
|
mSegmentEnd = 0;
|
1999-11-16 19:14:08 +00:00
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
goto out;
|
|
|
|
}
|
2000-01-21 23:16:35 +00:00
|
|
|
mLastSegmentNum++;
|
1999-11-16 19:14:08 +00:00
|
|
|
mSegmentEnd = mWriteCursor + mSegmentSize;
|
|
|
|
availableInSegment = mSegmentEnd - mWriteCursor;
|
2006-01-02 02:30:32 +00:00
|
|
|
LOG(("nsStorageStream [%p] Write (new seg) mWriteCursor=%x mSegmentEnd=%x\n",
|
|
|
|
this, mWriteCursor, mSegmentEnd));
|
1999-11-16 19:14:08 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 12:22:03 +00:00
|
|
|
count = XPCOM_MIN(availableInSegment, remaining);
|
1999-11-16 19:14:08 +00:00
|
|
|
memcpy(mWriteCursor, readCursor, count);
|
|
|
|
remaining -= count;
|
|
|
|
readCursor += count;
|
|
|
|
mWriteCursor += count;
|
2006-01-02 02:30:32 +00:00
|
|
|
LOG(("nsStorageStream [%p] Writing mWriteCursor=%x mSegmentEnd=%x count=%d\n",
|
|
|
|
this, mWriteCursor, mSegmentEnd, count));
|
1999-11-16 19:14:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
out:
|
|
|
|
*aNumWritten = aCount - remaining;
|
|
|
|
mLogicalLength += *aNumWritten;
|
|
|
|
|
2006-01-02 02:30:32 +00:00
|
|
|
LOG(("nsStorageStream [%p] Wrote mWriteCursor=%x mSegmentEnd=%x numWritten=%d\n",
|
|
|
|
this, mWriteCursor, mSegmentEnd, *aNumWritten));
|
1999-11-16 19:14:08 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2000-08-22 07:03:33 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsStorageStream::WriteFrom(nsIInputStream *inStr, uint32_t count, uint32_t *_retval)
|
2000-08-22 07:03:33 +00:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsStorageStream::WriteSegments(nsReadSegmentFun reader, void * closure, uint32_t count, uint32_t *_retval)
|
2000-08-22 07:03:33 +00:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsStorageStream::IsNonBlocking(bool *aNonBlocking)
|
2000-08-22 07:03:33 +00:00
|
|
|
{
|
2011-11-19 11:53:57 +00:00
|
|
|
*aNonBlocking = false;
|
2002-03-12 00:59:06 +00:00
|
|
|
return NS_OK;
|
2000-08-22 07:03:33 +00:00
|
|
|
}
|
|
|
|
|
1999-11-16 19:14:08 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsStorageStream::GetLength(uint32_t *aLength)
|
1999-11-16 19:14:08 +00:00
|
|
|
{
|
|
|
|
*aLength = mLogicalLength;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate the buffer by deleting the end segments
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsStorageStream::SetLength(uint32_t aLength)
|
1999-11-16 19:14:08 +00:00
|
|
|
{
|
2013-11-19 21:27:37 +00:00
|
|
|
if (NS_WARN_IF(!mSegmentedBuffer))
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2005-04-20 15:37:38 +00:00
|
|
|
|
1999-11-16 19:14:08 +00:00
|
|
|
if (mWriteInProgress)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
if (aLength > mLogicalLength)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t newLastSegmentNum = SegNum(aLength);
|
|
|
|
int32_t segmentOffset = SegOffset(aLength);
|
1999-11-16 19:14:08 +00:00
|
|
|
if (segmentOffset == 0)
|
|
|
|
newLastSegmentNum--;
|
|
|
|
|
|
|
|
while (newLastSegmentNum < mLastSegmentNum) {
|
|
|
|
mSegmentedBuffer->DeleteLastSegment();
|
|
|
|
mLastSegmentNum--;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLogicalLength = aLength;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsStorageStream::GetWriteInProgress(bool *aWriteInProgress)
|
1999-11-16 19:14:08 +00:00
|
|
|
{
|
|
|
|
*aWriteInProgress = mWriteInProgress;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
2012-08-22 15:56:38 +00:00
|
|
|
nsStorageStream::Seek(int32_t aPosition)
|
1999-11-16 19:14:08 +00:00
|
|
|
{
|
2013-11-19 21:27:37 +00:00
|
|
|
if (NS_WARN_IF(!mSegmentedBuffer))
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2005-04-20 15:37:38 +00:00
|
|
|
|
1999-11-16 19:14:08 +00:00
|
|
|
// An argument of -1 means "seek to end of stream"
|
|
|
|
if (aPosition == -1)
|
|
|
|
aPosition = mLogicalLength;
|
|
|
|
|
|
|
|
// Seeking beyond the buffer end is illegal
|
2012-08-22 15:56:38 +00:00
|
|
|
if ((uint32_t)aPosition > mLogicalLength)
|
1999-11-16 19:14:08 +00:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
// Seeking backwards in the write stream results in truncation
|
|
|
|
SetLength(aPosition);
|
|
|
|
|
|
|
|
// Special handling for seek to start-of-buffer
|
|
|
|
if (aPosition == 0) {
|
|
|
|
mWriteCursor = 0;
|
|
|
|
mSegmentEnd = 0;
|
2006-01-02 02:30:32 +00:00
|
|
|
LOG(("nsStorageStream [%p] Seek mWriteCursor=%x mSegmentEnd=%x\n",
|
|
|
|
this, mWriteCursor, mSegmentEnd));
|
1999-11-16 19:14:08 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Segment may have changed, so reset pointers
|
|
|
|
mWriteCursor = mSegmentedBuffer->GetSegment(mLastSegmentNum);
|
2000-01-21 23:16:35 +00:00
|
|
|
NS_ASSERTION(mWriteCursor, "null mWriteCursor");
|
1999-11-16 19:14:08 +00:00
|
|
|
mSegmentEnd = mWriteCursor + mSegmentSize;
|
2004-09-29 16:45:22 +00:00
|
|
|
|
|
|
|
// Adjust write cursor for current segment offset. This test is necessary
|
|
|
|
// because SegNum may reference the next-to-be-allocated segment, in which
|
|
|
|
// case we need to be pointing at the end of the last segment.
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t segmentOffset = SegOffset(aPosition);
|
|
|
|
if (segmentOffset == 0 && (SegNum(aPosition) > (uint32_t) mLastSegmentNum))
|
2004-09-29 16:45:22 +00:00
|
|
|
mWriteCursor = mSegmentEnd;
|
|
|
|
else
|
|
|
|
mWriteCursor += segmentOffset;
|
1999-11-16 19:14:08 +00:00
|
|
|
|
2006-01-02 02:30:32 +00:00
|
|
|
LOG(("nsStorageStream [%p] Seek mWriteCursor=%x mSegmentEnd=%x\n",
|
|
|
|
this, mWriteCursor, mSegmentEnd));
|
1999-11-16 19:14:08 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-01-21 23:16:35 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-11-16 19:14:08 +00:00
|
|
|
// There can be many nsStorageInputStreams for a single nsStorageStream
|
2012-06-05 23:51:58 +00:00
|
|
|
class nsStorageInputStream MOZ_FINAL : public nsIInputStream
|
|
|
|
, public nsISeekableStream
|
1999-11-16 19:14:08 +00:00
|
|
|
{
|
|
|
|
public:
|
2012-08-22 15:56:38 +00:00
|
|
|
nsStorageInputStream(nsStorageStream *aStorageStream, uint32_t aSegmentSize)
|
1999-11-16 19:14:08 +00:00
|
|
|
: mStorageStream(aStorageStream), mReadCursor(0),
|
1999-11-18 03:38:32 +00:00
|
|
|
mSegmentEnd(0), mSegmentNum(0),
|
2006-01-02 02:30:32 +00:00
|
|
|
mSegmentSize(aSegmentSize), mLogicalCursor(0),
|
|
|
|
mStatus(NS_OK)
|
1999-11-16 19:14:08 +00:00
|
|
|
{
|
|
|
|
NS_ADDREF(mStorageStream);
|
|
|
|
}
|
|
|
|
|
2013-07-19 02:31:26 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
1999-11-16 19:14:08 +00:00
|
|
|
NS_DECL_NSIINPUTSTREAM
|
2002-01-25 02:13:00 +00:00
|
|
|
NS_DECL_NSISEEKABLESTREAM
|
1999-11-16 19:14:08 +00:00
|
|
|
|
2004-01-15 06:14:18 +00:00
|
|
|
private:
|
|
|
|
~nsStorageInputStream()
|
|
|
|
{
|
|
|
|
NS_IF_RELEASE(mStorageStream);
|
|
|
|
}
|
|
|
|
|
1999-11-16 19:14:08 +00:00
|
|
|
protected:
|
2012-08-22 15:56:38 +00:00
|
|
|
NS_METHOD Seek(uint32_t aPosition);
|
1999-11-16 19:14:08 +00:00
|
|
|
|
|
|
|
friend class nsStorageStream;
|
|
|
|
|
|
|
|
private:
|
1999-11-18 03:38:32 +00:00
|
|
|
nsStorageStream* mStorageStream;
|
2013-11-25 19:02:47 +00:00
|
|
|
uint32_t mReadCursor; // Next memory location to read byte, or 0
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mSegmentEnd; // One byte past end of current buffer segment
|
|
|
|
uint32_t mSegmentNum; // Segment number containing read cursor
|
|
|
|
uint32_t mSegmentSize; // All segments, except the last, are of this size
|
|
|
|
uint32_t mLogicalCursor; // Logical offset into stream
|
2006-01-02 02:30:32 +00:00
|
|
|
nsresult mStatus;
|
1999-11-16 19:14:08 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t SegNum(uint32_t aPosition) {return aPosition >> mStorageStream->mSegmentSizeLog2;}
|
|
|
|
uint32_t SegOffset(uint32_t aPosition) {return aPosition & (mSegmentSize - 1);}
|
1999-11-16 19:14:08 +00:00
|
|
|
};
|
|
|
|
|
2013-07-19 02:31:26 +00:00
|
|
|
NS_IMPL_ISUPPORTS2(nsStorageInputStream,
|
|
|
|
nsIInputStream,
|
|
|
|
nsISeekableStream)
|
1999-11-16 19:14:08 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsStorageStream::NewInputStream(int32_t aStartingOffset, nsIInputStream* *aInputStream)
|
1999-11-16 19:14:08 +00:00
|
|
|
{
|
2013-11-19 21:27:37 +00:00
|
|
|
if (NS_WARN_IF(!mSegmentedBuffer))
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2007-03-09 12:28:55 +00:00
|
|
|
|
1999-11-16 19:14:08 +00:00
|
|
|
nsStorageInputStream *inputStream = new nsStorageInputStream(this, mSegmentSize);
|
|
|
|
if (!inputStream)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(inputStream);
|
|
|
|
|
2002-03-19 14:11:06 +00:00
|
|
|
nsresult rv = inputStream->Seek(aStartingOffset);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(inputStream);
|
|
|
|
return rv;
|
1999-11-16 19:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*aInputStream = inputStream;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStorageInputStream::Close()
|
|
|
|
{
|
2006-01-02 02:30:32 +00:00
|
|
|
mStatus = NS_BASE_STREAM_CLOSED;
|
1999-11-16 19:14:08 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsStorageInputStream::Available(uint64_t *aAvailable)
|
1999-11-16 19:14:08 +00:00
|
|
|
{
|
2006-01-02 02:30:32 +00:00
|
|
|
if (NS_FAILED(mStatus))
|
|
|
|
return mStatus;
|
|
|
|
|
1999-11-16 19:14:08 +00:00
|
|
|
*aAvailable = mStorageStream->mLogicalLength - mLogicalCursor;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsStorageInputStream::Read(char* aBuffer, uint32_t aCount, uint32_t *aNumRead)
|
1999-11-16 19:14:08 +00:00
|
|
|
{
|
2006-01-02 02:30:32 +00:00
|
|
|
return ReadSegments(NS_CopySegmentToBuffer, aBuffer, aCount, aNumRead);
|
1999-11-16 19:14:08 +00:00
|
|
|
}
|
|
|
|
|
2000-08-22 07:03:33 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsStorageInputStream::ReadSegments(nsWriteSegmentFun writer, void * closure, uint32_t aCount, uint32_t *aNumRead)
|
2000-08-22 07:03:33 +00:00
|
|
|
{
|
2007-03-09 12:28:55 +00:00
|
|
|
*aNumRead = 0;
|
|
|
|
if (mStatus == NS_BASE_STREAM_CLOSED)
|
2006-01-02 02:30:32 +00:00
|
|
|
return NS_OK;
|
|
|
|
if (NS_FAILED(mStatus))
|
|
|
|
return mStatus;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t count, availableInSegment, remainingCapacity, bytesConsumed;
|
2003-01-09 21:37:30 +00:00
|
|
|
nsresult rv;
|
2000-12-13 23:11:32 +00:00
|
|
|
|
|
|
|
remainingCapacity = aCount;
|
|
|
|
while (remainingCapacity) {
|
|
|
|
availableInSegment = mSegmentEnd - mReadCursor;
|
|
|
|
if (!availableInSegment) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t available = mStorageStream->mLogicalLength - mLogicalCursor;
|
2000-12-13 23:11:32 +00:00
|
|
|
if (!available)
|
|
|
|
goto out;
|
2002-02-07 14:19:32 +00:00
|
|
|
|
2011-09-08 07:12:42 +00:00
|
|
|
mSegmentNum++;
|
|
|
|
mReadCursor = 0;
|
2013-01-15 12:22:03 +00:00
|
|
|
mSegmentEnd = XPCOM_MIN(mSegmentSize, available);
|
2011-09-08 07:12:42 +00:00
|
|
|
availableInSegment = mSegmentEnd;
|
2000-12-13 23:11:32 +00:00
|
|
|
}
|
2011-09-08 07:12:42 +00:00
|
|
|
const char *cur = mStorageStream->mSegmentedBuffer->GetSegment(mSegmentNum);
|
2000-12-13 23:11:32 +00:00
|
|
|
|
2013-01-15 12:22:03 +00:00
|
|
|
count = XPCOM_MIN(availableInSegment, remainingCapacity);
|
2011-09-08 07:12:42 +00:00
|
|
|
rv = writer(this, closure, cur + mReadCursor, aCount - remainingCapacity,
|
2006-01-02 02:30:32 +00:00
|
|
|
count, &bytesConsumed);
|
2003-01-09 21:37:30 +00:00
|
|
|
if (NS_FAILED(rv) || (bytesConsumed == 0))
|
|
|
|
break;
|
2000-12-13 23:11:32 +00:00
|
|
|
remainingCapacity -= bytesConsumed;
|
|
|
|
mReadCursor += bytesConsumed;
|
|
|
|
mLogicalCursor += bytesConsumed;
|
|
|
|
};
|
|
|
|
|
|
|
|
out:
|
|
|
|
*aNumRead = aCount - remainingCapacity;
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool isWriteInProgress = false;
|
2002-02-25 18:49:16 +00:00
|
|
|
if (NS_FAILED(mStorageStream->GetWriteInProgress(&isWriteInProgress)))
|
2011-10-17 14:59:28 +00:00
|
|
|
isWriteInProgress = false;
|
2002-02-25 18:49:16 +00:00
|
|
|
|
|
|
|
if (*aNumRead == 0 && isWriteInProgress)
|
2006-01-02 02:30:32 +00:00
|
|
|
return NS_BASE_STREAM_WOULD_BLOCK;
|
|
|
|
|
|
|
|
return NS_OK;
|
2000-08-22 07:03:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsStorageInputStream::IsNonBlocking(bool *aNonBlocking)
|
2000-08-22 07:03:33 +00:00
|
|
|
{
|
2006-01-02 02:30:32 +00:00
|
|
|
// TODO: This class should implement nsIAsyncInputStream so that callers
|
|
|
|
// have some way of dealing with NS_BASE_STREAM_WOULD_BLOCK errors.
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
*aNonBlocking = true;
|
2002-03-12 00:59:06 +00:00
|
|
|
return NS_OK;
|
2000-08-22 07:03:33 +00:00
|
|
|
}
|
|
|
|
|
2002-01-25 02:13:00 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsStorageInputStream::Seek(int32_t aWhence, int64_t aOffset)
|
2002-01-25 02:13:00 +00:00
|
|
|
{
|
2006-01-02 02:30:32 +00:00
|
|
|
if (NS_FAILED(mStatus))
|
|
|
|
return mStatus;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t pos = aOffset;
|
2002-01-25 02:13:00 +00:00
|
|
|
|
|
|
|
switch (aWhence) {
|
|
|
|
case NS_SEEK_SET:
|
|
|
|
break;
|
|
|
|
case NS_SEEK_CUR:
|
2004-04-13 14:37:53 +00:00
|
|
|
pos += mLogicalCursor;
|
2002-01-25 02:13:00 +00:00
|
|
|
break;
|
|
|
|
case NS_SEEK_END:
|
2004-04-13 14:37:53 +00:00
|
|
|
pos += mStorageStream->mLogicalLength;
|
2002-01-25 02:13:00 +00:00
|
|
|
break;
|
2002-01-25 10:47:24 +00:00
|
|
|
default:
|
|
|
|
NS_NOTREACHED("unexpected whence value");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2002-01-25 02:13:00 +00:00
|
|
|
}
|
2012-08-22 15:56:38 +00:00
|
|
|
if (pos == int64_t(mLogicalCursor))
|
2003-01-18 02:15:14 +00:00
|
|
|
return NS_OK;
|
|
|
|
|
2002-01-25 02:13:00 +00:00
|
|
|
return Seek(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsStorageInputStream::Tell(int64_t *aResult)
|
2002-01-25 02:13:00 +00:00
|
|
|
{
|
2006-01-02 02:30:32 +00:00
|
|
|
if (NS_FAILED(mStatus))
|
|
|
|
return mStatus;
|
|
|
|
|
2012-10-25 23:25:57 +00:00
|
|
|
*aResult = mLogicalCursor;
|
2002-01-25 02:13:00 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStorageInputStream::SetEOF()
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("nsStorageInputStream::SetEOF");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
1999-11-16 19:14:08 +00:00
|
|
|
NS_METHOD
|
2012-08-22 15:56:38 +00:00
|
|
|
nsStorageInputStream::Seek(uint32_t aPosition)
|
1999-11-16 19:14:08 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t length = mStorageStream->mLogicalLength;
|
2007-03-09 12:28:55 +00:00
|
|
|
if (aPosition > length)
|
1999-11-16 19:14:08 +00:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
2007-08-17 20:27:50 +00:00
|
|
|
if (length == 0)
|
|
|
|
return NS_OK;
|
|
|
|
|
1999-11-16 19:14:08 +00:00
|
|
|
mSegmentNum = SegNum(aPosition);
|
2011-09-08 07:12:42 +00:00
|
|
|
mReadCursor = SegOffset(aPosition);
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t available = length - aPosition;
|
2013-01-15 12:22:03 +00:00
|
|
|
mSegmentEnd = mReadCursor + XPCOM_MIN(mSegmentSize - mReadCursor, available);
|
1999-11-16 19:14:08 +00:00
|
|
|
mLogicalCursor = aPosition;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-08-18 13:46:39 +00:00
|
|
|
nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
NS_NewStorageStream(uint32_t segmentSize, uint32_t maxSize, nsIStorageStream **result)
|
1999-11-16 19:14:08 +00:00
|
|
|
{
|
|
|
|
nsStorageStream* storageStream = new nsStorageStream();
|
|
|
|
if (!storageStream) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(storageStream);
|
2012-07-30 14:20:58 +00:00
|
|
|
nsresult rv = storageStream->Init(segmentSize, maxSize, nullptr);
|
2004-03-02 01:13:15 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(storageStream);
|
|
|
|
return rv;
|
|
|
|
}
|
1999-11-16 19:14:08 +00:00
|
|
|
*result = storageStream;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-11-26 17:42:58 +00:00
|
|
|
|
|
|
|
// Undefine LOG, so that other .cpp files (or their includes) won't complain
|
|
|
|
// about it already being defined, when we build in unified mode.
|
|
|
|
#undef LOG
|