summaryrefslogtreecommitdiff
path: root/js/src/Ice/browser/Buffer.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/src/Ice/browser/Buffer.js')
-rw-r--r--js/src/Ice/browser/Buffer.js718
1 files changed, 357 insertions, 361 deletions
diff --git a/js/src/Ice/browser/Buffer.js b/js/src/Ice/browser/Buffer.js
index 5d6c33393fe..54ef6475713 100644
--- a/js/src/Ice/browser/Buffer.js
+++ b/js/src/Ice/browser/Buffer.js
@@ -7,413 +7,409 @@
//
// **********************************************************************
-(function(global){
- //
- // IE 10 doesn't implement ArrayBuffer.slice
- //
-
- if(!ArrayBuffer.prototype.slice)
+var Ice = require("../Ice/ModuleRegistry").Ice;
+
+Ice.__M.require(module, "Ice", ["../Ice/Class", "../Ice/Long"]);
+
+var Long = Ice.Long;
+
+//
+// IE 10 doesn't implement ArrayBuffer.slice
+//
+
+if(!ArrayBuffer.prototype.slice)
+{
+ ArrayBuffer.prototype.slice = function (start, end)
{
- ArrayBuffer.prototype.slice = function (start, end)
+ var b = new Uint8Array(this);
+ end = end === undefined ? b.length : end;
+ var result = new Uint8Array(new ArrayBuffer(end - start));
+ for(var i = 0, length = result.length; i < length; i++)
{
- var b = new Uint8Array(this);
- end = end === undefined ? b.length : end;
- var result = new Uint8Array(new ArrayBuffer(end - start));
- for(var i = 0, length = result.length; i < length; i++)
- {
- result[i] = b[i + start];
- }
- return result.buffer;
- };
- }
-
- var __BufferOverflowException__ = "BufferOverflowException";
- var __BufferUnderflowException__ = "BufferUnderflowException";
- var __IndexOutOfBoundsException__ = "IndexOutOfBoundsException";
-
- //
- // Buffer implementation to be used by web browsers, it uses ArrayBuffer as
- // the store.
- //
-
- require("Ice/Class");
- require("Ice/Long");
-
- var Ice = global.Ice || Ice;
- var Long = Ice.Long;
+ result[i] = b[i + start];
+ }
+ return result.buffer;
+ };
+}
- var Buffer = Ice.Class({
- __init__: function(buffer)
+var __BufferOverflowException__ = "BufferOverflowException";
+var __BufferUnderflowException__ = "BufferUnderflowException";
+var __IndexOutOfBoundsException__ = "IndexOutOfBoundsException";
+
+//
+// Buffer implementation to be used by web browsers, it uses ArrayBuffer as
+// the store.
+//
+var Buffer = Ice.Class({
+ __init__: function(buffer)
+ {
+ if(buffer !== undefined)
{
- if(buffer !== undefined)
- {
- this.b = buffer;
- this.v = new DataView(this.b);
- }
- else
- {
- this.b = null; // ArrayBuffer
- this.v = null; // DataView
- }
- this._position = 0;
- this._limit = 0;
- this._shrinkCounter = 0;
- },
- empty: function()
+ this.b = buffer;
+ this.v = new DataView(this.b);
+ }
+ else
{
- return this._limit === 0;
- },
- resize: function(n)
+ this.b = null; // ArrayBuffer
+ this.v = null; // DataView
+ }
+ this._position = 0;
+ this._limit = 0;
+ this._shrinkCounter = 0;
+ },
+ empty: function()
+ {
+ return this._limit === 0;
+ },
+ resize: function(n)
+ {
+ if(n === 0)
{
- if(n === 0)
- {
- this.clear();
- }
- else if(n > this.capacity)
- {
- this.reserve(n);
- }
- this._limit = n;
- },
- clear: function()
- {
- this.b = null;
- this.v = null;
- this._position = 0;
- this._limit = 0;
- },
- //
- // Call expand(n) to add room for n additional bytes. Note that expand()
- // examines the current position of the buffer first; we don't want to
- // expand the buffer if the caller is writing to a location that is
- // already in the buffer.
- //
- expand: function(n)
+ this.clear();
+ }
+ else if(n > this.capacity)
{
- var sz = this.capacity === 0 ? n : this._position + n;
- if(sz > this._limit)
- {
- this.resize(sz);
- }
- },
- reset: function()
+ this.reserve(n);
+ }
+ this._limit = n;
+ },
+ clear: function()
+ {
+ this.b = null;
+ this.v = null;
+ this._position = 0;
+ this._limit = 0;
+ },
+ //
+ // Call expand(n) to add room for n additional bytes. Note that expand()
+ // examines the current position of the buffer first; we don't want to
+ // expand the buffer if the caller is writing to a location that is
+ // already in the buffer.
+ //
+ expand: function(n)
+ {
+ var sz = this.capacity === 0 ? n : this._position + n;
+ if(sz > this._limit)
{
- if(this._limit > 0 && this._limit * 2 < this.capacity)
- {
- //
- // If the current buffer size is smaller than the
- // buffer capacity, we shrink the buffer memory to the
- // current size. This is to avoid holding on to too much
- // memory if it's not needed anymore.
- //
- if(++this._shrinkCounter > 2)
- {
- this.reserve(this._limit);
- this._shrinkCounter = 0;
- }
- }
- else
+ this.resize(sz);
+ }
+ },
+ reset: function()
+ {
+ if(this._limit > 0 && this._limit * 2 < this.capacity)
+ {
+ //
+ // If the current buffer size is smaller than the
+ // buffer capacity, we shrink the buffer memory to the
+ // current size. This is to avoid holding on to too much
+ // memory if it's not needed anymore.
+ //
+ if(++this._shrinkCounter > 2)
{
+ this.reserve(this._limit);
this._shrinkCounter = 0;
}
- this._limit = 0;
- this._position = 0;
- },
- reserve: function(n)
+ }
+ else
{
- if(n > this.capacity)
- {
- var capacity = Math.max(n, 2 * this.capacity);
- capacity = Math.max(1024, capacity);
- if(!this.b)
- {
- this.b = new ArrayBuffer(capacity);
- }
- else
- {
- var b = new Uint8Array(capacity);
- b.set(new Uint8Array(this.b));
- this.b = b.buffer;
- }
- this.v = new DataView(this.b);
- }
- else if(n < this.capacity)
+ this._shrinkCounter = 0;
+ }
+ this._limit = 0;
+ this._position = 0;
+ },
+ reserve: function(n)
+ {
+ if(n > this.capacity)
+ {
+ var capacity = Math.max(n, 2 * this.capacity);
+ capacity = Math.max(1024, capacity);
+ if(!this.b)
{
- this.b = this.b.slice(0, this.capacity);
- this.v = new DataView(this.b);
+ this.b = new ArrayBuffer(capacity);
}
else
{
- return;
+ var b = new Uint8Array(capacity);
+ b.set(new Uint8Array(this.b));
+ this.b = b.buffer;
}
- },
- put: function(v)
+ this.v = new DataView(this.b);
+ }
+ else if(n < this.capacity)
{
- if(this._position === this._limit)
- {
- throw new Error(__BufferOverflowException__);
- }
- this.v.setUint8(this._position, v);
- this._position++;
- },
- putAt: function(i, v)
+ this.b = this.b.slice(0, this.capacity);
+ this.v = new DataView(this.b);
+ }
+ else
{
- if(i >= this._limit)
- {
- throw new Error(__IndexOutOfBoundsException__);
- }
- this.v.setUint8(i, v);
- },
- putArray: function(v)
+ return;
+ }
+ },
+ put: function(v)
+ {
+ if(this._position === this._limit)
{
- if(v.byteLength > 0)
- {
- //Expects an Uint8Array
- if(this._position + v.length > this._limit)
- {
- throw new Error(__BufferOverflowException__);
- }
- new Uint8Array(this.b, 0, this.b.byteLength).set(v, this._position);
- this._position += v.byteLength;
- }
- },
- putShort: function(v)
+ throw new Error(__BufferOverflowException__);
+ }
+ this.v.setUint8(this._position, v);
+ this._position++;
+ },
+ putAt: function(i, v)
+ {
+ if(i >= this._limit)
{
- if(this._position + 2 > this._limit)
- {
- throw new Error(__BufferOverflowException__);
- }
- this.v.setInt16(this._position, v, true);
- this._position += 2;
- },
- putInt: function(v)
+ throw new Error(__IndexOutOfBoundsException__);
+ }
+ this.v.setUint8(i, v);
+ },
+ putArray: function(v)
+ {
+ if(v.byteLength > 0)
{
- if(this._position + 4 > this._limit)
+ //Expects an Uint8Array
+ if(this._position + v.length > this._limit)
{
throw new Error(__BufferOverflowException__);
- }
- this.v.setInt32(this._position, v, true);
- this._position += 4;
- },
- putIntAt: function(i, v)
+ }
+ new Uint8Array(this.b, 0, this.b.byteLength).set(v, this._position);
+ this._position += v.byteLength;
+ }
+ },
+ putShort: function(v)
+ {
+ if(this._position + 2 > this._limit)
{
- if(i + 4 > this._limit || i < 0)
- {
- throw new Error(__IndexOutOfBoundsException__);
- }
- this.v.setInt32(i, v, true);
- },
- putFloat: function(v)
+ throw new Error(__BufferOverflowException__);
+ }
+ this.v.setInt16(this._position, v, true);
+ this._position += 2;
+ },
+ putInt: function(v)
+ {
+ if(this._position + 4 > this._limit)
{
- if(this._position + 4 > this._limit)
- {
- throw new Error(__BufferOverflowException__);
- }
- this.v.setFloat32(this._position, v, true);
- this._position += 4;
- },
- putDouble: function(v)
+ throw new Error(__BufferOverflowException__);
+ }
+ this.v.setInt32(this._position, v, true);
+ this._position += 4;
+ },
+ putIntAt: function(i, v)
+ {
+ if(i + 4 > this._limit || i < 0)
{
- if(this._position + 8 > this._limit)
- {
- throw new Error(__BufferOverflowException__);
- }
- this.v.setFloat64(this._position, v, true);
- this._position += 8;
- },
- putLong: function(v)
+ throw new Error(__IndexOutOfBoundsException__);
+ }
+ this.v.setInt32(i, v, true);
+ },
+ putFloat: function(v)
+ {
+ if(this._position + 4 > this._limit)
{
- if(this._position + 8 > this._limit)
- {
- throw new Error(__BufferOverflowException__);
- }
- this.v.setInt32(this._position, v.low, true);
- this._position += 4;
- this.v.setInt32(this._position, v.high, true);
- this._position += 4;
- },
- writeString: function(stream, v)
+ throw new Error(__BufferOverflowException__);
+ }
+ this.v.setFloat32(this._position, v, true);
+ this._position += 4;
+ },
+ putDouble: function(v)
+ {
+ if(this._position + 8 > this._limit)
{
- //
- // Encode the string as utf8
- //
- var encoded = unescape(encodeURIComponent(v));
-
- stream.writeSize(encoded.length);
- stream.expand(encoded.length);
- this.putString(encoded, encoded.length);
- },
- putString: function(v, sz)
- {
- if(this._position + sz > this._limit)
- {
- throw new Error(__BufferOverflowException__);
- }
- for(var i = 0; i < sz; ++i)
- {
- this.v.setUint8(this._position, v.charCodeAt(i));
- this._position++;
- }
- },
- get: function()
+ throw new Error(__BufferOverflowException__);
+ }
+ this.v.setFloat64(this._position, v, true);
+ this._position += 8;
+ },
+ putLong: function(v)
+ {
+ if(this._position + 8 > this._limit)
{
- if(this._position >= this._limit)
- {
- throw new Error(__BufferUnderflowException__);
- }
- var v = this.v.getUint8(this._position);
+ throw new Error(__BufferOverflowException__);
+ }
+ this.v.setInt32(this._position, v.low, true);
+ this._position += 4;
+ this.v.setInt32(this._position, v.high, true);
+ this._position += 4;
+ },
+ writeString: function(stream, v)
+ {
+ //
+ // Encode the string as utf8
+ //
+ var encoded = unescape(encodeURIComponent(v));
+
+ stream.writeSize(encoded.length);
+ stream.expand(encoded.length);
+ this.putString(encoded, encoded.length);
+ },
+ putString: function(v, sz)
+ {
+ if(this._position + sz > this._limit)
+ {
+ throw new Error(__BufferOverflowException__);
+ }
+ for(var i = 0; i < sz; ++i)
+ {
+ this.v.setUint8(this._position, v.charCodeAt(i));
this._position++;
- return v;
- },
- getAt: function(i)
+ }
+ },
+ get: function()
+ {
+ if(this._position >= this._limit)
{
- if(i < 0 || i >= this._limit)
- {
- throw new Error(__IndexOutOfBoundsException__);
- }
- return this.v.getUint8(i);
- },
- getArray: function(length)
+ throw new Error(__BufferUnderflowException__);
+ }
+ var v = this.v.getUint8(this._position);
+ this._position++;
+ return v;
+ },
+ getAt: function(i)
+ {
+ if(i < 0 || i >= this._limit)
{
- if(this._position + length > this._limit)
- {
- throw new Error(__BufferUnderflowException__);
- }
- var buffer = this.b.slice(this._position, this._position + length);
- this._position += length;
- return new Uint8Array(buffer);
- },
- getArrayAt: function(position, length)
+ throw new Error(__IndexOutOfBoundsException__);
+ }
+ return this.v.getUint8(i);
+ },
+ getArray: function(length)
+ {
+ if(this._position + length > this._limit)
{
- if(position + length > this._limit)
- {
- throw new Error(__BufferUnderflowException__);
- }
- length = length === undefined ? (this.b.byteLength - position) : length;
- return new Uint8Array(this.b.slice(position, position + length));
- },
- getShort: function()
+ throw new Error(__BufferUnderflowException__);
+ }
+ var buffer = this.b.slice(this._position, this._position + length);
+ this._position += length;
+ return new Uint8Array(buffer);
+ },
+ getArrayAt: function(position, length)
+ {
+ if(position + length > this._limit)
{
- if(this._limit - this._position < 2)
- {
- throw new Error(__BufferUnderflowException__);
- }
- var v = this.v.getInt16(this._position, true);
- this._position += 2;
- return v;
- },
- getInt: function()
+ throw new Error(__BufferUnderflowException__);
+ }
+ length = length === undefined ? (this.b.byteLength - position) : length;
+ return new Uint8Array(this.b.slice(position, position + length));
+ },
+ getShort: function()
+ {
+ if(this._limit - this._position < 2)
{
- if(this._limit - this._position < 4)
- {
- throw new Error(__BufferUnderflowException__);
- }
- var v = this.v.getInt32(this._position, true);
- this._position += 4;
- return v;
- },
- getFloat: function()
+ throw new Error(__BufferUnderflowException__);
+ }
+ var v = this.v.getInt16(this._position, true);
+ this._position += 2;
+ return v;
+ },
+ getInt: function()
+ {
+ if(this._limit - this._position < 4)
{
- if(this._limit - this._position < 4)
- {
- throw new Error(__BufferUnderflowException__);
- }
- var v = this.v.getFloat32(this._position, true);
- this._position += 4;
- return v;
- },
- getDouble: function()
+ throw new Error(__BufferUnderflowException__);
+ }
+ var v = this.v.getInt32(this._position, true);
+ this._position += 4;
+ return v;
+ },
+ getFloat: function()
+ {
+ if(this._limit - this._position < 4)
{
- if(this._limit - this._position < 8)
- {
- throw new Error(__BufferUnderflowException__);
- }
- var v = this.v.getFloat64(this._position, true);
- this._position += 8;
- return v;
- },
- getLong: function()
+ throw new Error(__BufferUnderflowException__);
+ }
+ var v = this.v.getFloat32(this._position, true);
+ this._position += 4;
+ return v;
+ },
+ getDouble: function()
+ {
+ if(this._limit - this._position < 8)
{
- if(this._limit - this._position < 8)
- {
- throw new Error(__BufferUnderflowException__);
- }
- var v = new Long();
- v.low = this.v.getInt32(this._position, true);
- this._position += 4;
- v.high = this.v.getInt32(this._position, true);
- this._position += 4;
- return v;
- },
- getString: function(length)
- {
- if(this._position + length > this._limit)
- {
- throw new Error(__BufferUnderflowException__);
- }
-
- var data = new DataView(this.b, this._position, length);
- var s = "";
-
- for(var i = 0; i < length; ++i)
- {
- s += String.fromCharCode(data.getUint8(i));
- }
- this._position += length;
- s = decodeURIComponent(escape(s));
- return s;
+ throw new Error(__BufferUnderflowException__);
}
- });
-
- var prototype = Buffer.prototype;
-
- Object.defineProperty(prototype, "position", {
- get: function() { return this._position; },
- set: function(position){
- if(position >= 0 && position <= this._limit)
- {
- this._position = position;
- }
+ var v = this.v.getFloat64(this._position, true);
+ this._position += 8;
+ return v;
+ },
+ getLong: function()
+ {
+ if(this._limit - this._position < 8)
+ {
+ throw new Error(__BufferUnderflowException__);
}
- });
-
- Object.defineProperty(prototype, "limit", {
- get: function() { return this._limit; },
- set: function(limit){
- if(limit <= this.capacity)
- {
- this._limit = limit;
- if(this._position > limit)
- {
- this._position = limit;
- }
- }
+ var v = new Long();
+ v.low = this.v.getInt32(this._position, true);
+ this._position += 4;
+ v.high = this.v.getInt32(this._position, true);
+ this._position += 4;
+ return v;
+ },
+ getString: function(length)
+ {
+ if(this._position + length > this._limit)
+ {
+ throw new Error(__BufferUnderflowException__);
+ }
+
+ var data = new DataView(this.b, this._position, length);
+ var s = "";
+
+ for(var i = 0; i < length; ++i)
+ {
+ s += String.fromCharCode(data.getUint8(i));
}
- });
+ this._position += length;
+ s = decodeURIComponent(escape(s));
+ return s;
+ }
+});
- Object.defineProperty(prototype, "capacity", {
- get: function() { return this.b === null ? 0 : this.b.byteLength; }
- });
+var prototype = Buffer.prototype;
- Object.defineProperty(prototype, "remaining", {
- get: function() { return this._limit - this._position; }
- });
-
- //
- // Create a native buffer from an array of bytes.
- //
- Buffer.createNative = function(data)
- {
- if(data === undefined)
+Object.defineProperty(prototype, "position", {
+ get: function() { return this._position; },
+ set: function(position){
+ if(position >= 0 && position <= this._limit)
{
- return new Uint8Array(0);
+ this._position = position;
}
- else
+ }
+});
+
+Object.defineProperty(prototype, "limit", {
+ get: function() { return this._limit; },
+ set: function(limit){
+ if(limit <= this.capacity)
{
- return new Uint8Array(data);
+ this._limit = limit;
+ if(this._position > limit)
+ {
+ this._position = limit;
+ }
}
- };
+ }
+});
+
+Object.defineProperty(prototype, "capacity", {
+ get: function() { return this.b === null ? 0 : this.b.byteLength; }
+});
+
+Object.defineProperty(prototype, "remaining", {
+ get: function() { return this._limit - this._position; }
+});
+
+//
+// Create a native buffer from an array of bytes.
+//
+Buffer.createNative = function(data)
+{
+ if(data === undefined)
+ {
+ return new Uint8Array(0);
+ }
+ else
+ {
+ return new Uint8Array(data);
+ }
+};
- Ice.Buffer = Buffer;
- global.Ice = Ice;
-}(typeof (global) === "undefined" ? window : global));
+Ice.Buffer = Buffer;