关于javascript前端开发之实现二进制读写操作的相关介绍,请看以下内容详解,本文介绍的非常详细,具有参考价值。
由于种种原因,在浏览器中无法像nodejs那样操作二进制。
最近写了一个在浏览器端操作读写二进制的帮助类
!function (entrance) {
"use strict";
if ("object" === typeof exports && "undefined" !== typeof module) {
module.exports = entrance();
} else if ("function" === typeof define && define.amd) {
define([], entrance());
} else {
var f;
if ("undefined" !== typeof window) {
f = window;
} else {
throw new Error('wrong execution environment');
}
f.TinyStream = entrance();
}
}(function () {
var binaryPot = {
init: function (array) {
for (var i = 0; i < array.length; i++) {
array[i] *= 1;
if (array[i] < 0) {
array[i] += 256
}
if(array[i]>255){
throw new Error('不合法字节流')
}
}
return array;
},
writeUTF: function (str, isGetBytes) {
var back = [],
byteSize = 0;
for (var i = 0; i < str.length; i++) {
var code = str.charCodeAt(i);
if (code >= 0 && code <= 127) {
byteSize += 1;
back.push(code);
} else if (code >= 128 && code <= 2047) {
byteSize += 2;
back.push((192 | (31 & (code >> 6))));
back.push((128 | (63 & code)))
} else if (code >= 2048 && code <= 65535) {
byteSize += 3;
back.push((224 | (15 & (code >> 12))));
back.push((128 | (63 & (code >> 6))));
back.push((128 | (63 & code)))
}
}
for (i = 0; i < back.length; i++) {
if (back[i] > 255) {
back[i] &= 255
}
}
if (isGetBytes) {
return back
}
if (byteSize <= 255) {
return [0, byteSize].concat(back);
} else {
return [byteSize >> 8, byteSize & 255].concat(back);
}
},
readUTF: function (arr) {
if (Object.prototype.toString.call(arr) == "[object String]") {
return arr;
}
var UTF = "",
_arr = this.init(arr);
for (var i = 0; i < _arr.length; i++) {
var one = _arr[i].toString(2),
v = one.match(/^1+?(?=0)/);
if (v && one.length == 8) {
var bytesLength = v[0].length,
store = _arr[i].toString(2).slice(7 - bytesLength);
for (var st = 1; st < bytesLength; st++) {
store += _arr[st + i].toString(2).slice(2)
}
UTF += String.fromCharCode(parseInt(store, 2));
i += bytesLength - 1
} else {
UTF += String.fromCharCode(_arr[i])
}
}
return UTF
},
convertStream: function (x) {
if (x instanceof Stream) {
return x
} else {
return new Stream(x)
}
},
toMQttString: function (str) {
return this.writeUTF(str)
}
};
function baseRead(m, i, a) {
var t = a ? a : [];
for (var start = 0; start < i; start++) {
t[start] = m.pool[m.position++]
}
return t
}
var supportArrayBuffer = (function () {
return !!window.ArrayBuffer;
})();
function Stream(array) {
if (!(this instanceof Stream)) {
return new Stream(array);
}
this.pool = [];
if (Object.prototype.toString.call(array) === '[object Array]') {
this.pool = binaryPot.init(array);
} else if (Object.prototype.toString.call(array) == "[object ArrayBuffer]") {
var arr = new Int8Array(array);
this.pool = binaryPot.init([].slice.call(arr));
} else if (typeof array === 'string') {
this.pool = binaryPot.writeUTF(array);
}
var self = this;
//当前流执行的起始位置
this.position = 0;
//当前流写入的多少字节
this.writen = 0;
//返回当前流执行的起始位置是否已经大于整个流的长度
this.check = function () {
return self.position >= self.pool.length
};
}
Stream.parse = function (x) {
return binaryPot.convertStream(x);
};
Stream.prototype = {
readInt: function () {
if (this.check()) {
return -1
}
var end = "";
for (var i = 0; i < 4; i++) {
end += this.pool[this.position++].toString(16)
}
return parseInt(end, 16);
},
readByte: function () {
if (this.check()) {
return -1
}
var val = this.pool[this.position++];
if (val > 255) {
val &= 255;
}
return val;
},
read: function (bytesArray) {
if (this.check()) {
return -1
}
if (bytesArray) {
return baseRead(this, bytesArray.length | 0, bytesArray)
} else {
return this.readByte();
}
},
readUTF: function () {
var big = (this.readByte() << 8) | this.readByte();
return binaryPot.readUTF(this.pool.slice(this.position, this.position += big));
},
write: function (_byte) {
var b = _byte;
if (Object.prototype.toString.call(b).toLowerCase() == "[object array]") {
[].push.apply(this.pool, b);
this.writen += b.length;
} else {
if (+b == b) {
if (b > 255) {
b &= 255;
}
this.pool.push(b);
this.writen++
}
}
return b
},
writeChar: function (v) {
if (+v != v) {
throw new Error("writeChar:arguments type is error")
}
this.write((v >> 8) & 255);
this.write(v & 255);
this.writen += 2
},
writeUTF: function (str) {
var val = binaryPot.writeUTF(str);
[].push.apply(this.pool, val);
this.writen += val.length;
},
toComplements: function () {
var _tPool = this.pool;
for (var i = 0; i < _tPool.length; i++) {
if (_tPool[i] > 128) {
_tPool[i] -= 256
}
}
return _tPool
},
getBytesArray: function (isCom) {
if (isCom) {
return this.toComplements()
}
return this.pool
},
toArrayBuffer: function () {
if (supportArrayBuffer) {
return new ArrayBuffer(this.getBytesArray());
} else {
throw new Error('not support arraybuffer');
}
},
clear: function () {
this.pool = [];
this.writen = this.position = 0;
}
};
return Stream;
});
如何使用?
以后,我可以不用为浏览器段处理二进制而发愁了!!!希望本文分享对大家学习javascript二进制相关知识有所帮助。



