Use Uint8Array instead of byte array (from websocket branch).
diff --git a/js/encoding/BinaryXMLDecoder.js b/js/encoding/BinaryXMLDecoder.js
index 0c88e0f..53343f4 100644
--- a/js/encoding/BinaryXMLDecoder.js
+++ b/js/encoding/BinaryXMLDecoder.js
@@ -537,12 +537,11 @@
};
-//byte[]
+//Uint8Array
BinaryXMLDecoder.prototype.decodeBlob = function(
//int
blobLength) {
-
if(null == blobLength){
//TypeAndVal
tv = this.decodeTypeAndVal();
@@ -560,12 +559,10 @@
}
//
- //byte []
-
- var bytes = this.istream.slice(this.offset, this.offset+ blobLength);
+ //Uint8Array
+ var bytes = this.istream.subarray(this.offset, this.offset+ blobLength);
this.offset += blobLength;
- //int
return bytes;
};
@@ -679,9 +676,6 @@
TypeAndVal.prototype.val = function(){
return this.v;
};
-//TODO
-
-
@@ -713,13 +707,14 @@
return strElementText;
};
+
/*
* Set the offset into the input, used for the next read.
*/
BinaryXMLDecoder.prototype.seek = function(
//int
offset) {
- this.offset = offset;
+ this.offset = offset;
}
/*
@@ -734,4 +729,3 @@
ContentDecodingException.prototype = new Error();
ContentDecodingException.prototype.name = "ContentDecodingException";
-
diff --git a/js/encoding/BinaryXMLEncoder.js b/js/encoding/BinaryXMLEncoder.js
index c8eec3b..2a67e05 100644
--- a/js/encoding/BinaryXMLEncoder.js
+++ b/js/encoding/BinaryXMLEncoder.js
@@ -41,37 +41,37 @@
var BinaryXMLEncoder = function BinaryXMLEncoder(){
-
- this.ostream = new Array(10000);
-
-
+ this.ostream = new Uint8Array(10000);
this.offset =0;
-
this.CODEC_NAME = "Binary";
-
};
-BinaryXMLEncoder.prototype.writeUString = function(/*String*/ utf8Content){
+
+BinaryXMLEncoder.prototype.writeUString = function(/*String*/ utf8Content) {
this.encodeUString(this.ostream, utf8Content, XML_UDATA);
};
-BinaryXMLEncoder.prototype.writeBlob = function(/*byte []*/ binaryContent
+
+BinaryXMLEncoder.prototype.writeBlob = function(
+ /*Uint8Array*/ binaryContent
//, /*int*/ offset, /*int*/ length
- ) {
+ ) {
if(LOG >3) console.log(binaryContent);
this.encodeBlob(this.ostream, binaryContent, this.offset, binaryContent.length);
};
-BinaryXMLEncoder.prototype.writeStartElement = function(/*String*/ tag, /*TreeMap<String,String>*/ attributes){
- /*Long*/ dictionaryVal = tag;//stringToTag(tag);
+BinaryXMLEncoder.prototype.writeStartElement = function(
+ /*String*/ tag,
+ /*TreeMap<String,String>*/ attributes
+ ) {
+
+ /*Long*/ dictionaryVal = tag; //stringToTag(tag);
if (null == dictionaryVal) {
-
this.encodeUString(this.ostream, tag, XML_TAG);
-
} else {
this.encodeTypeAndVal(XML_DTAG, dictionaryVal, this.ostream);
}
@@ -82,14 +82,13 @@
};
-BinaryXMLEncoder.prototype.writeEndElement = function(){
-
+BinaryXMLEncoder.prototype.writeEndElement = function() {
this.ostream[this.offset] = XML_CLOSE;
- this.offset+= 1;
+ this.offset += 1;
}
+
BinaryXMLEncoder.prototype.writeAttributes = function(/*TreeMap<String,String>*/ attributes) {
-
if (null == attributes) {
return;
}
@@ -114,10 +113,9 @@
this.encodeUString(this.ostream, strValue);
}
-
-
}
+
//returns a string
stringToTag = function(/*long*/ tagVal) {
if ((tagVal >= 0) && (tagVal < CCNProtocolDTagsStrings.length)) {
@@ -149,45 +147,35 @@
//byte[]
Content,
//TreeMap<String, String>
- attributes) {
-
+ attributes
+ ) {
this.writeStartElement(tag, attributes);
// Will omit if 0-length
if(typeof Content === 'number') {
- if(LOG>4) console.log('GOING TO WRITE THE NUMBER .charCodeAt(0) ' +Content.toString().charCodeAt(0) );
- if(LOG>4) console.log('GOING TO WRITE THE NUMBER ' +Content.toString() );
- if(LOG>4) console.log('type of number is ' +typeof Content.toString() );
-
-
+ if(LOG>4) console.log('GOING TO WRITE THE NUMBER .charCodeAt(0) ' + Content.toString().charCodeAt(0) );
+ if(LOG>4) console.log('GOING TO WRITE THE NUMBER ' + Content.toString() );
+ if(LOG>4) console.log('type of number is ' + typeof Content.toString() );
this.writeUString(Content.toString());
//whatever
-
}
-
else if(typeof Content === 'string'){
- if(LOG>4) console.log('GOING TO WRITE THE STRING ' +Content );
- if(LOG>4) console.log('type of STRING is ' +typeof Content );
+ if(LOG>4) console.log('GOING TO WRITE THE STRING ' + Content );
+ if(LOG>4) console.log('type of STRING is ' + typeof Content );
this.writeUString(Content);
}
-
else{
- //else if(typeof Content === 'string'){
- //console.log('went here');
- //this.writeBlob(Content);
- //}
- if(LOG>4) console.log('GOING TO WRITE A BLOB ' +Content );
- //else if(typeof Content === 'object'){
+ if(LOG>4) console.log('GOING TO WRITE A BLOB ' + Content );
+
this.writeBlob(Content);
- //}
}
this.writeEndElement();
}
-//TODO
+
var TypeAndVal = function TypeAndVal(_type,_val) {
this.type = _type;
@@ -195,17 +183,19 @@
};
+
BinaryXMLEncoder.prototype.encodeTypeAndVal = function(
//int
type,
//long
val,
- //byte []
- buf) {
+ //ArrayBufferView
+ ostream
+ ) {
- if(LOG>4)console.log('Encoding type '+ type+ ' and value '+ val);
+ if(LOG>4) console.log('Encoding type '+ type+ ' and value '+ val);
- if(LOG>4) console.log('OFFSET IS ' + this.offset );
+ if(LOG>4) console.log('OFFSET IS ' + this.offset);
if ((type > XML_UDATA) || (type < 0) || (val < 0)) {
throw new Error("Tag and value must be positive, and tag valid.");
@@ -214,14 +204,14 @@
// Encode backwards. Calculate how many bytes we need:
var numEncodingBytes = this.numEncodingBytes(val);
- if ((this.offset + numEncodingBytes) > buf.length) {
- throw new Error("Buffer space of " + (buf.length-this.offset) +
+ if ((this.offset + numEncodingBytes) > ostream.length) {
+ throw new Error("Buffer space of " + (ostream.length - this.offset) +
" bytes insufficient to hold " +
numEncodingBytes + " of encoded type and value.");
}
// Bottom 4 bits of val go in last byte with tag.
- buf[this.offset + numEncodingBytes - 1] =
+ ostream[this.offset + numEncodingBytes - 1] =
//(byte)
(BYTE_MASK &
(((XML_TT_MASK & type) |
@@ -233,9 +223,8 @@
// is "more" flag.
var i = this.offset + numEncodingBytes - 2;
while ((0 != val) && (i >= this.offset)) {
- buf[i] = //(byte)
- (BYTE_MASK &
- (val & XML_REG_VAL_MASK)); // leave top bit unset
+ ostream[i] = //(byte)
+ (BYTE_MASK & (val & XML_REG_VAL_MASK)); // leave top bit unset
val = val >>> XML_REG_VAL_BITS;
--i;
}
@@ -248,6 +237,7 @@
return numEncodingBytes;
};
+
BinaryXMLEncoder.prototype.encodeUString = function(
//OutputStream
ostream,
@@ -260,23 +250,15 @@
return;
}
-
- //byte [] data utils
- /*custom*/
- //byte[]
-
if(LOG>3) console.log("The string to write is ");
-
if(LOG>3) console.log(ustring);
//COPY THE STRING TO AVOID PROBLEMS
strBytes = new Array(ustring.length);
-
- var i = 0;
- for( ; i<ustring.length; i++) //in InStr.ToCharArray())
+ for (i = 0; i < ustring.length; i++) //in InStr.ToCharArray())
{
- if(LOG>3)console.log("ustring[" + i + '] = ' + ustring[i]);
+ if(LOG>3) console.log('ustring[' + i + '] = ' + ustring[i]);
strBytes[i] = ustring.charCodeAt(i);
}
@@ -292,9 +274,7 @@
if(LOG>3) console.log(strBytes);
this.writeString(strBytes,this.offset);
-
this.offset+= strBytes.length;
-
};
@@ -302,7 +282,7 @@
BinaryXMLEncoder.prototype.encodeBlob = function(
//OutputStream
ostream,
- //byte []
+ //Uint8Array
blob,
//int
offset,
@@ -315,21 +295,17 @@
if(LOG>4) console.log('LENGTH OF XML_BLOB IS '+length);
+ /*blobCopy = new Array(blob.Length);
- blobCopy = new Array(blob.Length);
- var i = 0;
- for( ;i<blob.length;i++) //in InStr.ToCharArray())
+ for (i = 0; i < blob.length; i++) //in InStr.ToCharArray())
{
blobCopy[i] = blob[i];
- }
+ }*/
- this.encodeTypeAndVal(XML_BLOB, length, ostream,offset);
-
- if (null != blob) {
+ this.encodeTypeAndVal(XML_BLOB, length, ostream, offset);
- this.writeBlobArray(blobCopy,this.offset);
- this.offset += length;
- }
+ this.writeBlobArray(blob, this.offset);
+ this.offset += length;
};
var ENCODING_LIMIT_1_BYTE = ((1 << (XML_TT_VAL_BITS)) - 1);
@@ -376,13 +352,10 @@
if(LOG>4)console.log('ENCODING DATE with BINARY VALUE');
if(LOG>4)console.log(binarydate);
-
if(LOG>4)console.log('ENCODING DATE with BINARY VALUE(HEX)');
if(LOG>4)console.log(DataUtils.toHex(binarydate));
-
this.writeElement(tag, binarydate);
-
};
BinaryXMLEncoder.prototype.writeString = function(
@@ -396,19 +369,16 @@
if(LOG>4) console.log('GOING TO WRITE A STRING');
if(LOG>4) console.log(input);
- for (var i = 0; i < input.length; i++) {
+ for (i = 0; i < input.length; i++) {
if(LOG>4) console.log('input.charCodeAt(i)=' + input.charCodeAt(i));
this.ostream[this.offset+i] = (input.charCodeAt(i));
}
}
-
else{
-
- if(LOG>4) console.log('GOING TO WRITE A STRING IN BINARY FORM');
- if(LOG>4) console.log(input);
-
- this.writeBlobArray(input);
-
+ if(LOG>4) console.log('GOING TO WRITE A STRING IN BINARY FORM');
+ if(LOG>4) console.log(input);
+
+ this.writeBlobArray(input);
}
/*
else if(typeof input === 'object'){
@@ -417,25 +387,23 @@
*/
};
+
BinaryXMLEncoder.prototype.writeBlobArray = function(
- //String
- Blob,
- //CCNTime
+ //Uint8Array
+ blob,
+ //int
offset) {
if(LOG>4) console.log('GOING TO WRITE A BLOB');
- for (var i = 0; i < Blob.length; i++) {
+ /*for (var i = 0; i < Blob.length; i++) {
this.ostream[this.offset+i] = Blob[i];
- }
-
+ }*/
+ this.ostream.set(blob, this.offset);
};
-
BinaryXMLEncoder.prototype.getReducedOstream = function() {
-
- return this.ostream.slice(0,this.offset);
-
+ return this.ostream.subarray(0, this.offset);
};