// Truncate the buffer by deleting the end segments NS_IMETHODIMP nsStorageStream::SetLength(uint32_t aLength) { if (NS_WARN_IF(!mSegmentedBuffer)) return NS_ERROR_NOT_INITIALIZED; if (mWriteInProgress) return NS_ERROR_NOT_AVAILABLE; if (aLength > mLogicalLength) return NS_ERROR_INVALID_ARG; int32_t newLastSegmentNum = SegNum(aLength); int32_t segmentOffset = SegOffset(aLength); if (segmentOffset == 0) newLastSegmentNum--; while (newLastSegmentNum < mLastSegmentNum) { mSegmentedBuffer->DeleteLastSegment(); mLastSegmentNum--; } mLogicalLength = aLength; return NS_OK; }
NS_METHOD nsStorageStream::Seek(int32_t aPosition) { if (NS_WARN_IF(!mSegmentedBuffer)) return NS_ERROR_NOT_INITIALIZED; // An argument of -1 means "seek to end of stream" if (aPosition == -1) aPosition = mLogicalLength; // Seeking beyond the buffer end is illegal if ((uint32_t)aPosition > mLogicalLength) 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; LOG(("nsStorageStream [%p] Seek mWriteCursor=%x mSegmentEnd=%x\n", this, mWriteCursor, mSegmentEnd)); return NS_OK; } // Segment may have changed, so reset pointers mWriteCursor = mSegmentedBuffer->GetSegment(mLastSegmentNum); NS_ASSERTION(mWriteCursor, "null mWriteCursor"); mSegmentEnd = mWriteCursor + mSegmentSize; // 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. int32_t segmentOffset = SegOffset(aPosition); if (segmentOffset == 0 && (SegNum(aPosition) > (uint32_t) mLastSegmentNum)) mWriteCursor = mSegmentEnd; else mWriteCursor += segmentOffset; LOG(("nsStorageStream [%p] Seek mWriteCursor=%x mSegmentEnd=%x\n", this, mWriteCursor, mSegmentEnd)); return NS_OK; }
NS_IMETHODIMP nsStorageStream::Close() { mWriteInProgress = PR_FALSE; PRInt32 segmentOffset = SegOffset(mLogicalLength); // 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; PR_LOG(StorageStreamLog, PR_LOG_DEBUG, ("nsStorageStream [%x] Close mWriteCursor=%x mSegmentEnd=%x\n", this, mWriteCursor, mSegmentEnd)); return NS_OK; }
NS_IMETHODIMP nsStorageStream::Close() { NS_ENSURE_TRUE(mSegmentedBuffer, NS_ERROR_NOT_INITIALIZED); mWriteInProgress = false; PRInt32 segmentOffset = SegOffset(mLogicalLength); // 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; LOG(("nsStorageStream [%p] Close mWriteCursor=%x mSegmentEnd=%x\n", this, mWriteCursor, mSegmentEnd)); return NS_OK; }
// Truncate the buffer by deleting the end segments NS_IMETHODIMP nsStorageStream::SetLength(PRUint32 aLength) { if (mWriteInProgress) return NS_ERROR_NOT_AVAILABLE; if (aLength > mLogicalLength) return NS_ERROR_INVALID_ARG; PRInt32 newLastSegmentNum = SegNum(aLength); PRInt32 segmentOffset = SegOffset(aLength); if (segmentOffset == 0) newLastSegmentNum--; while (newLastSegmentNum < mLastSegmentNum) { mSegmentedBuffer->DeleteLastSegment(); mLastSegmentNum--; } mLogicalLength = aLength; return NS_OK; }
NS_METHOD nsStorageStream::Seek(PRInt32 aPosition) { // An argument of -1 means "seek to end of stream" if (aPosition == -1) aPosition = mLogicalLength; // Seeking beyond the buffer end is illegal if ((PRUint32)aPosition > mLogicalLength) 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; PR_LOG(StorageStreamLog, PR_LOG_DEBUG, ("nsStorageStream [%x] Seek mWriteCursor=%x mSegmentEnd=%x\n", this, mWriteCursor, mSegmentEnd)); return NS_OK; } // Segment may have changed, so reset pointers mWriteCursor = mSegmentedBuffer->GetSegment(mLastSegmentNum); NS_ASSERTION(mWriteCursor, "null mWriteCursor"); mSegmentEnd = mWriteCursor + mSegmentSize; PRInt32 segmentOffset = SegOffset(aPosition); mWriteCursor += segmentOffset; PR_LOG(StorageStreamLog, PR_LOG_DEBUG, ("nsStorageStream [%x] Seek mWriteCursor=%x mSegmentEnd=%x\n", this, mWriteCursor, mSegmentEnd)); return NS_OK; }