Java Reference
In-Depth Information
The specification for typed array implemented by Nashorn contains an isView(args)
static method in the ArrayBuffer object. However, it does not seem to be implemented
by Nashorn in Java 8u40. A bug has been filed at https://bugs.openjdk.java.net/
browse/JDK-8061959 . The method returns true if the specified argument represents
an array buffer view. For example, if you pass an object like Int8Array , Int16Array ,
DataView , and so on to this method, it returns true , because these objects represent an
array buffer view. If you pass a simple object or a primitive value to this method, it returns
false . The following code shows how to use this method. Note that as of Java 8u40, this
method does not exist in Nashorn and the code will throw an exception:
// Creates an array buffer view of length 4
var int8View = new Int8Array(4);
// Assigns true to isView1
var isView1 = ArrayBuffer.isView(int8View);
// Assigns false to isView2
var isView2 = ArrayBuffer.isView({});
The ArrayBuffer object contains a slice(start, end) method that creates and
returns a new ArrayBuffer whose contents are a copy of the original ArrayBuffer
from the index start , inclusive, up to end , exclusive. If start or end is negative, it
refers to an index from the end of the buffer. If end is unspecified, it defaults to the
byteLength property of the ArrayBuffer . Both start and end are clamped between 0 and
byteLength—1 . The following code shows how to use the slice() method:
// Create an ArrayBuffer of 4 bytes. Bytes have indexes 0, 1, 2, and 3
var buffer = new ArrayBuffer(4);
// Manipulate buffer using one of the typed views here...
// Copy the last 2 bytes from buffer to buffer2
var buffer2 = buffer.slice(2, 4);
// Copy the bytes from buffer from index 1 to the end (last 3 bytes)
// to buffer3
var buffer3 = buffer.slice(1);
Views of an ArrayBuffer
There are two types of views of an ArrayBuffer :
Typed Array Views
DataView Views
 
Search WWH ::




Custom Search