1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/base/src/nsTemporaryFileInputStream.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,93 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsTemporaryFileInputStream.h" 1.10 +#include "nsStreamUtils.h" 1.11 +#include <algorithm> 1.12 + 1.13 +NS_IMPL_ISUPPORTS(nsTemporaryFileInputStream, nsIInputStream) 1.14 + 1.15 +nsTemporaryFileInputStream::nsTemporaryFileInputStream(FileDescOwner* aFileDescOwner, uint64_t aStartPos, uint64_t aEndPos) 1.16 + : mFileDescOwner(aFileDescOwner), 1.17 + mStartPos(aStartPos), 1.18 + mEndPos(aEndPos), 1.19 + mClosed(false) 1.20 +{ 1.21 + NS_ASSERTION(aStartPos <= aEndPos, "StartPos should less equal than EndPos!"); 1.22 +} 1.23 + 1.24 +NS_IMETHODIMP 1.25 +nsTemporaryFileInputStream::Close() 1.26 +{ 1.27 + mClosed = true; 1.28 + return NS_OK; 1.29 +} 1.30 + 1.31 +NS_IMETHODIMP 1.32 +nsTemporaryFileInputStream::Available(uint64_t * bytesAvailable) 1.33 +{ 1.34 + if (mClosed) 1.35 + return NS_BASE_STREAM_CLOSED; 1.36 + 1.37 + NS_ASSERTION(mStartPos <= mEndPos, "StartPos should less equal than EndPos!"); 1.38 + 1.39 + *bytesAvailable = mEndPos - mStartPos; 1.40 + return NS_OK; 1.41 +} 1.42 + 1.43 +NS_IMETHODIMP 1.44 +nsTemporaryFileInputStream::Read(char* buffer, uint32_t count, uint32_t* bytesRead) 1.45 +{ 1.46 + return ReadSegments(NS_CopySegmentToBuffer, buffer, count, bytesRead); 1.47 +} 1.48 + 1.49 +NS_IMETHODIMP 1.50 +nsTemporaryFileInputStream::ReadSegments(nsWriteSegmentFun writer, 1.51 + void * closure, 1.52 + uint32_t count, 1.53 + uint32_t * result) 1.54 +{ 1.55 + NS_ASSERTION(result, "null ptr"); 1.56 + NS_ASSERTION(mStartPos <= mEndPos, "bad stream state"); 1.57 + *result = 0; 1.58 + 1.59 + if (mClosed) { 1.60 + return NS_BASE_STREAM_CLOSED; 1.61 + } 1.62 + 1.63 + mozilla::MutexAutoLock lock(mFileDescOwner->FileMutex()); 1.64 + PR_Seek64(mFileDescOwner->mFD, mStartPos, PR_SEEK_SET); 1.65 + 1.66 + count = std::min(count, uint32_t(mEndPos - mStartPos)); 1.67 + uint32_t remainBufCount = count; 1.68 + 1.69 + char buf[4096]; 1.70 + while (remainBufCount > 0) { 1.71 + uint32_t bufCount = std::min(remainBufCount, (uint32_t)sizeof(buf)); 1.72 + int32_t read_result = PR_Read(mFileDescOwner->mFD, buf, bufCount); 1.73 + if (read_result < 0) { 1.74 + return NS_ErrorAccordingToNSPR(); 1.75 + } 1.76 + uint32_t write_result = 0; 1.77 + nsresult rv = writer(this, closure, buf, 1.78 + count - remainBufCount, bufCount, &write_result); 1.79 + remainBufCount -= bufCount; 1.80 + if (NS_SUCCEEDED(rv)) { 1.81 + NS_ASSERTION(write_result <= bufCount, 1.82 + "writer should not write more than we asked it to write"); 1.83 + } 1.84 + mStartPos += bufCount; 1.85 + } 1.86 + *result = count; 1.87 + return NS_OK; 1.88 +} 1.89 + 1.90 +NS_IMETHODIMP 1.91 +nsTemporaryFileInputStream::IsNonBlocking(bool * nonBlocking) 1.92 +{ 1.93 + *nonBlocking = false; 1.94 + return NS_OK; 1.95 +} 1.96 +