Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsTemporaryFileInputStream.h" |
michael@0 | 7 | #include "nsStreamUtils.h" |
michael@0 | 8 | #include <algorithm> |
michael@0 | 9 | |
michael@0 | 10 | NS_IMPL_ISUPPORTS(nsTemporaryFileInputStream, nsIInputStream) |
michael@0 | 11 | |
michael@0 | 12 | nsTemporaryFileInputStream::nsTemporaryFileInputStream(FileDescOwner* aFileDescOwner, uint64_t aStartPos, uint64_t aEndPos) |
michael@0 | 13 | : mFileDescOwner(aFileDescOwner), |
michael@0 | 14 | mStartPos(aStartPos), |
michael@0 | 15 | mEndPos(aEndPos), |
michael@0 | 16 | mClosed(false) |
michael@0 | 17 | { |
michael@0 | 18 | NS_ASSERTION(aStartPos <= aEndPos, "StartPos should less equal than EndPos!"); |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | NS_IMETHODIMP |
michael@0 | 22 | nsTemporaryFileInputStream::Close() |
michael@0 | 23 | { |
michael@0 | 24 | mClosed = true; |
michael@0 | 25 | return NS_OK; |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | NS_IMETHODIMP |
michael@0 | 29 | nsTemporaryFileInputStream::Available(uint64_t * bytesAvailable) |
michael@0 | 30 | { |
michael@0 | 31 | if (mClosed) |
michael@0 | 32 | return NS_BASE_STREAM_CLOSED; |
michael@0 | 33 | |
michael@0 | 34 | NS_ASSERTION(mStartPos <= mEndPos, "StartPos should less equal than EndPos!"); |
michael@0 | 35 | |
michael@0 | 36 | *bytesAvailable = mEndPos - mStartPos; |
michael@0 | 37 | return NS_OK; |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | NS_IMETHODIMP |
michael@0 | 41 | nsTemporaryFileInputStream::Read(char* buffer, uint32_t count, uint32_t* bytesRead) |
michael@0 | 42 | { |
michael@0 | 43 | return ReadSegments(NS_CopySegmentToBuffer, buffer, count, bytesRead); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | NS_IMETHODIMP |
michael@0 | 47 | nsTemporaryFileInputStream::ReadSegments(nsWriteSegmentFun writer, |
michael@0 | 48 | void * closure, |
michael@0 | 49 | uint32_t count, |
michael@0 | 50 | uint32_t * result) |
michael@0 | 51 | { |
michael@0 | 52 | NS_ASSERTION(result, "null ptr"); |
michael@0 | 53 | NS_ASSERTION(mStartPos <= mEndPos, "bad stream state"); |
michael@0 | 54 | *result = 0; |
michael@0 | 55 | |
michael@0 | 56 | if (mClosed) { |
michael@0 | 57 | return NS_BASE_STREAM_CLOSED; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | mozilla::MutexAutoLock lock(mFileDescOwner->FileMutex()); |
michael@0 | 61 | PR_Seek64(mFileDescOwner->mFD, mStartPos, PR_SEEK_SET); |
michael@0 | 62 | |
michael@0 | 63 | count = std::min(count, uint32_t(mEndPos - mStartPos)); |
michael@0 | 64 | uint32_t remainBufCount = count; |
michael@0 | 65 | |
michael@0 | 66 | char buf[4096]; |
michael@0 | 67 | while (remainBufCount > 0) { |
michael@0 | 68 | uint32_t bufCount = std::min(remainBufCount, (uint32_t)sizeof(buf)); |
michael@0 | 69 | int32_t read_result = PR_Read(mFileDescOwner->mFD, buf, bufCount); |
michael@0 | 70 | if (read_result < 0) { |
michael@0 | 71 | return NS_ErrorAccordingToNSPR(); |
michael@0 | 72 | } |
michael@0 | 73 | uint32_t write_result = 0; |
michael@0 | 74 | nsresult rv = writer(this, closure, buf, |
michael@0 | 75 | count - remainBufCount, bufCount, &write_result); |
michael@0 | 76 | remainBufCount -= bufCount; |
michael@0 | 77 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 78 | NS_ASSERTION(write_result <= bufCount, |
michael@0 | 79 | "writer should not write more than we asked it to write"); |
michael@0 | 80 | } |
michael@0 | 81 | mStartPos += bufCount; |
michael@0 | 82 | } |
michael@0 | 83 | *result = count; |
michael@0 | 84 | return NS_OK; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | NS_IMETHODIMP |
michael@0 | 88 | nsTemporaryFileInputStream::IsNonBlocking(bool * nonBlocking) |
michael@0 | 89 | { |
michael@0 | 90 | *nonBlocking = false; |
michael@0 | 91 | return NS_OK; |
michael@0 | 92 | } |
michael@0 | 93 |