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