ByteData class abstract final

A fixed-length, random-access sequence of bytes that also provides random and unaligned access to the fixed-width integers and floating point numbers represented by those bytes.

ByteData may be used to pack and unpack data from external sources (such as networks or files systems), and to process large quantities of numerical data more efficiently than would be possible with ordinary List implementations. ByteData can save space, by eliminating the need for object headers, and time, by eliminating the need for data copies.

If data comes in as bytes, they can be converted to ByteData by sharing the same buffer.

Uint8List bytes = ...;
var blob = ByteData.sublistView(bytes);
if (blob.getUint32(0, Endian.little) == 0x04034b50) { // Zip file marker
  ...
}

Finally, ByteData may be used to intentionally reinterpret the bytes representing one arithmetic type as another. For example this code fragment determine what 32-bit signed integer is represented by the bytes of a 32-bit floating point number (both stored as big endian):

var bdata = ByteData(8);
bdata.setFloat32(0, 3.04);
int huh = bdata.getInt32(0); // 0x40428f5c

It is a compile-time error for a class to attempt to extend or implement ByteData.

Implemented types
Available extensions

Constructors

ByteData(int length)
Creates a ByteData of the specified length (in elements), all of whose bytes are initially zero.
factory
ByteData.sublistView(TypedData data, [int start = 0, int? end])
Creates a ByteData view on a range of elements of data.
factory
ByteData.view(ByteBuffer buffer, [int offsetInBytes = 0, int? length])
Creates an ByteData view of the specified region in buffer.
factory

Properties

buffer ByteBuffer
The byte buffer associated with this object.
no setterinherited
elementSizeInBytes int
The number of bytes in the representation of each element in this list.
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
lengthInBytes int
The length of this view, in bytes.
no setterinherited
offsetInBytes int
The offset of this view into the underlying byte buffer, in bytes.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
toJS JSDataView

Available on ByteData, provided by the ByteDataToJSDataView extension

Converts this ByteData to a JSDataView by either casting, unwrapping, or cloning the ByteData.
no setter

Methods

asUnmodifiableView() ByteData
A read-only view of this ByteData.
getFloat32(int byteOffset, [Endian endian = Endian.big]) double
The floating point number represented by the four bytes at the specified byteOffset in this object, in IEEE 754 single-precision binary floating-point format (binary32).
getFloat64(int byteOffset, [Endian endian = Endian.big]) double
The floating point number represented by the eight bytes at the specified byteOffset in this object, in IEEE 754 double-precision binary floating-point format (binary64).
getInt16(int byteOffset, [Endian endian = Endian.big]) int
The (possibly negative) integer represented by the two bytes at the specified byteOffset in this object, in two's complement binary form.
getInt32(int byteOffset, [Endian endian = Endian.big]) int
The (possibly negative) integer represented by the four bytes at the specified byteOffset in this object, in two's complement binary form.
getInt64(int byteOffset, [Endian endian = Endian.big]) int
The (possibly negative) integer represented by the eight bytes at the specified byteOffset in this object, in two's complement binary form.
getInt8(int byteOffset) int
The (possibly negative) integer represented by the byte at the specified byteOffset in this object, in two's complement binary representation.
getUint16(int byteOffset, [Endian endian = Endian.big]) int
The positive integer represented by the two bytes starting at the specified byteOffset in this object, in unsigned binary form.
getUint32(int byteOffset, [Endian endian = Endian.big]) int
The positive integer represented by the four bytes starting at the specified byteOffset in this object, in unsigned binary form.
getUint64(int byteOffset, [Endian endian = Endian.big]) int
The positive integer represented by the eight bytes starting at the specified byteOffset in this object, in unsigned binary form.
getUint8(int byteOffset) int
The positive integer represented by the byte at the specified byteOffset in this object, in unsigned binary form.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
setFloat32(int byteOffset, double value, [Endian endian = Endian.big]) → void
Sets the four bytes starting at the specified byteOffset in this object to the IEEE 754 single-precision binary floating-point (binary32) representation of the specified value.
setFloat64(int byteOffset, double value, [Endian endian = Endian.big]) → void
Sets the eight bytes starting at the specified byteOffset in this object to the IEEE 754 double-precision binary floating-point (binary64) representation of the specified value.
setInt16(int byteOffset, int value, [Endian endian = Endian.big]) → void
Sets the two bytes starting at the specified byteOffset in this object to the two's complement binary representation of the specified value, which must fit in two bytes.
setInt32(int byteOffset, int value, [Endian endian = Endian.big]) → void
Sets the four bytes starting at the specified byteOffset in this object to the two's complement binary representation of the specified value, which must fit in four bytes.
setInt64(int byteOffset, int value, [Endian endian = Endian.big]) → void
Sets the eight bytes starting at the specified byteOffset in this object to the two's complement binary representation of the specified value, which must fit in eight bytes.
setInt8(int byteOffset, int value) → void
Sets the byte at the specified byteOffset in this object to the two's complement binary representation of the specified value, which must fit in a single byte.
setUint16(int byteOffset, int value, [Endian endian = Endian.big]) → void
Sets the two bytes starting at the specified byteOffset in this object to the unsigned binary representation of the specified value, which must fit in two bytes.
setUint32(int byteOffset, int value, [Endian endian = Endian.big]) → void
Sets the four bytes starting at the specified byteOffset in this object to the unsigned binary representation of the specified value, which must fit in four bytes.
setUint64(int byteOffset, int value, [Endian endian = Endian.big]) → void
Sets the eight bytes starting at the specified byteOffset in this object to the unsigned binary representation of the specified value, which must fit in eight bytes.
setUint8(int byteOffset, int value) → void
Sets the byte at the specified byteOffset in this object to the unsigned binary representation of the specified value, which must fit in a single byte.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited