Bug 661529: Add NS_ReadInputStreamToBuffer to complement NS_ReadInputStreamToString. r=biesi
authorKyle Huey <khuey@kylehuey.com>
Wed, 08 Jun 2011 11:11:41 -0700
changeset 70754 edaa20bfaffe35ec6abec28b1654eb2ff7b8828b
parent 70753 78b64bf8262221202ce7babeee0d8f46e680013c
child 70755 dd0a133916c7f6b9fd89bc2855ac607d9d0e1945
push idunknown
push userunknown
push dateunknown
reviewersbiesi
bugs661529
milestone7.0a1
Bug 661529: Add NS_ReadInputStreamToBuffer to complement NS_ReadInputStreamToString. r=biesi
netwerk/base/public/nsNetUtil.h
--- a/netwerk/base/public/nsNetUtil.h
+++ b/netwerk/base/public/nsNetUtil.h
@@ -1166,26 +1166,29 @@ NS_NewPostDataStream(nsIInputStream  **r
     if (NS_FAILED(rv))
         return rv;
 
     NS_ADDREF(*result = stream);
     return NS_OK;
 }
 
 inline nsresult
-NS_ReadInputStreamToString(nsIInputStream *aInputStream, 
-                           nsACString &aDest,
+NS_ReadInputStreamToBuffer(nsIInputStream *aInputStream, 
+                           void** aDest,
                            PRUint32 aCount)
 {
     nsresult rv;
 
-    aDest.SetLength(aCount);
-    if (aDest.Length() != aCount)
-        return NS_ERROR_OUT_OF_MEMORY;
-    char * p = aDest.BeginWriting();
+    if (!*aDest) {
+        *aDest = malloc(aCount);
+        if (!*aDest)
+            return NS_ERROR_OUT_OF_MEMORY;
+    }
+
+    char * p = reinterpret_cast<char*>(*aDest);
     PRUint32 bytesRead;
     PRUint32 totalRead = 0;
     while (1) {
         rv = aInputStream->Read(p + totalRead, aCount - totalRead, &bytesRead);
         if (!NS_SUCCEEDED(rv)) 
             return rv;
         totalRead += bytesRead;
         if (totalRead == aCount)
@@ -1193,16 +1196,28 @@ NS_ReadInputStreamToString(nsIInputStrea
         // if Read reads 0 bytes, we've hit EOF 
         if (bytesRead == 0)
             return NS_ERROR_UNEXPECTED;
     }
     return rv; 
 }
 
 inline nsresult
+NS_ReadInputStreamToString(nsIInputStream *aInputStream, 
+                           nsACString &aDest,
+                           PRUint32 aCount)
+{
+    aDest.SetLength(aCount);
+    if (aDest.Length() != aCount)
+        return NS_ERROR_OUT_OF_MEMORY;
+    void* dest = aDest.BeginWriting();
+    return NS_ReadInputStreamToBuffer(aInputStream, &dest, aCount);
+}
+
+inline nsresult
 NS_LoadPersistentPropertiesFromURI(nsIPersistentProperties **result,
                                    nsIURI                   *uri,
                                    nsIIOService             *ioService = nsnull)
 {
     nsCOMPtr<nsIInputStream> in;
     nsresult rv = NS_OpenURI(getter_AddRefs(in), uri, ioService);
     if (NS_SUCCEEDED(rv)) {
         nsCOMPtr<nsIPersistentProperties> properties =