mozilla
Your Search Results

    Uint8Array Redirect 1

    Draft
    This page is not complete.

    The Uint8Array type represents an array of 8-bit unsigned integers.

    Once established, you can reference elements in the array using the object's methods, or using standard array index syntax (that is, using bracket notation).

    Constructors

    Note: In these methods, TypedArray represents any of the typed array object types.
    Uint8Array Uint8Array(unsigned long length);
    Uint8Array Uint8Array(TypedArray array);
    Uint8Array Uint8Array(sequence<type> array);
    Uint8Array Uint8Array(ArrayBuffer buffer, optional unsigned long byteOffset, optional unsigned long length);

    Method overview

    Note: In these methods, TypedArray represents any of the typed array object types.
    void set(TypedArray array, optional unsigned long offset);
    void set(type[] array, optional unsigned long offset);
    Uint8Array subarray(long begin, optional long end);

    Attributes

    Attribute Type Description
    buffer ArrayBuffer The ArrayBuffer object used to contain the TypedArray datas. Read only.
    byteOffset unsigned long The index at which starts the TypedArray in it underlying ArrayBuffer. Read only.
    length unsigned long The number of entries in the array; for these 8-bit values, this is the same as the size of the array in bytes. Read only.

    Constants

    Element size

    Constant Value Description
    BYTES_PER_ELEMENT 1 The size, in bytes, of each array element.

    Constructor

    Returns a new Uint8Array object. Every entry in the array is initialized to 0 unless initial data is specifically provided.

    Uint8Array Uint8Array(
      unsigned long length
    );
    
    Uint8Array Uint8Array(
      TypedArray array
    );
    
    Uint8Array Uint8Array(
      sequence<type> array
    );
    
    Uint8Array Uint8Array(
      ArrayBuffer buffer,
      optional unsigned long byteOffset,
      optional unsigned long length
    );
    
    Parameters
    length
    The number of elements in the byte array. If unspecified, length of the array view will match the buffer's length.
    array
    An object of any of the typed array types (such as Int32Array), or a sequence of objects of a particular type, to copy into a new ArrayBuffer. Each value in the source array is converted to an 8-bit unsigned integer before being copied into the new array.
    buffer
    An existing ArrayBuffer to use as the storage for the new Uint8Array object.
    byteOffset
    The offset, in bytes, to the first byte in the specified buffer for the new view to reference. If not specified, the Uint8Array's view of the buffer will start with the first byte.
    Return value

    A new Uint8Array object representing the specified data buffer.

    Notes

    The first form of the constructor, accepting only a length, creates a new ArrayBuffer object that can hold the specified number of unsigned 8-bit integers, then creates and returns a typed array view referring to that new buffer. The length must be specified in this case.

    The second form creates a new ArrayBuffer object that is a duplicate of the specified array, except with each element in the new array converted to an 8-bit unsigned integer.

    The third form of the constructor likewise creates a new ArrayBuffer object that is a duplicate of the specified JavaScript array, with each element in the new array converted to an 8-bit unsigned integer.

    The final form of the constructor creates a new Uint8Array object, using the specified ArrayBuffer as its storage. This lets you access the existing buffer in a different format. If specified, the byteOffset and length parameters let you create the new view to only a portion of the buffer.

    Exceptions thrown
    INDEX_SIZE_ERR
    The specified byteOffset isn't a multiple of the element size, or the byteOffset and length result in the specified view extending past the end of the buffer.

    Methods

    set()

    Sets multiple values in the typed array, reading input values from a specified array.

    void set(
      TypedArray array,
      optional unsigned long offset
    );
    
    void set(
      type[] array,
      optional unsigned long offset
    );
    
    Parameters
    array
    An array from which to copy values. All values from the source array are copied into the target array, unless the length of the source array plus the offset exceeds the length of the target array, in which case an exception is thrown. If the source array is a typed array, the two arrays may share the same underlying ArrayBuffer; the browser will intelligently copy the source range of the buffer to the destination range.
    offset Optional
    The offset into the target array at which to begin writing values from the source array. If you omit this value, 0 is assumed (that is, the source array will overwrite values in the target array starting at index 0).

    subarray()

    Returns a new Uint8Array view on the ArrayBuffer store for this Uint8Array object.

    Uint8Array subarray(
      long begin,
      optional long end
    );
    
    Parameters
    begin
    The offset to the first element in the array to be referenced by the new Uint8Array object.
    end Optional
    The offset to the element after last element in the array to be referenced by the new Uint8Array object; if not specified, all elements from the one specified by begin to the end of the array are included in the new view.
    Notes

    The range specified by begin and end is clamped to the valid index range for the current array; if the computed length of the new array would be negative, it's clamped to zero. If either begin or end is negative, it refers to an index from the end of the array instead of from the beginning.

    Note: Keep in mind that this is creating a new view on the existing buffer; changes to the new object's contents will impact the original object and vice versa.

    Getting an Uint8Array from existing data

    From an ArrayBuffer

    var myArray = new Uint8Array(myArrayBuffer);

    From a Base64-encoded string

    Please, read this paragraph.

    See also

    Document Tags and Contributors

    Contributors to this page: fscholz, teoli
    Last updated by: fscholz,