Skip to content

Commit

Permalink
Bug 831107 - Part 1: Allow TCPSocket consumers to receive typed array…
Browse files Browse the repository at this point in the history
… data without converting it to a byte string first. r=vlad
  • Loading branch information
jdm committed Apr 19, 2013
1 parent c9bb10b commit ecf5004
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
6 changes: 3 additions & 3 deletions dom/network/src/TCPSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,9 @@ TCPSocket.prototype = {
// nsIStreamListener (Triggered by _inputStreamPump.asyncRead)
onDataAvailable: function ts_onDataAvailable(request, context, inputStream, offset, count) {
if (this._binaryType === "arraybuffer") {
let ua = this.useWin ? new this.useWin.Uint8Array(count)
: new Uint8Array(count);
ua.set(this._inputStreamBinary.readByteArray(count));
let buffer = this._inputStreamBinary.readArrayBuffer(count);
let constructor = this.useWin ? this.useWin.Uint8Array : Uint8Array;
let ua = new constructor(buffer);
this.callListener("data", ua);
} else {
this.callListener("data", this._inputStreamScriptable.read(count));
Expand Down
27 changes: 27 additions & 0 deletions xpcom/io/nsBinaryStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include "nsComponentManagerUtils.h"
#include "nsIURI.h" // for NS_IURI_IID

#include "jsapi.h"
#include "jsfriendapi.h"

NS_IMPL_ISUPPORTS3(nsBinaryOutputStream, nsIObjectOutputStream, nsIBinaryOutputStream, nsIOutputStream)

NS_IMETHODIMP
Expand Down Expand Up @@ -715,6 +718,30 @@ nsBinaryInputStream::ReadByteArray(uint32_t aLength, uint8_t* *_rval)
return ReadBytes(aLength, reinterpret_cast<char **>(_rval));
}

NS_IMETHODIMP
nsBinaryInputStream::ReadArrayBuffer(uint32_t aLength, JSContext* cx, JS::Value* _rval)
{
JSAutoRequest ar(cx);
JS::RootedObject buffer(cx, JS_NewArrayBuffer(cx, aLength));
if (!buffer) {
return NS_ERROR_FAILURE;
}
uint8_t* data = JS_GetArrayBufferData(buffer);
if (!data) {
return NS_ERROR_FAILURE;
}

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

*_rval = JS::ObjectValue(*buffer);
return NS_OK;
}

NS_IMETHODIMP
nsBinaryInputStream::ReadObject(bool aIsStrongRef, nsISupports* *aObject)
{
Expand Down
10 changes: 10 additions & 0 deletions xpcom/io/nsIBinaryInputStream.idl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ interface nsIBinaryInputStream : nsIInputStream {
*/
void readByteArray(in uint32_t aLength,
[array, size_is(aLength), retval] out uint8_t aBytes);

/**
* Read opaque bytes from the stream, storing the results in an ArrayBuffer.
*
* @param aLength the number of bytes that must be read
*
* @throws NS_ERROR_FAILURE if it can't read aLength bytes
*/
[implicit_jscontext]
jsval readArrayBuffer(in uint32_t aLength);
};

%{C++
Expand Down

0 comments on commit ecf5004

Please sign in to comment.