-
Notifications
You must be signed in to change notification settings - Fork 2
/
Item.js
53 lines (44 loc) · 1.33 KB
/
Item.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const Format = require('./Format')
class Item {
constructor (format, value) {
this.Format = format
this.Value = value
let arr = Buffer.from(value)
let byteLength = Buffer.byteLength(arr)
let encoded = this.encode(byteLength)
let result = encoded.result
let headerLength = encoded.initialBytes
arr.copy(result, headerLength, 0, byteLength)
this.data = result
}
encode (valueCount) {
if (valueCount <= 0xff) {
// 1 byte
let result = Buffer.alloc(valueCount + 2)
result[0] = (this.Format | 1)
result[1] = valueCount
// console.log("res[0]", result[0])
// console.log("res[1]", result[1])
return { result: result, initialBytes: 2 }
}
if (valueCount <= 0xffff) {
// 2 bytes
let result = Buffer.alloc(valueCount + 3)
result[0] = (this.Format | 2)
result[1] = this.Value
return { result: result, initialBytes: 3 }
}
if (valueCount <= 0xffffff) {
// 3 bytes
let result = Buffer.alloc(valueCount + 4)
result[0] = (this.Format | 3)
result[1] = this.Value
return { result: result, initialBytes: 4 }
}
}
}
// let num = 1337
// let buf = new Buffer(4)
// buf.writeInt32BE(num)
// const item = new Item(Format.I4, buf)
module.exports = Item