1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsScriptableInputStream.h"
10 NS_IMPL_ISUPPORTS(nsScriptableInputStream
, nsIScriptableInputStream
)
12 // nsIScriptableInputStream methods
14 nsScriptableInputStream::Close() {
16 return NS_ERROR_NOT_INITIALIZED
;
18 return mInputStream
->Close();
22 nsScriptableInputStream::Init(nsIInputStream
* aInputStream
) {
24 return NS_ERROR_NULL_POINTER
;
26 mInputStream
= aInputStream
;
31 nsScriptableInputStream::Available(uint64_t* aResult
) {
33 return NS_ERROR_NOT_INITIALIZED
;
35 return mInputStream
->Available(aResult
);
39 nsScriptableInputStream::Read(uint32_t aCount
, char** aResult
) {
42 char* buffer
= nullptr;
45 return NS_ERROR_NOT_INITIALIZED
;
48 rv
= mInputStream
->Available(&count64
);
53 // bug716556 - Ensure count+1 doesn't overflow
55 XPCOM_MIN((uint32_t)XPCOM_MIN
<uint64_t>(count64
, aCount
), UINT32_MAX
- 1);
56 buffer
= (char*)malloc(count
+ 1); // make room for '\0'
58 return NS_ERROR_OUT_OF_MEMORY
;
61 rv
= ReadHelper(buffer
, count
);
73 nsScriptableInputStream::ReadBytes(uint32_t aCount
, nsACString
& aResult
) {
75 return NS_ERROR_NOT_INITIALIZED
;
78 if (!aResult
.SetLength(aCount
, mozilla::fallible
)) {
79 return NS_ERROR_OUT_OF_MEMORY
;
82 MOZ_ASSERT(aResult
.Length() == aCount
);
83 char* ptr
= aResult
.BeginWriting();
84 nsresult rv
= ReadHelper(ptr
, aCount
);
91 nsresult
nsScriptableInputStream::ReadHelper(char* aBuffer
, uint32_t aCount
) {
92 uint32_t totalBytesRead
= 0;
95 nsresult rv
= mInputStream
->Read(aBuffer
+ totalBytesRead
,
96 aCount
- totalBytesRead
, &bytesRead
);
101 totalBytesRead
+= bytesRead
;
102 if (totalBytesRead
== aCount
) {
106 // If we have read zero bytes, we have hit EOF.
107 if (bytesRead
== 0) {
108 return NS_ERROR_FAILURE
;
114 nsresult
nsScriptableInputStream::Create(REFNSIID aIID
, void** aResult
) {
115 RefPtr
<nsScriptableInputStream
> sis
= new nsScriptableInputStream();
116 return sis
->QueryInterface(aIID
, aResult
);