Bug 590654 - Let JavaScript read embedded nulls from input streams

Adds a new method to NetUtil (readInputStreamToString) that will read a string
with or without embedded NULLs from an input stream.  Also adds the needed API
on nsIScriptableInputStream to make this happen.
r=bz
sr=biesi
a=blocking2.0
This commit is contained in:
Shawn Wilsher
2010-08-27 12:42:51 -07:00
parent f06b4fd630
commit d8779db890
4 changed files with 197 additions and 10 deletions

View File

@@ -37,6 +37,7 @@
#include "nsScriptableInputStream.h"
#include "nsMemory.h"
#include "nsString.h"
NS_IMPL_ISUPPORTS1(nsScriptableInputStream, nsIScriptableInputStream)
@@ -88,6 +89,43 @@ nsScriptableInputStream::Read(PRUint32 aCount, char **_retval) {
return NS_OK;
}
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;
}
nsresult
nsScriptableInputStream::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult) {
if (aOuter) return NS_ERROR_NO_AGGREGATION;