In readAllFromSocket onDataAvailable, use the enhanced BinaryXMLDecoder so that we only have to concatenate the data once at the very end to avoid multiple large copies.
diff --git a/js/XpcomTransport.js b/js/XpcomTransport.js
index 643b918..c017848 100644
--- a/js/XpcomTransport.js
+++ b/js/XpcomTransport.js
@@ -24,12 +24,6 @@
var co = new ContentObject();
co.from_ccnb(decoder);
- if(LOG>2) {
- dump("DECODED CONTENT OBJECT\n");
- dump(co);
- dump("\n");
- }
-
// TODO: verify the content object and set kind to UPCALL_CONTENT.
var result = closure.upcall(Closure.UPCALL_CONTENT_UNVERIFIED,
new UpcallInfo(ndn, interest, 0, co));
@@ -70,7 +64,7 @@
outStream.flush();
var inStream = transport.openInputStream(0, 0, 0);
var dataListener = {
- data: new Uint8Array(0),
+ dataParts: [],
structureDecoder: new BinaryXMLStructureDecoder(),
calledOnReceivedData: false,
@@ -81,7 +75,7 @@
outStream.close();
if (!this.calledOnReceivedData) {
this.calledOnReceivedData = true;
- listener.onReceivedData(this.data);
+ listener.onReceivedData(DataUtils.concatArrays(this.dataParts));
}
},
onDataAvailable: function (request, context, _inputStream, offset, count) {
@@ -92,11 +86,15 @@
try {
// Ignore _inputStream and use inStream.
// Use readInputStreamToString to handle binary data.
- var rawData = NetUtil.readInputStreamToString(inStream, count);
- this.data = DataUtils.concatFromString(this.data, rawData);
+ // TODO: Can we go directly from the stream to Uint8Array?
+ var rawData = DataUtils.toNumbersFromString
+ (NetUtil.readInputStreamToString(inStream, count));
+ // Save for later call to concatArrays so that we only reallocate a buffer once.
+ this.dataParts.push(rawData);
// Scan the input to check if a whole ccnb object has been read.
- if (this.structureDecoder.findElementEnd(this.data))
+ this.structureDecoder.seek(0);
+ if (this.structureDecoder.findElementEnd(rawData))
// Finish.
this.onStopRequest();
} catch (ex) {