New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ByteBuffer

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ByteBuffer - npm Package Compare versions

Comparing version 0.1.3 to 0.2.0

91

ByteBuffer.js

@@ -18,7 +18,12 @@ /*!

var ByteBuffer = function (org_buf) {
/*
* 构造方法
* @param org_buf 需要解包的二进制
* @param offset 指定数据在二进制的初始位置 默认是0
*/
var ByteBuffer = function (org_buf,offset) {
var _org_buf = org_buf;
var _encoding = 'utf8';
var _offset = 0;
var _offset = offset || 0;
var _list = [];

@@ -46,8 +51,8 @@ var _offset = 0;

this.byte = function(val){
if(val == undefined){
this.byte = function(val,index){
if(val == undefined || val == null){
_list.push(_org_buf.readUInt8(_offset));
_offset+=1;
}else{
_list.push({t:Type_Byte,d:val,l:1});
_list.splice(index != undefined ? index : _list.length,0,{t:Type_Byte,d:val,l:1});
_offset += 1;

@@ -58,8 +63,8 @@ }

this.short = function(val){
if(val == undefined){
this.short = function(val,index){
if(val == undefined || val == null){
_list.push(_org_buf['readInt16'+_endian+'E'](_offset));
_offset+=2;
}else{
_list.push({t:Type_Short,d:val,l:2});
_list.splice(index != undefined ? index : _list.length,0,{t:Type_Short,d:val,l:2});
_offset += 2;

@@ -70,8 +75,8 @@ }

this.ushort = function(val){
if(val == undefined){
this.ushort = function(val,index){
if(val == undefined || val == null){
_list.push(_org_buf['readUInt16'+_endian+'E'](_offset));
_offset+=2;
}else{
_list.push({t:Type_UShort,d:val,l:2});
_list.splice(index != undefined ? index : _list.length,0,{t:Type_UShort,d:val,l:2});
_offset += 2;

@@ -82,8 +87,8 @@ }

this.int32 = function(val){
if(val == undefined){
this.int32 = function(val,index){
if(val == undefined || val == null){
_list.push(_org_buf['readInt32'+_endian+'E'](_offset));
_offset+=4;
}else{
_list.push({t:Type_Int32,d:val,l:4});
_list.splice(index != undefined ? index : _list.length,0,{t:Type_Int32,d:val,l:4});
_offset += 4;

@@ -94,8 +99,8 @@ }

this.uint32 = function(val){
if(val == undefined){
this.uint32 = function(val,index){
if(val == undefined || val == null){
_list.push(_org_buf['readUInt32'+_endian+'E'](_offset));
_offset+=4;
}else{
_list.push({t:Type_UInt32,d:val,l:4});
_list.splice(index != undefined ? index : _list.length,0,{t:Type_UInt32,d:val,l:4});
_offset += 4;

@@ -109,4 +114,4 @@ }

**/
this.string = function(val){
if(val == undefined){
this.string = function(val,index){
if(val == undefined || val == null){
var len = _org_buf['readInt16'+_endian+'E'](_offset);

@@ -119,3 +124,3 @@ _offset+=2;

if(val)len = Buffer.byteLength(val, _encoding);
_list.push({t:Type_String,d:val,l:len});
_list.splice(index != undefined ? index : _list.length,0,{t:Type_String,d:val,l:len});
_offset += len + 2;

@@ -129,3 +134,3 @@ }

**/
this.vstring = function(val,len){
this.vstring = function(val,len,index){
if(!len){

@@ -143,3 +148,3 @@ throw new Error('vstring must got len argument');

}else{
_list.push({t:Type_VString,d:val,l:len});
_list.splice(index != undefined ? index : _list.length,0,{t:Type_VString,d:val,l:len});
_offset += len;

@@ -150,8 +155,8 @@ }

this.int64 = function(val){
if(val == undefined){
this.int64 = function(val,index){
if(val == undefined || val == null){
_list.push(_org_buf['readDouble'+_endian+'E'](_offset));
_offset+=8;
}else{
_list.push({t:Type_Int64,d:val,l:8});
_list.splice(index != undefined ? index : _list.length,0,{t:Type_Int64,d:val,l:8});
_offset += 8;

@@ -162,8 +167,8 @@ }

this.float = function(val){
if(val == undefined){
this.float = function(val,index){
if(val == undefined || val == null){
_list.push(_org_buf['readFloat'+_endian+'E'](_offset));
_offset+=4;
}else{
_list.push({t:Type_Float,d:val,l:4});
_list.splice(index != undefined ? index : _list.length,0,{t:Type_Float,d:val,l:4});
_offset += 4;

@@ -174,8 +179,8 @@ }

this.double = function(val){
if(val == undefined){
this.double = function(val,index){
if(val == undefined || val == null){
_list.push(_org_buf['readDouble'+_endian+'E'](_offset));
_offset+=8;
}else{
_list.push({t:Type_Double,d:val,l:8});
_list.splice(index != undefined ? index : _list.length,0,{t:Type_Double,d:val,l:8});
_offset += 8;

@@ -195,6 +200,11 @@ }

* 打包成二进制
* @param ifHead 是否在前面加上2个字节表示包长
**/
this.pack = function(){
_org_buf = new Buffer(_offset);
this.pack = function(ifHead){
_org_buf = new Buffer((ifHead)?_offset+2:_offset);
var offset = 0;
if(ifHead){
_org_buf['writeUInt16'+_endian+'E'](_offset,offset);
offset+=2;
}
for (var i = 0; i < _list.length; i++) {

@@ -256,15 +266,2 @@ switch(_list[i].t){

module.exports = exports = ByteBuffer;
/****************************************************************
//压包
var sbuf = new ByteBuffer();
var buffer = sbuf.string('abc123').int32(999).float(0.5).int64(9999999).double(-0.5).pack();
console.log(buffer);
//解包
var rbuf = new ByteBuffer(buffer);
var arr = rbuf.string().int32().float().int64().double().unpack();
console.log(arr);
****************************************************************/
module.exports = exports = ByteBuffer;

@@ -9,3 +9,3 @@ {

"main" : "ByteBuffer.js",
"version" : "0.1.3"
"version" : "0.2.0"
}
var ByteBuffer = require('./ByteBuffer');
/*************************基本操作****************************/

@@ -11,3 +12,3 @@ //压包操作

.vstring('abcd',5)//定长字符串,不足的字节补0x00
.pack();
.pack();//结尾调用打包方法

@@ -24,3 +25,3 @@ console.log(buffer);

.vstring(null,5)//定长字符串,不足的字节补0x00
.unpack();
.unpack();//结尾调用解包方法

@@ -30,7 +31,18 @@ console.log(arr);

/*************************更多操作****************************/
//指定字符编码(默认:utf8):utf8/ascii/
var sbuf = new ByteBuffer().encoding('ascii');
//指定字节序(默认:LittleEndian)
var sbuf = new ByteBuffer().encoding('ascii').bigEndian().string('abc123');
var rbuf = new ByteBuffer(sbuf.pack()).encoding('ascii').bigEndian();
console.log(rbuf.string().unpack());
var sbuf = new ByteBuffer().bigEndian();
//指定数据在二进制的初始位置 默认是0
var sbuf = new ByteBuffer(buffer,2);
//插入数据到指定位置
var sbuf = new ByteBuffer();
sbuf.int32(9999,0);//把这个int32数据插入到ByteBuffer的第一个位置
//在打包的时候在开始位置插入一个short型表示包长(通信层中的包头)
var buffer = sbuf.pack(true);
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc