Java Reference
In-Depth Information
Typed Arrays
Typed arrays in Nashorn are array-like objects. They provide views of raw binary data
called the
buffer
. You can have multiple typed views of the same buffer. Suppose you have
a buffer of 4 bytes. You can have an 8-bit signed integer typed array view of the buffer that
will represent four 8-bit signed integers. At the same time, you can have a 32-bit unsigned
integer typed array view of the same buffer that can represent one 32-bit unsigned integer.
■
a typed array is an array-like object providing a typed view of raw binary data in
memory. the specification for typed arrays implementation in nashorn can be found at
Tip
The buffer in a typed array is represented by an
ArrayBuffer
object. Using an
ArrayBuffer
object directly or indirectly, you can create a typed array view. Once you
have a typed array view, you can write or read data of the specific type supported by the
typed array view, using the array-like syntax. In the next section, I will discuss how to
work with
ArrayBuffer
objects. In the subsequent sections, I will discuss different types
of typed array views (or simply called typed arrays).
The ArrayBuffer Object
An
ArrayBuffer
object represents a fixed-length buffer of raw binary data. The contents
of an
ArrayBuffer
does not have a type. You cannot directly modify the data in an
ArrayBuffer
; you must create and use one of the typed views on it to do so. It contains
few properties and methods to copy its contents into another
ArrayBuffer
and to query
its size.
The
ArrayBuffer
constructor takes one argument that is the length of the buffer in
bytes. The length of an
ArrayBuffer
object cannot be changed, after it is created. The
object has a read-only property named
byteLength
that represents the length of the
buffer. The following statement creates a buffer of 32 bytes and prints its length:
// Create a ArrayBuffer of 32 bytes
var buffer = new ArrayBuffer(32);
// Assigns 32 to len
var len = buffer.byteLength;
■
When you create an
ArrayBuffer
, its contents are initialized to zero. there is no
way to initialize its contents with values other than zero, at the time of creation. each byte in
the
ArrayBuffer
uses a zero-based index. the first byte has indexed 0, second 1, third 2,
and so on.
Tip
Search WWH ::
Custom Search