DataView is a type of view that can read and write an ArrayBuffer.
DataView does not assume anything about the buffer contents.
DataView is not iterable.
A DataView must be created to read from and write to an ArrayBuffer that already exists.
It can use the whole buffer or only part of it.
DataView maintains a reference to the buffer instance.
const buf = new ArrayBuffer(16);//from w w w . j a v a 2 s . c om // DataView default to use the entire ArrayBuffer const fullDataView = new DataView(buf); console.log(fullDataView.byteOffset); // 0 console.log(fullDataView.byteLength); // 16 console.log(fullDataView.buffer === buf); // true
Constructor takes an optional byte offset and byte length
byteOffset=0
begins the view at the start of the buffer
byteLength=8
restricts the view to the first 8 bytes
const buf = new ArrayBuffer(16);/*from w ww . j av a 2 s . c o m*/ const firstHalfDataView = new DataView(buf, 0, 8); console.log(firstHalfDataView.byteOffset); // 0 console.log(firstHalfDataView.byteLength); // 8 console.log(firstHalfDataView.buffer === buf); // true
DataView will use the remainder of the buffer unless specified
byteOffset=8
begins the view at the 9th byte of the buffer.
byteLength
default is the remainder of the buffer
const buf = new ArrayBuffer(16);//w w w . j a v a2 s . c om const secondHalfDataView = new DataView(buf, 8); console.log(secondHalfDataView.byteOffset); // 8 console.log(secondHalfDataView.byteLength); // 8 console.log(secondHalfDataView.buffer === buf); // true