Skip to content

Commit

Permalink
Bug 996310 - Change readArrayBuffer to return the number of bytes rea…
Browse files Browse the repository at this point in the history
…d. r=bsmedberg
  • Loading branch information
hawkinswnaf committed Apr 29, 2014
1 parent b94c682 commit 0e26fbe
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
14 changes: 6 additions & 8 deletions xpcom/io/nsBinaryStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,9 +814,10 @@ nsBinaryInputStream::ReadByteArray(uint32_t aLength, uint8_t** aResult)
}

NS_IMETHODIMP
nsBinaryInputStream::ReadArrayBuffer(uint32_t aLength,
JS::Handle<JS::Value> aBuffer,
JSContext* aCx)
+nsBinaryInputStream::ReadArrayBuffer(uint32_t aLength,
JS::HandleValue aBuffer,
JSContext* aCx,
uint32_t *rLength)
{
if (!aBuffer.isObject()) {
return NS_ERROR_FAILURE;
Expand All @@ -831,14 +832,11 @@ nsBinaryInputStream::ReadArrayBuffer(uint32_t aLength,
return NS_ERROR_FAILURE;
}

uint32_t bytesRead;
nsresult rv = Read(reinterpret_cast<char*>(data), aLength, &bytesRead);
nsresult rv = Read(reinterpret_cast<char*>(data), aLength, rLength);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
if (bytesRead != aLength) {
return NS_ERROR_FAILURE;
}

return NS_OK;
}

Expand Down
4 changes: 2 additions & 2 deletions xpcom/io/nsIBinaryInputStream.idl
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ interface nsIBinaryInputStream : nsIInputStream {
* Note: passing view.buffer, where view is an ArrayBufferView of an
* ArrayBuffer, is not valid unless view.byteOffset == 0.
*
* @throws NS_ERROR_FAILURE if it can't read aLength bytes
* @return The number of bytes actually read into aArrayBuffer.
*/
[implicit_jscontext]
void readArrayBuffer(in uint32_t aLength, in jsval aArrayBuffer);
unsigned long readArrayBuffer(in uint32_t aLength, in jsval aArrayBuffer);
};

%{C++
Expand Down
5 changes: 5 additions & 0 deletions xpcom/tests/unit/test_streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function test_binary_streams() {
const HelloArray = Array.map(HelloStr, function(c) {return c.charCodeAt(0)});
var countObj = {};
var msg = {};
var buffer = ArrayBuffer(HelloArray.length);

// Test reading immediately after writing.
os.writeBoolean(true);
Expand Down Expand Up @@ -72,6 +73,10 @@ function test_binary_streams() {
do_check_eq(typeof msg, typeof HelloArray);
do_check_eq(msg.toSource(), HelloArray.toSource());
do_check_eq(is.available(), 0);
os.writeByteArray(HelloArray, HelloArray.length);
do_check_eq(is.readArrayBuffer(buffer.byteLength, buffer), HelloArray.length);
do_check_eq([b for (b of Uint8Array(buffer))].toSource(), HelloArray.toSource());
do_check_eq(is.available(), 0);

// Test writing in one big chunk.
os.writeBoolean(true);
Expand Down

0 comments on commit 0e26fbe

Please sign in to comment.