-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbufferreader.js
More file actions
178 lines (159 loc) · 4.99 KB
/
bufferreader.js
File metadata and controls
178 lines (159 loc) · 4.99 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
module.exports = function(){
/**
* Create a new BufferReader
* @constructor
* @param {ArrayBuffer} buffer - An ArrayBuffer object for storing data
* @param {Number} [offset=0] - The starting offset
* @param {Boolean} [littleEndian=false] - Set endianness
*/
function BufferReader(buffer, offset, littleEndian){
this.offset = offset || 0;
this.littleEndian = littleEndian || false;
this.buffer = new DataView(buffer);
this.size = buffer.byteLength;
}
/**
* Read a null-terminated string of UTF-16 characters at the current offset <br>
* Increase offset by the 2 * (length of string + 1)
* @returns {String} The null-terminated string of UTF-16 characters read
*/
BufferReader.prototype.readString = function(){
var result = "";
var charCode;
while((charCode = this.readUint16()) !== 0){
result += String.fromCharCode(charCode);
}
return result;
};
/**
* Read a single UTF-16 character at the current offset <br>
* Increase offset by 2
* @returns {Char} The UTF-16 character read
*/
BufferReader.prototype.readChar = function(){
return String.fromCharCode(this.readUint16());
};
/**
* Read a signed 8-bit integer at the current offset <br>
* Increase offset by 1
* @returns {Number} The signed 8-bit integer read
*/
BufferReader.prototype.readInt8 = function(){
var result = this.buffer.getInt8(this.offset);
this.offset += 1;
return result;
};
/**
* Read an unsigned 8-bit integer at the current offset <br>
* Increase offset by 1
* @returns {Number} The unsigned 8-bit integer read
*/
BufferReader.prototype.readUint8 = function(){
var result = this.buffer.getUint8(this.offset);
this.offset += 1;
return result;
};
/**
* Read a signed 16-bit integer at the current offset <br>
* Increase offset by 2
* @returns {Number} The signed 16-bit integer read
*/
BufferReader.prototype.readInt16 = function(){
var result = this.buffer.getInt16(this.offset, this.littleEndian);
this.offset += 2;
return result;
};
/**
* Read an unsigned 16-bit integer at the current offset <br>
* Increase offset by 2
* @returns {Number} The unsigned 16-bit integer read
*/
BufferReader.prototype.readUint16 = function(){
var result = this.buffer.getUint16(this.offset, this.littleEndian);
this.offset += 2;
return result;
};
/**
* Read a signed 32-bit integer at the current offset <br>
* Increase offset by 4
* @returns {Number} The signed 32-bit integer read
*/
BufferReader.prototype.readInt32 = function(){
var result = this.buffer.getInt32(this.offset, this.littleEndian);
this.offset += 4;
return result;
};
/**
* Read an unsigned 32-bit integer at the current offset <br>
* Increase offset by 4
* @returns {Number} The unsigned 32-bit integer read
*/
BufferReader.prototype.readUint32 = function(){
var result = this.buffer.getUint32(this.offset, this.littleEndian);
this.offset += 4;
return result;
};
/**
* Read a 32-bit float at the current offset <br>
* Increase offset by 4
* @returns {Number} The 32-bit float read
*/
BufferReader.prototype.readFloat32 = function(){
var result = this.buffer.getFloat32(this.offset, this.littleEndian);
this.offset +=4;
return result;
};
/**
* Read a 64-bit float at the current offset <br>
* Increase offset by 8
* @returns {Number} The 64-bit float read
*/
BufferReader.prototype.readFloat64 = function(){
var result = this.buffer.getFloat64(this.offset, this.littleEndian);
this.offset += 8;
return result;
};
/**
* Change to little endian
*/
BufferReader.prototype.useLittleEndian = function(){
this.littleEndian = true;
return this;
};
/**
* Change to big endian
*/
BufferReader.prototype.useBigEndian = function(){
this.littleEndian = false;
return this;
};
/**
* Returns true if using little endian
* @returns {Boolean} is little endian
*/
BufferReader.prototype.isLittleEndian = function(){
return this.littleEndian;
};
/**
* Returns true if using big endian
* @returns {Boolean} is big endian
*/
BufferReader.prototype.isBigEndian = function(){
return !this.littleEndian;
};
/**
* Returns size of the arraybuffer
* @returns {Number} size of arraybuffer
*/
BufferReader.prototype.getSize = function(){
return this.size;
};
/**
* Returns the current offset
* @returns {Number} the current offset
*/
BufferReader.prototype.getOffset = function(){
return this.offset;
};
return BufferReader;
};