Bug 1364768: Part 1 - Add NetUtil.readInputStream helper. r?aswan draft
authorKris Maglione <maglione.k@gmail.com>
Sun, 14 May 2017 15:54:12 -0700
changeset 577570 ee313b675c825a0510354280b4fb6c0588e3f589
parent 577569 55562d4d732bc2cc3023e2a3f64a504d7fe48cb6
child 577571 c23fc0dedeae5778ae549588a04164734565489e
push id58718
push usermaglione.k@gmail.com
push dateSun, 14 May 2017 23:25:47 +0000
reviewersaswan
bugs1364768
milestone55.0a1
Bug 1364768: Part 1 - Add NetUtil.readInputStream helper. r?aswan MozReview-Commit-ID: IZRvdcIiV4z
netwerk/base/NetUtil.jsm
--- a/netwerk/base/NetUtil.jsm
+++ b/netwerk/base/NetUtil.jsm
@@ -20,16 +20,19 @@ const Cc = Components.classes;
 const Cr = Components.results;
 const Cu = Components.utils;
 
 const PR_UINT32_MAX = 0xffffffff;
 
 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
 Components.utils.import("resource://gre/modules/Services.jsm");
 
+const BinaryInputStream = Components.Constructor("@mozilla.org/binaryinputstream;1",
+                                                 "nsIBinaryInputStream", "setInputStream");
+
 ////////////////////////////////////////////////////////////////////////////////
 //// NetUtil Object
 
 this.NetUtil = {
     /**
      * Function to perform simple async copying from aSource (an input stream)
      * to aSink (an output stream).  The copy will happen on some background
      * thread.  Both streams will be closed when the copy completes.
@@ -443,16 +446,53 @@ this.NetUtil = {
         catch (e) {
             // Adjust the stack so it throws at the caller's location.
             throw new Components.Exception(e.message, e.result,
                                            Components.stack.caller, e.data);
         }
     },
 
     /**
+     * Reads aCount bytes from aInputStream into a string.
+     *
+     * @param {nsIInputStream} aInputStream
+     *        The input stream to read from.
+     * @param {integer} [aCount = aInputStream.available()]
+     *        The number of bytes to read from the stream.
+     *
+     * @return the bytes from the input stream in string form.
+     *
+     * @throws NS_ERROR_INVALID_ARG if aInputStream is not an nsIInputStream.
+     * @throws NS_BASE_STREAM_WOULD_BLOCK if reading from aInputStream would
+     *         block the calling thread (non-blocking mode only).
+     * @throws NS_ERROR_FAILURE if there are not enough bytes available to read
+     *         aCount amount of data.
+     */
+    readInputStream(aInputStream, aCount)
+    {
+        if (!(aInputStream instanceof Ci.nsIInputStream)) {
+            let exception = new Components.Exception(
+                "First argument should be an nsIInputStream",
+                Cr.NS_ERROR_INVALID_ARG,
+                Components.stack.caller
+            );
+            throw exception;
+        }
+
+        if (!aCount) {
+            aCount = aInputStream.available();
+        }
+
+        let stream = new BinaryInputStream(aInputStream);
+        let result = new ArrayBuffer(aCount);
+        stream.readArrayBuffer(result.byteLength, result);
+        return result;
+    },
+
+    /**
      * Returns a reference to nsIIOService.
      *
      * @return a reference to nsIIOService.
      */
     get ioService()
     {
         delete this.ioService;
         return this.ioService = Cc["@mozilla.org/network/io-service;1"].