-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbufferwriter.js
More file actions
177 lines (159 loc) · 5.07 KB
/
bufferwriter.js
File metadata and controls
177 lines (159 loc) · 5.07 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
module.exports = function(){
/**
* Create a new BufferWriter
* @constructor
* @param {ArrayBuffer} buffer - An ArrayBuffer object for storing data
* @param {Number} [offset=0] - The starting offset
* @param {Boolean} [littleEndian=false] - Set endianness
*/
function BufferWriter(buffer, offset, littleEndian){
this.offset = offset || 0;
this.littleEndian = littleEndian || false;
this.buffer = new DataView(buffer);
this.size = buffer.byteLength;
}
/**
* Write a null-terminated string of UTF-16 characters at the current offset <br>
* Increase offset by the 2 * (length of string + 1)
* @param {Number} value - The string to be written
*/
BufferWriter.prototype.writeString = function(value){
for(var i = 0; i < value.length; i++){
this.writeChar(value[i]);
}
this.writeUint16(0);
return this;
};
/**
* Write a single UTF-16 character at the current offset <br>
* Increase offset by 2
* @param {Number} value - The character to be written
*/
BufferWriter.prototype.writeChar = function(value){
this.writeUint16(value.charCodeAt(0));
return this;
};
/**
* Write a signed 8-bit integer at the current offset <br>
* Increase offset by 1
* @param {Number} value - The signed 8-bit integer to be written
*/
BufferWriter.prototype.writeInt8 = function(value){
this.buffer.setInt8(this.offset, value);
this.offset += 1;
return this;
};
/**
* Write an unsigned 8-bit integer at the current offset <br>
* Increase offset by 1
* @param {Number} value - The unsigned 8-bit integer to be written
*/
BufferWriter.prototype.writeUint8 = function(value){
this.buffer.setUint8(this.offset, value);
this.offset += 1;
return this;
};
/**
* Write a signed 16-bit integer at the current offset <br>
* Increase offset by 2
* @param {Number} value - The signed 16-bit integer to be written
*/
BufferWriter.prototype.writeInt16 = function(value){
this.buffer.setInt16(this.offset, value, this.littleEndian);
this.offset += 2;
return this;
};
/**
* Write an unsigned 16-bit integer at the current offset <br>
* Increase offset by 2
* @param {Number} value - The unsigned 16-bit integer to be written
*/
BufferWriter.prototype.writeUint16 = function(value){
this.buffer.setUint16(this.offset, value, this.littleEndian);
this.offset += 2;
return this;
};
/**
* Write a signed 32-bit integer at the current offset <br>
* Increase offset by 4
* @param {Number} value - The signed 32-bit integer to be written
*/
BufferWriter.prototype.writeInt32 = function(value){
this.buffer.setInt32(this.offset, value, this.littleEndian);
this.offset += 4;
return this;
};
/**
* Write an unsigned 32-bit integer at the current offset <br>
* Increase offset by 4
* @param {Number} value - The unsigned 32-bit integer to be written
*/
BufferWriter.prototype.writeUint32 = function(value){
this.buffer.setUint32(this.offset, value, this.littleEndian);
this.offset += 4;
return this;
};
/**
* Write a 32-bit float at the current offset <br>
* Increase offset by 4
* @param {Number} value - The 32-bit float to be written
*/
BufferWriter.prototype.writeFloat32 = function(value){
this.buffer.setFloat32(this.offset, value, this.littleEndian);
this.offset += 4;
return this;
};
/**
* Write a 64-bit float at the current offset <br>
* Increase offset by 8
* @param {Number} value - The 64-bit float to be written
*/
BufferWriter.prototype.writeFloat64 = function(value){
this.buffer.setFloat64(this.offset, value, this.littleEndian);
this.offset += 8;
return this;
};
/**
* Change to little endian
*/
BufferWriter.prototype.useLittleEndian = function(){
this.littleEndian = true;
return this;
};
/**
* Change to big endian
*/
BufferWriter.prototype.useBigEndian = function(){
this.littleEndian = false;
return this;
};
/**
* Returns true if using little endian
* @returns {Boolean} is little endian
*/
BufferWriter.prototype.isLittleEndian = function(){
return this.littleEndian;
};
/**
* Returns true if using big endian
* @returns {Boolean} is big endian
*/
BufferWriter.prototype.isBigEndian = function(){
return !this.littleEndian;
};
/**
* Returns size of the arraybuffer
* @returns {Number} size of arraybuffer
*/
BufferWriter.prototype.getSize = function(){
return this.size;
};
/**
* Returns the current offset
* @returns {Number} the current offset
*/
BufferWriter.prototype.getOffset = function(){
return this.offset;
};
return BufferWriter;
};