1 jls.loader.provide('jls.lang.Buffer'); 2 3 jls.loader.require('jls.lang.IllegalArgumentException'); 4 jls.loader.require('jls.lang.BufferOverflowException'); 5 jls.loader.require('jls.lang.BufferUnderflowException'); 6 7 /** 8 * @class A container for data of a specific primitive type. 9 * @see jls.lang.ByteBuffer 10 * @see jls.lang.CharBuffer 11 */ 12 jls.lang.Buffer = jls.lang.Class.create(/** @lends jls.lang.Buffer.prototype */ 13 { 14 initialize : function(capacity, limit, position, offset) { 15 this._capacity = (typeof capacity != 'undefined') ? capacity : 0; 16 this._limit = (typeof limit != 'undefined') ? limit : this._capacity; 17 this._position = position || 0; 18 this._offset = offset || 0; 19 }, 20 /** 21 * Returns this buffer's offset into the native byte array. 22 * 23 * @returns {Number} The offset of this buffer. 24 */ 25 getOffset : function() { 26 return this._offset; 27 }, 28 /** 29 * Returns the byte array position, considering the offset. 30 * 31 * @returns {Number} The position of the byte array. 32 */ 33 offset : function() { 34 return this._offset + this._position; 35 }, 36 /** 37 * Clears this buffer, setting the position to zero and the limit to the capacity. 38 * 39 * @returns {jls.lang.Buffer} This buffer. 40 */ 41 clear : function() { 42 this._position = 0; 43 this._limit = this._capacity; 44 return this; 45 }, 46 /** 47 * Flips this buffer, setting the limit to the position and the position to zero. 48 * 49 * @returns {jls.lang.Buffer} This buffer. 50 */ 51 flip : function() { 52 this._limit = this._position; 53 this._position = 0; 54 return this; 55 }, 56 /** 57 * Returns this buffer's position. 58 * 59 * @returns {Number} The position of this buffer. 60 */ 61 position : function() { 62 return this._position; 63 }, 64 /** 65 * Returns this buffer's capacity. 66 * 67 * @returns {Number} The capacity of this buffer. 68 */ 69 capacity : function() { 70 return this._capacity; 71 }, 72 /** 73 * Returns this buffer's limit. 74 * 75 * @returns {Number} The limit of this buffer. 76 */ 77 limit : function() { 78 return this._limit; 79 }, 80 /** 81 * Returns this buffer's remaining. 82 * 83 * @returns {Number} The remaining of this buffer. 84 */ 85 remaining : function() { 86 return this._limit - this._position; 87 }, 88 /** 89 * Increments this buffer's position and then returns the resulting position. 90 * 91 * @param {Number} d The delta to increment. 92 * @returns {Number} The position of this buffer. 93 */ 94 incrementPosition : function(d) { 95 if (this._position + d > this._limit) { 96 throw new jls.lang.IllegalArgumentException(); 97 } 98 this._position += d; 99 return this._position; 100 }, 101 /** 102 * Sets this buffer's position. 103 * 104 * @param {Number} position The position to set. 105 * @returns {jls.lang.Buffer} This buffer. 106 */ 107 setPosition : function(position) { 108 if (position > this._limit) { 109 throw new jls.lang.IllegalArgumentException(); 110 } 111 this._position = position; 112 return this; 113 }, 114 /** 115 * Sets this buffer's limit. 116 * 117 * @param {Number} limit The limit to set. 118 * @returns {jls.lang.Buffer} This buffer. 119 */ 120 setLimit : function(limit) { 121 if (limit > this._capacity) { 122 throw new jls.lang.IllegalArgumentException(); 123 } 124 this._limit = limit; 125 return this; 126 } 127 }); 128 129