|
110 | 110 | * @const
|
111 | 111 | * @expose
|
112 | 112 | */
|
113 |
| - ByteBuffer.VERSION = "2.2.0"; |
| 113 | + ByteBuffer.VERSION = "2.3.0"; |
114 | 114 |
|
115 | 115 | /**
|
116 | 116 | * Default buffer capacity of `16`. The ByteBuffer will be automatically resized by a factor of 2 if required.
|
|
195 | 195 | * ByteBuffer's offset to 0 and its length to the wrapped object's byte length.
|
196 | 196 | * @param {!ArrayBuffer|!Buffer|!{array: !ArrayBuffer}|!{buffer: !ArrayBuffer}|string} buffer Anything that can
|
197 | 197 | * be wrapped
|
198 |
| - * @param {(string|boolean)=} enc String encoding if a string is provided (hex, utf8, defaults to base64) |
| 198 | + * @param {(string|boolean)=} enc String encoding if a string is provided (hex, utf8, binary, defaults to base64) |
199 | 199 | * @param {boolean=} littleEndian `true` to use little endian multi byte values, defaults to `false` for big
|
200 | 200 | * endian.
|
201 | 201 | * @returns {!ByteBuffer}
|
|
210 | 210 | // Wrap a string
|
211 | 211 | if (typeof buffer === 'string') {
|
212 | 212 | switch (enc) {
|
213 |
| - case "hex": |
214 |
| - return ByteBuffer.decodeHex(buffer, littleEndian); |
215 | 213 | case "base64":
|
216 | 214 | return ByteBuffer.decode64(buffer, littleEndian);
|
| 215 | + case "hex": |
| 216 | + return ByteBuffer.decodeHex(buffer, littleEndian); |
| 217 | + case "binary": |
| 218 | + return ByteBuffer.decodeBinary(buffer, littleEndian); |
217 | 219 | default:
|
218 | 220 | return new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, littleEndian).writeUTF8String(buffer).flip();
|
219 | 221 | }
|
|
1615 | 1617 | ByteBuffer.calculateUTF8String = function(str) {
|
1616 | 1618 | str = ""+str;
|
1617 | 1619 | var bytes = 0;
|
1618 |
| - for (var i=0, k=str.length; i<k; i++) { |
| 1620 | + for (var i=0, k=str.length; i<k; ++i) { |
1619 | 1621 | // Does not throw since JS strings are already UTF8 encoded
|
1620 | 1622 | bytes += ByteBuffer.calculateUTF8Char(str.charCodeAt(i));
|
1621 | 1623 | }
|
|
1719 | 1721 | }
|
1720 | 1722 | if (bb.array === null) return "";
|
1721 | 1723 | var val, out = [];
|
1722 |
| - for (var i=bb.offset, k=bb.length; i<k; i++) { |
| 1724 | + for (var i=bb.offset, k=bb.length; i<k; ++i) { |
1723 | 1725 | val = bb.view.getUint8(i).toString(16).toUpperCase();
|
1724 | 1726 | if (val.length < 2) val = "0"+val;
|
1725 | 1727 | out.push(val);
|
|
1750 | 1752 | return out.flip();
|
1751 | 1753 | };
|
1752 | 1754 |
|
| 1755 | + /** |
| 1756 | + * Encodes a ByteBuffer to a binary string. A binary string in this case is a string composed of 8bit values |
| 1757 | + * as characters with a char code between 0 and 255 inclusive. |
| 1758 | + * @param {!ByteBuffer} bb ByteBuffer to encode. Will be cloned and flipped if length < offset. |
| 1759 | + * @returns {string} Binary string |
| 1760 | + * @throws {Error} If the argument is not a valid ByteBuffer |
| 1761 | + * @expose |
| 1762 | + */ |
| 1763 | + ByteBuffer.encodeBinary = function(bb) { |
| 1764 | + if (!(bb instanceof ByteBuffer)) { |
| 1765 | + bb = ByteBuffer.wrap(bb); |
| 1766 | + } else if (bb.length < bb.offset) { |
| 1767 | + bb = bb.clone().flip(); |
| 1768 | + } |
| 1769 | + var out = [], view = bb.view; |
| 1770 | + for (var i=bb.offset, k=bb.length; i<k; ++i) { |
| 1771 | + out.push(String.fromCharCode(view.getUint8(i))); |
| 1772 | + } |
| 1773 | + return out.join(''); |
| 1774 | + }; |
| 1775 | + |
| 1776 | + /** |
| 1777 | + * Decodes a binary string to a ByteBuffer.A binary string in this case is a string composed of 8bit values |
| 1778 | + * as characters with a char code between 0 and 255 inclusive. |
| 1779 | + * @param {string} str Binary string |
| 1780 | + * @param {boolean=} littleEndian `true` to use little endian byte order, defaults to `false` for big endian. |
| 1781 | + * @returns {!ByteBuffer} ByteBuffer |
| 1782 | + * @throws {Error} If the argument is not a valid binary string |
| 1783 | + * @expose |
| 1784 | + */ |
| 1785 | + ByteBuffer.decodeBinary = function(str, littleEndian) { |
| 1786 | + if (typeof str !== 'string') { |
| 1787 | + throw(new Error("Illegal argument: Not a string")); |
| 1788 | + } |
| 1789 | + var k=str.length, |
| 1790 | + dst = new ArrayBuffer(k), |
| 1791 | + view = new DataView(dst), |
| 1792 | + val; |
| 1793 | + for (var i=0; i<k; ++i) { |
| 1794 | + if ((val = str.charCodeAt(i)) > 255) throw(new Error("Illegal argument: Not a binary string (char code "+val+")")); |
| 1795 | + view.setUint8(i, val); |
| 1796 | + } |
| 1797 | + return ByteBuffer.wrap(dst, littleEndian); |
| 1798 | + }; |
1753 | 1799 |
|
1754 | 1800 | /**
|
1755 | 1801 | * Writes an UTF8 string.
|
|
1764 | 1810 | var start = offset;
|
1765 | 1811 | var encLen = ByteBuffer.calculateUTF8String(str); // See [1]
|
1766 | 1812 | this.ensureCapacity(offset+encLen);
|
1767 |
| - for (var i=0, j=str.length; i<j; i++) { |
| 1813 | + for (var i=0, j=str.length; i<j; ++i) { |
1768 | 1814 | // [1] Does not throw since JS strings are already UTF8 encoded
|
1769 | 1815 | offset += ByteBuffer.encodeUTF8Char(str.charCodeAt(i), this, offset);
|
1770 | 1816 | }
|
|
1789 | 1835 | var advance = typeof offset === 'undefined';
|
1790 | 1836 | offset = typeof offset !== 'undefined' ? offset : this.offset;
|
1791 | 1837 | var dec, result = "", start = offset;
|
1792 |
| - for (var i=0; i<chars; i++) { |
| 1838 | + for (var i=0; i<chars; ++i) { |
1793 | 1839 | dec = ByteBuffer.decodeUTF8Char(this, offset);
|
1794 | 1840 | offset += dec["length"];
|
1795 | 1841 | result += String.fromCharCode(dec["char"]);
|
|
2042 | 2088 | } else {
|
2043 | 2089 | out += " ";
|
2044 | 2090 | }
|
2045 |
| - for (var i=0, k=this.array.byteLength; i<k; i++) { |
| 2091 | + for (var i=0, k=this.array.byteLength; i<k; ++i) { |
2046 | 2092 | if (i>0 && i%wrap == 0) {
|
2047 | 2093 | while (out.length < 3*wrap+1) out += " "; // Make it equal to maybe show something on the right
|
2048 | 2094 | lines.push(out);
|
|
2065 | 2111 | lines.push(out);
|
2066 | 2112 | }
|
2067 | 2113 | // Make it equal
|
2068 |
| - for (i=0, k=lines.length; i<k; i++) { |
| 2114 | + for (i=0, k=lines.length; i<k; ++i) { |
2069 | 2115 | while (lines[i].length < 3*wrap+1) lines[i] += " "; // Make it equal to maybe show something on the right
|
2070 | 2116 | }
|
2071 | 2117 |
|
2072 | 2118 | // Right column: ASCII, using dots for (usually) non-printable characters
|
2073 | 2119 | var n = 0;
|
2074 | 2120 | out = "";
|
2075 |
| - for (i=0, k=this.array.byteLength; i<k; i++) { |
| 2121 | + for (i=0, k=this.array.byteLength; i<k; ++i) { |
2076 | 2122 | if (i>0 && i%wrap == 0) {
|
2077 | 2123 | lines[n] += " "+out;
|
2078 | 2124 | out = ""; n++;
|
|
2112 | 2158 | view = this.view,
|
2113 | 2159 | i, k;
|
2114 | 2160 | if (!debug) {
|
2115 |
| - if (this.array === null) return ""; |
2116 |
| - for (i=this.offset, k=this.length; i<k; i++) { |
2117 |
| - val = view.getUint8(i).toString(16).toUpperCase(); |
2118 |
| - if (val.length < 2) val = "0"+val; |
2119 |
| - out += val; |
2120 |
| - } |
2121 |
| - return out; |
| 2161 | + return ByteBuffer.encodeHex(this); |
2122 | 2162 | } else {
|
2123 | 2163 | if (this.array === null) return "DESTROYED";
|
2124 | 2164 | if (this.offset == 0 && this.length == 0) {
|
|
2130 | 2170 | } else {
|
2131 | 2171 | out += " ";
|
2132 | 2172 | }
|
2133 |
| - for (i=0, k=this.array.byteLength; i<k; i++) { |
| 2173 | + for (i=0, k=this.array.byteLength; i<k; ++i) { |
2134 | 2174 | val = view.getUint8(i).toString(16).toUpperCase();
|
2135 | 2175 | if (val.length < 2) val = "0"+val;
|
2136 | 2176 | out += val;
|
|
2148 | 2188 | }
|
2149 | 2189 | };
|
2150 | 2190 |
|
| 2191 | + /** |
| 2192 | + * Returns the ByteBuffer's contents between offset and length as a binary string. A binary string in this case |
| 2193 | + * is a string composed of 8bit values as characters with a char code between 0 and 255 inclusive. |
| 2194 | + * @returns {string} Binary string |
| 2195 | + * @expose |
| 2196 | + */ |
| 2197 | + ByteBuffer.prototype.toBinary = function() { |
| 2198 | + return ByteBuffer.encodeBinary(this); |
| 2199 | + }; |
| 2200 | + |
2151 | 2201 | /**
|
2152 | 2202 | * Returns the base64 encoded representation of the ByteBuffer's contents.
|
2153 | 2203 | * @returns {string} Base 64 encoded string
|
|
2171 | 2221 | /**
|
2172 | 2222 | * Converts the ByteBuffer to a string.
|
2173 | 2223 | * @param {string=} enc Output encoding. Returns an informative string representation by default but also allows
|
2174 |
| - * direct conversion to "utf8", "hex" and "base64" encoding. "debug" returns a hex representation with marked |
2175 |
| - * offsets. |
| 2224 | + * direct conversion to "utf8", "hex", "base64" and "binary" encoding. "debug" returns a hex representation with |
| 2225 | + * marked offsets. |
2176 | 2226 | * @returns {string} String representation
|
2177 | 2227 | * @expose
|
2178 | 2228 | */
|
|
2185 | 2235 | return this.toBase64();
|
2186 | 2236 | case "hex":
|
2187 | 2237 | return this.toHex();
|
| 2238 | + case "binary": |
| 2239 | + return this.toBinary(); |
2188 | 2240 | case "debug":
|
2189 | 2241 | return this.toHex(true);
|
2190 | 2242 | default:
|
|
0 commit comments