Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Tablice reprezentujące typy JavaScript

To tłumaczenie jest niekompletne. Pomóż przetłumaczyć ten artykuł z języka angielskiego.

Jako, że aplikacje internetowe stają się coraz bardziej potężne, zapewniając takie możliwości jak chociażby manipulacja audio i wideo, dostęp do surowych danych używając WebSocket, i tak dalej, stało się jasne, że są sytuacje, w których przydałoby się, żeby kod JavaScript był w stanie szybko i łatwo manipulować surowymi danymi binarnymi. W przeszłości, musiało być to symulowane przez traktowanie surowych danych jako string i używanie metody charCodeAt(), aby przeczytać bajty z buforu danych.

Jakkolwiek, jest to wolne i podatne na błędy, ze względu na potrzebę wielu konwersji (szczególnie jeśli dane binarne nie są tak naprawdę danymi w formacie bajtów, ale, na przykład, 32-bitowymi liczbami całkowitymi lub zmiennoprzecinkowymi).

Tablice zawierające typy JavaScript zapewniają mechanizm dostępu do danych binarnych dużo bardziej wydajnie.

Dokumentacja

ArrayBuffer
The ArrayBuffer is a data type that is used to represent a generic, fixed-length binary data buffer. You can't directly manipulate the contents of an ArrayBuffer; instead, you create an ArrayBufferView object which represents the buffer in a specific format, and use that to read and write the contents of the buffer.
ArrayBufferView
The ArrayBufferView type describes a particular view on the contents of an ArrayBuffer's data. Of note is that you may create multiple views into the same buffer, each looking at the buffer's contents starting at a particular offset. This makes it possible to set up views of different data types to read the contents of a buffer based on the types of data at specific offsets into the buffer
DataView

The DataView view provides a low-level interface for reading data from and writing it to an ArrayBuffer.

StringView Non native
In this article is published a library of ours whose aims are:
  • creating a C-like interface for strings (i.e. array of characters codes — an ArrayBufferView in JavaScript) based upon the JavaScript ArrayBuffer interface,
  • creating an highly scalable library, that anyone can extend by adding methods to the object StringView.prototype,
  • creating a collection of methods for such string-like objects (since now: stringViews) which work strictly on arrays of numbers rather than on creating new immutable JavaScript strings,
  • working with other Unicode encodings different from default JavaScript's UTF-16 DOMStrings,
Getting ArrayBuffers or typed arrays from Base64-encoded strings
Code snippets to get ArrayBuffers or typed arrays from Base64-encoded strings.
FileReader.prototype.readAsArrayBuffer()
The FileReader.prototype.readAsArrayBuffer() method starts reading the contents of the specified Blob or File.
XMLHttpRequest.prototype.send()
XMLHttpRequest instances' send() method now supports typed arrays and ArrayBuffers as argument.

View All...

Społeczność

Narzędzia

View All...

Bufory i widoki: struktura tablic reprezentujących typy

To achieve maximum flexibility and efficiency, JavaScript typed arrays split the implementation into a buffer and a view. A buffer (implemented by the ArrayBuffer class) is an object representing a chunk of data; it has no format to speak of, and offers no mechanism for accessing its contents. In order to access the memory contained in a buffer, you need to use a view. A view provides a context—that is, a data type, starting offset, and number of elements—that turns the data into an actual typed array. Views are implemented by the ArrayBufferView class and its subclasses.

Podklasy tablic reprezentujących typy

The following subclasses provide buffer views allowing access to the data in specific data types. Note that the classes that work with more than one byte (e.g. Int16Array) use the platform byte order. If control over byte order is needed, use DataView instead.

Typ Rozmiar Opis Odpowiednik C
Int8Array 1 8-bitowa liczba całkowita ze znakiem w zapisie dopełnienia do dwóch signed char
Uint8Array 1 8-bitowa liczba całkowita bez znaku unsigned char
Uint8ClampedArray 1 8-bitowa liczba całkowita bez znaku unsigned char
Int16Array 2 16-bitowa liczba całkowita ze znakiem w zapisie dopełnienia do dwóch short
Uint16Array 2 16-bitowa liczba całkowita bez znaku unsigned short
Int32Array 4 32-bitowa liczba całkowita ze znakiem w zapisie dopełnienia do dwóch int
Uint32Array 4 32-bitowa liczba całkowita bez znaku unsigned int
Float32Array 4 32-bitowa liczba zmiennoprzecinkowa IEEE float
Float64Array 8 64-bitowa liczba zmiennoprzecinkowa IEEE double

Superklasy tablic reprezentujących typy

Typ Opis
DataView The DataView view provides a low-level interface for reading data from and writing it to an ArrayBuffer.
StringView Non native The StringView view provides a C-like interface for strings (i.e. array of characters codes — an ArrayBufferView in JavaScript) based upon the JavaScript ArrayBuffer interface,

Używanie widoków z buforami

Stwórzmy 16-bajtowy bufor:

var buffer = new ArrayBuffer(16);

At this point, we have a chunk of memory whose bytes are all pre-initialized to 0. There's not a lot we can do with it, though. We can confirm that it is indeed 16 bytes long, and that's about it:

if (buffer.byteLength == 16) {
  alert("Yes, it's 16 bytes.");
} else {
  alert("Oh no, it's the wrong size!");
} 

Before we can really work with this buffer, we need to create a view. Let's create a view that treats the data in the buffer as an array of 32-bit signed integers:

var int32View = new Int32Array(buffer);

Now we can access the fields in the array just like a normal array:

for (var i=0; i<int32View.length; i++) {
  int32View[i] = i*2;
}

This fills out the 4 entries in the array (4 entries at 4 bytes each makes 16 total bytes) with the values 0, 2, 4, and 6.

Wiele widoków tych samych danych

Things start to get really interesting when you consider that you can create multiple views onto the same data. For example, given the code above, we can continue like this:

var int16View = new Int16Array(buffer);

for (var i=0; i<int16View.length; i++) {
  console.log("Entry " + i + ": " + int16View[i]);
}

Here we create a 16-bit integer view that shares the same buffer as the existing 32-bit view and we output all the values in the buffer as 16-bit integers. Now we get the output 0, 0, 2, 0, 4, 0, 6, 0.

You can go a step farther, though. Consider this:

int16View[0] = 32;
console.log("Entry 0 in the 32-bit array is now " + int32View[0]);

The output from this is "Entry 0 in the 32-bit array is now 32". In other words, the two arrays are indeed simply views on the same data buffer, treating it as different formats. You can do this with any view types.

Praca ze złożonymi strukturami danych

By combining a single buffer with multiple views of different types, starting at different offsets into the buffer, you can interact with data objects containing multiple data types. This lets you, for example, interact with complex data structures from WebGL, data files, or C structures you need to use while using js-ctypes.

Rozważ tą strukturę C:

struct someStruct {
  unsigned long id;
  char username[16];
  float amountDue;
};

Możesz uzyskać dostęp do bufora zawierającego dane w tych formacie w ten sposób:

var buffer = new ArrayBuffer(24);

// ... zczytaj dane do bufora ...

var idView = new Uint32Array(buffer, 0, 1);
var usernameView = new Uint8Array(buffer, 4, 16);
var amountDueView = new Float32Array(buffer, 20, 1);

Potem możesz uzyskać dostęp, na przykład, do kwoty należnej używając amountDueView[0].

Note: The data structure alignment in a C structure is platform-dependent. Take precautions and considerations for these padding differences.

Konwersja do zwykłych tablic

After processing a typed array, it is sometimes useful to convert it back to a normal array in order to benefit from the Array prototype. Following is a way to do that.

var typedArray = new Uint8Array( [ 1, 2, 3, 4 ] ),
    normalArray = Array.apply( [], typedArray );
normalArray.length === 4;
normalArray.constructor === Array;

Kompatybilność

Typed arrays are available in WebKit as well. Chrome 7 includes support for ArrayBuffer, Float32Array, Int16Array, and Uint8Array. Chrome 9 and Firefox 15 add support for DataView objects. Internet Explorer 10 supports all types except Uint8ClampedArray and ArrayBuffer.prototype.slice.

Specyfikacja

Zobacz także

 

Autorzy i etykiety dokumentu

 Autorzy tej strony: teoli, Kuzirashi
 Ostatnia aktualizacja: teoli,