writeToBuffer method

void writeToBuffer(
  1. Pointer<Uint8> buffer,
  2. int offset, [
  3. int byteCnt = 8
])

Puts a double value into the buffer, starting at offset.

The value occupies byteCnt consecutive bytes in the buffer. byteCnt has to be 4 (Float32) or 8 (Float64), otherwise a MemException gets thrown. If the index is aligned, that is if (offset mod byteCnt) = 0, the method is more efficient. On unaligned data, byte by byte copying occurs.

Example:

  (-232.77).put(buf, 16, 8);

Implementation

void writeToBuffer(Pointer<Uint8> buffer, int offset, [int byteCnt = 8]) {
  if (offset % byteCnt == 0) {
    // the index is aligned
    switch (byteCnt) {
      case 4:
        Pointer<Float>.fromAddress(buffer.address + offset)
            .asTypedList(1)[0] = this;
      case 8:
        Pointer<Double>.fromAddress(buffer.address + offset)
            .asTypedList(1)[0] = this;
      default:
        throw MemException("Invalid target float byte length: $byteCnt");
    }
  } else {
    // the index is not aligned - copying byte by byte
    Uint8List bytes = toBytes(byteCnt);
    for (int i = 0; i < byteCnt; i++) {
      buffer[offset + i] = bytes[i];
    }
  }
}