ByteBuffer
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -18,10 +18,29 @@ /*! | ||
var ByteBuffer = function (org_buf,encoding) { | ||
var ByteBuffer = function (org_buf) { | ||
var _org_buf = org_buf; | ||
var _encoding = encoding || 'utf8'; | ||
var _encoding = 'utf8'; | ||
var _offset = 0; | ||
var _list = []; | ||
var _offset = 0; | ||
var _endian = 'L'; | ||
//指定文字编码 | ||
this.encoding = function(encode){ | ||
_encoding = encode; | ||
return this; | ||
}; | ||
//指定字节序 为BigEndian | ||
this.bigEndian = function(){ | ||
_endian = 'B'; | ||
return this; | ||
}; | ||
//指定字节序 为LittleEndian | ||
this.littleEndian = function(){ | ||
_endian = 'L'; | ||
return this; | ||
}; | ||
this.byte = function(val){ | ||
@@ -40,3 +59,3 @@ if(val == undefined){ | ||
if(val == undefined){ | ||
_list.push(_org_buf.readInt16LE(_offset)); | ||
_list.push(_org_buf['readInt16'+_endian+'E'](_offset)); | ||
_offset+=2; | ||
@@ -52,3 +71,3 @@ }else{ | ||
if(val == undefined){ | ||
_list.push(_org_buf.readUInt16LE(_offset)); | ||
_list.push(_org_buf['readUInt16'+_endian+'E'](_offset)); | ||
_offset+=2; | ||
@@ -64,3 +83,3 @@ }else{ | ||
if(val == undefined){ | ||
_list.push(_org_buf.readInt32LE(_offset)); | ||
_list.push(_org_buf['readInt32'+_endian+'E'](_offset)); | ||
_offset+=4; | ||
@@ -76,3 +95,3 @@ }else{ | ||
if(val == undefined){ | ||
_list.push(_org_buf.readUInt32LE(_offset)); | ||
_list.push(_org_buf['readUInt32'+_endian+'E'](_offset)); | ||
_offset+=4; | ||
@@ -91,3 +110,3 @@ }else{ | ||
if(val == undefined){ | ||
var len = _org_buf.readInt16LE(_offset); | ||
var len = _org_buf['readInt16'+_endian+'E'](_offset); | ||
_offset+=2; | ||
@@ -129,3 +148,3 @@ _list.push(_org_buf.toString(_encoding, _offset, _offset+len)); | ||
if(val == undefined){ | ||
_list.push(_org_buf.readDoubleLE(_offset)); | ||
_list.push(_org_buf['readDouble'+_endian+'E'](_offset)); | ||
_offset+=8; | ||
@@ -141,3 +160,3 @@ }else{ | ||
if(val == undefined){ | ||
_list.push(_org_buf.readFloatLE(_offset)); | ||
_list.push(_org_buf['readFloat'+_endian+'E'](_offset)); | ||
_offset+=4; | ||
@@ -153,3 +172,3 @@ }else{ | ||
if(val == undefined){ | ||
_list.push(_org_buf.readDoubleLE(_offset)); | ||
_list.push(_org_buf['readDouble'+_endian+'E'](_offset)); | ||
_offset+=8; | ||
@@ -183,15 +202,15 @@ }else{ | ||
case Type_Short: | ||
_org_buf.writeInt16LE(_list[i].d,offset); | ||
_org_buf['writeInt16'+_endian+'E'](_list[i].d,offset); | ||
offset+=_list[i].l; | ||
break; | ||
case Type_UShort: | ||
_org_buf.writeUInt16LE(_list[i].d,offset); | ||
_org_buf['writeUInt16'+_endian+'E'](_list[i].d,offset); | ||
offset+=_list[i].l; | ||
break; | ||
case Type_Int32: | ||
_org_buf.writeInt32LE(_list[i].d,offset); | ||
_org_buf['writeInt32'+_endian+'E'](_list[i].d,offset); | ||
offset+=_list[i].l; | ||
break; | ||
case Type_UInt32: | ||
_org_buf.writeUInt32LE(_list[i].d,offset); | ||
_org_buf['writeUInt32'+_endian+'E'](_list[i].d,offset); | ||
offset+=_list[i].l; | ||
@@ -201,3 +220,3 @@ break; | ||
//前2个字节表示字符串长度 | ||
_org_buf.writeInt16LE(_list[i].l,offset); | ||
_org_buf['writeInt16'+_endian+'E'](_list[i].l,offset); | ||
offset+=2; | ||
@@ -217,11 +236,11 @@ _org_buf.write(_list[i].d,_encoding,offset); | ||
case Type_Int64: | ||
_org_buf.writeDoubleLE(_list[i].d,offset); | ||
_org_buf['writeDouble'+_endian+'E'](_list[i].d,offset); | ||
offset+=_list[i].l; | ||
break; | ||
case Type_Float: | ||
_org_buf.writeFloatLE(_list[i].d,offset); | ||
_org_buf['writeFloat'+_endian+'E'](_list[i].d,offset); | ||
offset+=_list[i].l; | ||
break; | ||
case Type_Double: | ||
_org_buf.writeDoubleLE(_list[i].d,offset); | ||
_org_buf['writeDouble'+_endian+'E'](_list[i].d,offset); | ||
offset+=_list[i].l; | ||
@@ -228,0 +247,0 @@ break; |
@@ -9,3 +9,3 @@ { | ||
"main" : "ByteBuffer.js", | ||
"version" : "0.1.1" | ||
"version" : "0.1.2" | ||
} |
23
test.js
@@ -5,6 +5,7 @@ var ByteBuffer = require('./ByteBuffer'); | ||
var sbuf = new ByteBuffer(); | ||
var buffer = sbuf.string('abc123').int32(999).uint32(11).float(0.5) | ||
.int64(9999999).double(-0.5).short(3333).ushort(354) | ||
.byte(14) | ||
.vstring('abcd',10)//定长字符串 | ||
var buffer = sbuf.string('abc123')//变长字符串,前两个字节表示长度 | ||
.int32(-999).uint32(999).float(-0.5) | ||
.int64(9999999).double(-0.000005).short(32767).ushort(65535) | ||
.byte(255) | ||
.vstring('abcd',5)//定长字符串,不足的字节补0x00 | ||
.pack(); | ||
@@ -17,8 +18,18 @@ | ||
//解包出来是一个数组 | ||
var arr = rbuf.string().int32().uint32().float() | ||
var arr = rbuf.string()//变长字符串,前两个字节表示长度 | ||
.int32().uint32().float() | ||
.int64().double().short().ushort() | ||
.byte() | ||
.vstring(null,10)//定长字符串 | ||
.vstring(null,5)//定长字符串,不足的字节补0x00 | ||
.unpack(); | ||
console.log(arr); | ||
//指定字符编码(默认:utf8):utf8/ascii/ | ||
var rbuf = new ByteBuffer(buffer).encoding('ascii'); | ||
//指定字节序(默认:LittleEndian) | ||
var rbuf = new ByteBuffer(buffer).bigEndian(); | ||
var rbuf = new ByteBuffer(buffer).littleEndian(); | ||
9234
257