Java Reference
In-Depth Information
typedIds[0] = 10
typedIds[1] = 20
typedIds[2] = 30
It is also possible to use the same ArrayBuffer to store different types of values.
Recall that the views on the ArrayBuffer are typed, not the ArrayBuffer itself; it simply
contains raw binary data. The following code creates an 8-byte ArrayBuffer to store a
32-bit signed integer in the first 4 bytes and a 32-bit signed floating point number in the
last 4 bytes:
// Create an 8=byte buffer
var buffer = new ArrayBuffer(8);
// Create an Int32Array view for the first 4 bytes. byteOffset is 0
// and element count is 1
var id = new Int32Array(buffer, 0, 1);
// Create a Float32Array view for the second 4 bytes. The first 4 bytes
// will be used for integer value. byteOffset is 4 and element count is 1
var salary = new Float32Array(buffer, 4, 1);
// Use the Int32Array view to store an integer
id[0] = 1001;
// Use the Float32Array view to store a floating-point number
salary[0] = 129019.50;
// Read and print the two values using the two views
print("id = " + id[0]);
print("salary = " + salary[0]);
id = 1001
salary = 129019.5
When you create a typed array by supplying the length of an ArrayBuffer , the length
or the size of the ArrayBuffer must be a multiple of element's size. For example, when
you are creating an Int32Array , the size of its buffer must be a multiple of 4 (32-bit is 4
bytes). The following code will throw an exception:
var buffer = new ArrayBuffer(15);
// Throws an exception because an element of Int32Array takes 4 bytes and
// 15, which is the buffer size for the view, is not a multiple of 4
var id = new Int32Array(buffer);
Search WWH ::




Custom Search