2008-02-01 18:07:49 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsScriptableInputStream.h"
|
|
|
|
#include "nsMemory.h"
|
2010-08-27 12:42:51 -07:00
|
|
|
#include "nsString.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-02-01 18:07:49 -08:00
|
|
|
NS_IMPL_ISUPPORTS1(nsScriptableInputStream, nsIScriptableInputStream)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-08-09 09:28:00 -07:00
|
|
|
// nsIScriptableInputStream methods
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2008-02-01 18:07:49 -08:00
|
|
|
nsScriptableInputStream::Close(void) {
|
|
|
|
if (!mInputStream) return NS_ERROR_NOT_INITIALIZED;
|
2007-03-22 10:30:00 -07:00
|
|
|
return mInputStream->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2008-02-01 18:07:49 -08:00
|
|
|
nsScriptableInputStream::Init(nsIInputStream *aInputStream) {
|
|
|
|
if (!aInputStream) return NS_ERROR_NULL_POINTER;
|
|
|
|
mInputStream = aInputStream;
|
2008-01-31 11:18:07 -08:00
|
|
|
return NS_OK;
|
2007-07-25 09:53:37 -07:00
|
|
|
}
|
|
|
|
|
2008-02-01 00:51:29 -08:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsScriptableInputStream::Available(uint64_t *_retval) {
|
2008-02-01 18:07:49 -08:00
|
|
|
if (!mInputStream) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
return mInputStream->Available(_retval);
|
2008-02-01 00:51:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsScriptableInputStream::Read(uint32_t aCount, char **_retval) {
|
2008-02-01 18:07:49 -08:00
|
|
|
nsresult rv = NS_OK;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t count64 = 0;
|
2012-07-30 07:20:58 -07:00
|
|
|
char *buffer = nullptr;
|
2007-07-25 09:53:37 -07:00
|
|
|
|
2008-02-01 18:07:49 -08:00
|
|
|
if (!mInputStream) return NS_ERROR_NOT_INITIALIZED;
|
2008-02-01 00:51:29 -08:00
|
|
|
|
2012-08-10 19:44:11 -07:00
|
|
|
rv = mInputStream->Available(&count64);
|
2008-02-01 18:07:49 -08:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2007-07-25 09:53:37 -07:00
|
|
|
|
2012-02-13 11:26:28 -08:00
|
|
|
// bug716556 - Ensure count+1 doesn't overflow
|
2012-09-27 23:57:33 -07:00
|
|
|
uint32_t count = NS_MIN((uint32_t)NS_MIN<uint64_t>(count64, aCount), UINT32_MAX - 1);
|
2008-02-01 18:07:49 -08:00
|
|
|
buffer = (char*)nsMemory::Alloc(count+1); // make room for '\0'
|
|
|
|
if (!buffer) return NS_ERROR_OUT_OF_MEMORY;
|
2008-02-01 00:51:29 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t amtRead = 0;
|
2008-02-01 18:07:49 -08:00
|
|
|
rv = mInputStream->Read(buffer, count, &amtRead);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
nsMemory::Free(buffer);
|
|
|
|
return rv;
|
|
|
|
}
|
2008-02-01 00:51:29 -08:00
|
|
|
|
2008-02-01 18:07:49 -08:00
|
|
|
buffer[amtRead] = '\0';
|
|
|
|
*_retval = buffer;
|
|
|
|
return NS_OK;
|
2008-02-01 00:51:29 -08:00
|
|
|
}
|
|
|
|
|
2010-08-27 12:42:51 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsScriptableInputStream::ReadBytes(uint32_t aCount, nsACString &_retval) {
|
2010-08-27 12:42:51 -07:00
|
|
|
if (!mInputStream) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
_retval.SetLength(aCount);
|
|
|
|
if (_retval.Length() != aCount) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *ptr = _retval.BeginWriting();
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t totalBytesRead = 0;
|
2010-08-27 12:42:51 -07:00
|
|
|
while (1) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t bytesRead;
|
2010-08-27 12:42:51 -07:00
|
|
|
nsresult rv = mInputStream->Read(ptr + totalBytesRead,
|
|
|
|
aCount - totalBytesRead,
|
|
|
|
&bytesRead);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
totalBytesRead += bytesRead;
|
|
|
|
if (totalBytesRead == aCount) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have read zero bytes, we have hit EOF.
|
|
|
|
if (bytesRead == 0) {
|
|
|
|
_retval.Truncate();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
nsresult
|
2008-02-01 18:07:49 -08:00
|
|
|
nsScriptableInputStream::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult) {
|
|
|
|
if (aOuter) return NS_ERROR_NO_AGGREGATION;
|
2008-02-01 00:51:29 -08:00
|
|
|
|
2008-02-01 18:07:49 -08:00
|
|
|
nsScriptableInputStream *sis = new nsScriptableInputStream();
|
|
|
|
if (!sis) return NS_ERROR_OUT_OF_MEMORY;
|
2008-02-01 00:51:29 -08:00
|
|
|
|
2008-02-01 18:07:49 -08:00
|
|
|
NS_ADDREF(sis);
|
|
|
|
nsresult rv = sis->QueryInterface(aIID, aResult);
|
|
|
|
NS_RELEASE(sis);
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|