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
|
2008-02-01 18:07:49 -08:00
|
|
|
nsScriptableInputStream::Available(PRUint32 *_retval) {
|
|
|
|
if (!mInputStream) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
return mInputStream->Available(_retval);
|
2008-02-01 00:51:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2008-02-01 18:07:49 -08:00
|
|
|
nsScriptableInputStream::Read(PRUint32 aCount, char **_retval) {
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
PRUint32 count = 0;
|
|
|
|
char *buffer = nsnull;
|
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
|
|
|
|
2008-02-01 18:07:49 -08:00
|
|
|
rv = mInputStream->Available(&count);
|
|
|
|
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
|
|
|
|
count = NS_MIN(NS_MIN(count, aCount), PR_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
|
|
|
|
2008-02-01 18:07:49 -08:00
|
|
|
PRUint32 amtRead = 0;
|
|
|
|
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
|
|
|
|
nsScriptableInputStream::ReadBytes(PRUint32 aCount, nsACString &_retval) {
|
|
|
|
if (!mInputStream) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
_retval.SetLength(aCount);
|
|
|
|
if (_retval.Length() != aCount) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *ptr = _retval.BeginWriting();
|
|
|
|
PRUint32 totalBytesRead = 0;
|
|
|
|
while (1) {
|
|
|
|
PRUint32 bytesRead;
|
|
|
|
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;
|
|
|
|
}
|