Skip to content

Commit 890a56f

Browse files
committed
Add writer support.
1 parent 77d9f5d commit 890a56f

File tree

10 files changed

+337
-114
lines changed

10 files changed

+337
-114
lines changed

.idea/jsLibraryMappings.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/watcherTasks.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 2 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,3 @@
1-
const assert = require('assert');
1+
const BufferReader = require('./src/reader')
22

3-
module.exports = class BufferReader {
4-
constructor(buffer) {
5-
assert(Buffer.isBuffer(buffer), 'Invalid buffer.');
6-
this.buffer = buffer;
7-
this.offset = 0;
8-
}
9-
10-
seek(offset, fromBeginning = true) {
11-
if(!fromBeginning) {
12-
offset += this.offset;
13-
}
14-
15-
this._checkOffsetInRange(offset);
16-
this.offset = offset;
17-
}
18-
19-
nextDoubleLE() {
20-
return this._nextXX('DoubleLE', 8);
21-
}
22-
23-
nextDoubleBE() {
24-
return this._nextXX('DoubleBE', 8);
25-
}
26-
27-
nextFloatLE() {
28-
return this._nextXX('FloatLE', 4);
29-
}
30-
31-
nextFloatBE() {
32-
return this._nextXX('FloatBE', 4);
33-
}
34-
35-
nextInt32LE() {
36-
return this._nextXX('Int32LE', 4);
37-
}
38-
39-
nextInt32BE() {
40-
return this._nextXX('Int32BE', 4);
41-
}
42-
43-
nextUInt32LE() {
44-
return this._nextXX('UInt32LE', 4);
45-
}
46-
47-
nextUInt32BE() {
48-
return this._nextXX('UInt32BE', 4);
49-
}
50-
51-
nextUInt16LE() {
52-
return this._nextXX('UInt16LE', 2);
53-
}
54-
55-
nextUInt16BE() {
56-
return this._nextXX('UInt16BE', 2);
57-
}
58-
59-
nextInt16LE() {
60-
return this._nextXX('Int16LE', 2);
61-
}
62-
63-
nextInt16BE() {
64-
return this._nextXX('Int16BE', 2);
65-
}
66-
67-
nextUInt8() {
68-
return this._nextXX('UInt8', 1);
69-
}
70-
71-
nextInt8() {
72-
return this._nextXX('Int8', 1);
73-
}
74-
75-
nextBuffer(length) {
76-
this._checkPositive(length);
77-
this._checkOffsetInRange(this.offset + length);
78-
79-
const buffer = this.buffer.slice(this.offset, this.offset + length);
80-
this.offset += length;
81-
return buffer;
82-
}
83-
84-
nextString(length, encoding = 'utf-8') {
85-
this._checkPositive(length);
86-
this._checkOffsetInRange(this.offset + length);
87-
const str = this.buffer.toString(encoding, this.offset, this.offset + length);
88-
this.offset += length;
89-
return str;
90-
}
91-
92-
_nextXX(type, size) {
93-
this._checkOffsetInRange(this.offset + size, 'Offset');
94-
95-
const v = this.buffer[`read${type}`](this.offset);
96-
this.offset += size;
97-
return v;
98-
}
99-
100-
_checkPositive(number, name = 'Length') {
101-
assert(number >= 0, `${name} must be positive.`);
102-
}
103-
104-
_checkOffsetInRange(offset) {
105-
assert(offset >= 0 && offset <= this.buffer.length, 'Offset out of range.');
106-
}
107-
}
3+
module.exports = { BufferReader }

package.json

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
{
2-
"name": "ginkgoch-buffer-reader",
3-
"version": "1.0.3",
2+
"name": "@ginkgoch/buffer-io",
3+
"version": "1.0.0",
44
"main": "index.js",
5-
"homepage": "https://github.com/ginkgoch/node-buffer-reader",
5+
"homepage": "https://github.com/ginkgoch/node-buffer-io",
66
"scripts": {
77
"test": "./node_modules/.bin/jest"
88
},
99
"author": {
1010
"name": "Ginkgoch",
1111
"email": "ginkgoch@outlook.com",
12-
"url": "https://github.com/ginkgoch/node-buffer-reader"
12+
"url": "https://github.com/ginkgoch/node-buffer-io"
1313
},
1414
"keywords": [
15-
"ginkgoch", "buffer-reader", "bufferreader", "node-buffer-reader"
15+
"ginkgoch",
16+
"buffer-reader",
17+
"bufferreader",
18+
"node-buffer-reader",
19+
"buffer-writer",
20+
"bufferwriter",
21+
"node-buffer-writer"
1622
],
1723
"license": "MIT",
18-
"description": "This is a library to help to easily read Buffer instance.",
24+
"description": "This is a library to help to easily read and write Buffer instance.",
1925
"devDependencies": {
2026
"jest": "^23.4.1"
2127
},
2228
"repository": {
23-
"url": "https://github.com/ginkgoch/node-buffer-reader.git",
29+
"url": "https://github.com/ginkgoch/node-buffer-io.git",
2430
"type": "git"
2531
}
2632
}

src/reader.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
const assert = require('assert');
2+
3+
module.exports = class BufferReader {
4+
constructor(buffer) {
5+
assert(Buffer.isBuffer(buffer), 'Invalid buffer.');
6+
this.buffer = buffer;
7+
this.offset = 0;
8+
}
9+
10+
seek(offset, fromBeginning = true) {
11+
if(!fromBeginning) {
12+
offset += this.offset;
13+
}
14+
15+
this._checkOffsetInRange(offset);
16+
this.offset = offset;
17+
}
18+
19+
nextDouble() {
20+
return this.nextDoubleLE()
21+
}
22+
23+
nextDoubleLE() {
24+
return this._nextXX('DoubleLE', 8);
25+
}
26+
27+
nextDoubleBE() {
28+
return this._nextXX('DoubleBE', 8);
29+
}
30+
31+
nextFloat() {
32+
return this.nextFloat()
33+
}
34+
35+
nextFloatLE() {
36+
return this._nextXX('FloatLE', 4);
37+
}
38+
39+
nextFloatBE() {
40+
return this._nextXX('FloatBE', 4);
41+
}
42+
43+
nextInt32() {
44+
return this.nextInt32LE()
45+
}
46+
47+
nextInt32LE() {
48+
return this._nextXX('Int32LE', 4);
49+
}
50+
51+
nextInt32BE() {
52+
return this._nextXX('Int32BE', 4);
53+
}
54+
55+
nextUInt32() {
56+
return this.nextUInt32LE()
57+
}
58+
59+
nextUInt32LE() {
60+
return this._nextXX('UInt32LE', 4);
61+
}
62+
63+
nextUInt32BE() {
64+
return this._nextXX('UInt32BE', 4);
65+
}
66+
67+
nextUInt16() {
68+
return this.nextUInt16LE()
69+
}
70+
71+
nextUInt16LE() {
72+
return this._nextXX('UInt16LE', 2);
73+
}
74+
75+
nextUInt16BE() {
76+
return this._nextXX('UInt16BE', 2);
77+
}
78+
79+
nextInt16() {
80+
return this.nextInt16LE()
81+
}
82+
83+
nextInt16LE() {
84+
return this._nextXX('Int16LE', 2);
85+
}
86+
87+
nextInt16BE() {
88+
return this._nextXX('Int16BE', 2);
89+
}
90+
91+
nextUInt8() {
92+
return this._nextXX('UInt8', 1);
93+
}
94+
95+
nextInt8() {
96+
return this._nextXX('Int8', 1);
97+
}
98+
99+
nextBuffer(length) {
100+
this._checkPositive(length);
101+
this._checkOffsetInRange(this.offset + length);
102+
103+
const buffer = this.buffer.slice(this.offset, this.offset + length);
104+
this.offset += length;
105+
return buffer;
106+
}
107+
108+
nextString(length, encoding = 'utf-8') {
109+
this._checkPositive(length);
110+
this._checkOffsetInRange(this.offset + length);
111+
const str = this.buffer.toString(encoding, this.offset, this.offset + length);
112+
this.offset += length;
113+
return str;
114+
}
115+
116+
_nextXX(type, size) {
117+
this._checkOffsetInRange(this.offset + size, 'Offset');
118+
119+
const v = this.buffer[`read${type}`](this.offset);
120+
this.offset += size;
121+
return v;
122+
}
123+
124+
_checkPositive(number, name = 'Length') {
125+
assert(number >= 0, `${name} must be positive.`);
126+
}
127+
128+
_checkOffsetInRange(offset) {
129+
assert(offset >= 0 && offset <= this.buffer.length, 'Offset out of range.');
130+
}
131+
}

index.test.js renamed to src/reader.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const BufferReader = require('./index.js');
1+
const BufferReader = require('./reader.js');
22

33
describe('buffer reader tests', () => {
44
test('init test - normal', () => {

0 commit comments

Comments
 (0)