-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataView.ts
313 lines (275 loc) · 12.2 KB
/
dataView.ts
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
Copyright (c) 2010, Linden Research, Inc.
Copyright (c) 2014, Joshua Bell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
$/LicenseInfo$
*/
// Original can be found at:
// https://bitbucket.org/lindenlab/llsd
// Modifications by Joshua Bell [email protected]
// https://github.com/inexorabletash/polyfill
// ES3/ES5 implementation of the Krhonos Typed Array Specification
// Ref: http://www.khronos.org/registry/typedarray/specs/latest/
// Date: 2011-02-01
//
// Variations:
// * Allows typed_array.get/set() as alias for subscripts (typed_array[])
// * Gradually migrating structure from Khronos spec to ES2015 spec
class DataView {
protected _buffer: ArrayBuffer
protected _byteLength: number
protected _byteOffset: number
public constructor(buffer: ArrayBuffer,
byteOffset: number = 0,
byteLength: number = null
) {
byteOffset = byteOffset >>> 0
if (byteOffset > buffer.byteLength) {
throw "byteOffset is out of range."
}
if (byteLength === undefined || byteLength == null) {
byteLength = buffer.byteLength - byteOffset
} else {
byteLength = byteLength >>> 0
}
if ((byteOffset + byteLength) > buffer.byteLength) {
throw "byteOffset and length reference an area beyond the end of the buffer."
}
this._buffer = buffer
this._byteLength = byteLength
this._byteOffset = byteOffset
}
public get buffer(): ArrayBuffer {
return this._buffer
}
public get byteLength(): number {
return this._byteLength
}
public get byteOffset(): number {
return this._byteOffset
}
public static get IS_BIG_ENDIAN(): boolean {
let u16: Uint16Array = new Uint16Array()
let u8: Uint8Array = new Uint8Array()
u16.fromArray([0x1234,])
u8.fromArrayBuffer(u16.buffer)
return (u8.get(0) === 0x12)
}
/**
* Gets the Float32 value at the specified byte offset from the start of the view. There is
* no alignment constraint; multi-byte values may be fetched from any offset.
* @param byteOffset The place in the buffer at which the value should be retrieved.
* @param littleEndian If false or undefined, a big-endian value should be read.
*/
public getFloat32(byteOffset: number, littleEndian: boolean = false): number {
throw "Not yet implemented."
}
/**
* Gets the Float64 value at the specified byte offset from the start of the view. There is
* no alignment constraint; multi-byte values may be fetched from any offset.
* @param byteOffset The place in the buffer at which the value should be retrieved.
* @param littleEndian If false or undefined, a big-endian value should be read.
*/
public getFloat64(byteOffset: number, littleEndian: boolean = false): number {
throw "Not yet implemented."
}
/**
* Gets the Int8 value at the specified byte offset from the start of the view. There is
* no alignment constraint; multi-byte values may be fetched from any offset.
* @param byteOffset The place in the buffer at which the value should be retrieved.
*/
public getInt8(byteOffset: number): number {
let r: Int8Array = new Int8Array()
return this.getter(r, byteOffset)
}
/**
* Gets the Int16 value at the specified byte offset from the start of the view. There is
* no alignment constraint; multi-byte values may be fetched from any offset.
* @param byteOffset The place in the buffer at which the value should be retrieved.
* @param littleEndian If false or undefined, a big-endian value should be read.
*/
public getInt16(byteOffset: number, littleEndian: boolean = false): number {
let r: Int16Array = new Int16Array()
return this.getter(r, byteOffset, littleEndian)
}
/**
* Gets the Int32 value at the specified byte offset from the start of the view. There is
* no alignment constraint; multi-byte values may be fetched from any offset.
* @param byteOffset The place in the buffer at which the value should be retrieved.
* @param littleEndian If false or undefined, a big-endian value should be read.
*/
public getInt32(byteOffset: number, littleEndian: boolean = false): number {
let r: Int32Array = new Int32Array()
return this.getter(r, byteOffset, littleEndian)
}
/**
* Gets the Uint8 value at the specified byte offset from the start of the view. There is
* no alignment constraint; multi-byte values may be fetched from any offset.
* @param byteOffset The place in the buffer at which the value should be retrieved.
*/
public getUint8(byteOffset: number): number {
let r: Uint8Array = new Uint8Array()
return this.getter(r, byteOffset)
}
/**
* Gets the Uint16 value at the specified byte offset from the start of the view. There is
* no alignment constraint; multi-byte values may be fetched from any offset.
* @param byteOffset The place in the buffer at which the value should be retrieved.
* @param littleEndian If false or undefined, a big-endian value should be read.
*/
public getUint16(byteOffset: number, littleEndian: boolean = false): number {
let r: Uint16Array = new Uint16Array()
return this.getter(r, byteOffset, littleEndian)
}
/**
* Gets the Uint32 value at the specified byte offset from the start of the view. There is
* no alignment constraint; multi-byte values may be fetched from any offset.
* @param byteOffset The place in the buffer at which the value should be retrieved.
* @param littleEndian If false or undefined, a big-endian value should be read.
*/
public getUint32(byteOffset: number, littleEndian: boolean = false): number {
let r: Uint32Array = new Uint32Array()
return this.getter(r, byteOffset, littleEndian)
}
/**
* Stores an Float32 value at the specified byte offset from the start of the view.
* @param byteOffset The place in the buffer at which the value should be set.
* @param value The value to set.
* @param littleEndian If false or undefined, a big-endian value should be written.
*/
public setFloat32(byteOffset: number, value: number, littleEndian: boolean = false): void {
throw "Not yet implemented."
}
/**
* Stores an Float64 value at the specified byte offset from the start of the view.
* @param byteOffset The place in the buffer at which the value should be set.
* @param value The value to set.
* @param littleEndian If false or undefined, a big-endian value should be written.
*/
public setFloat64(byteOffset: number, value: number, littleEndian: boolean = false): void {
throw "Not yet implemented."
}
/**
* Stores an Int8 value at the specified byte offset from the start of the view.
* @param byteOffset The place in the buffer at which the value should be set.
* @param value The value to set.
*/
public setInt8(byteOffset: number, value: number): void {
let r: Int8Array = new Int8Array()
this.setter(r, byteOffset, value)
}
/**
* Stores an Int16 value at the specified byte offset from the start of the view.
* @param byteOffset The place in the buffer at which the value should be set.
* @param value The value to set.
* @param littleEndian If false or undefined, a big-endian value should be written.
*/
public setInt16(byteOffset: number, value: number, littleEndian: boolean = false): void {
let r: Int16Array = new Int16Array()
this.setter(r, byteOffset, value, littleEndian)
}
/**
* Stores an Int32 value at the specified byte offset from the start of the view.
* @param byteOffset The place in the buffer at which the value should be set.
* @param value The value to set.
* @param littleEndian If false or undefined, a big-endian value should be written.
*/
public setInt32(byteOffset: number, value: number, littleEndian: boolean = false): void {
let r: Int32Array = new Int32Array()
this.setter(r, byteOffset, value, littleEndian)
}
/**
* Stores an Uint8 value at the specified byte offset from the start of the view.
* @param byteOffset The place in the buffer at which the value should be set.
* @param value The value to set.
*/
public setUint8(byteOffset: number, value: number): void {
let r: Uint8Array = new Uint8Array()
this.setter(r, byteOffset, value)
}
/**
* Stores an Uint16 value at the specified byte offset from the start of the view.
* @param byteOffset The place in the buffer at which the value should be set.
* @param value The value to set.
* @param littleEndian If false or undefined, a big-endian value should be written.
*/
public setUint16(byteOffset: number, value: number, littleEndian: boolean = false): void {
let r: Uint16Array = new Uint16Array()
this.setter(r, byteOffset, value, littleEndian)
}
/**
* Stores an Uint32 value at the specified byte offset from the start of the view.
* @param byteOffset The place in the buffer at which the value should be set.
* @param value The value to set.
* @param littleEndian If false or undefined, a big-endian value should be written.
*/
public setUint32(byteOffset: number, value: number, littleEndian: boolean = false): void {
let r: Uint32Array = new Uint32Array()
this.setter(r, byteOffset, value, littleEndian)
}
protected getter(
r: TypedArray,
byteOffset: number,
littleEndian: boolean = false
): number {
byteOffset = byteOffset >>> 0
if (byteOffset + r.bytesPerElement > this.byteLength) {
throw "Array index out of range."
}
byteOffset += this.byteOffset
let u8 = new Uint8Array()
u8.fromArrayBuffer(this.buffer, byteOffset, r.bytesPerElement)
let bytes: number[] = []
for (let i: number = 0; i < r.bytesPerElement; i++) {
bytes.push(u8.get(i))
}
if (littleEndian == DataView.IS_BIG_ENDIAN) {
bytes.reverse()
}
let r8: Uint8Array = new Uint8Array()
r8.fromArray(bytes)
r.fromArrayBuffer(r8.buffer)
return r.get(0)
}
protected setter(
r: TypedArray,
byteOffset: number,
value: number,
littleEndian: boolean = false
): void {
byteOffset = byteOffset >>> 0
if (byteOffset + r.bytesPerElement > this.byteLength) {
throw "Array index out of range."
}
// Get bytes.
r.fromArray([value,])
let byteArray: Uint8Array = new Uint8Array()
byteArray.fromArrayBuffer(r.buffer)
let bytes: number[] = []
for (let i: number = 0; i < r.bytesPerElement; i++) {
bytes.push(byteArray.get(i))
}
// Flip if necessary.
if (littleEndian == DataView.IS_BIG_ENDIAN) {
bytes.reverse()
}
// Write them.
let byteView: Uint8Array = new Uint8Array()
byteView.fromArrayBuffer(this.buffer, byteOffset, r.bytesPerElement)
byteView.setFromArray(bytes)
}
}