loadBuffer method

void loadBuffer(
  1. ByteBuffer buffer, {
  2. List<int>? shape,
})

Loads a byte buffer into this TensorBuffer with specific shape.

If shape is null then TensorBuffer.shape is used.

Important: The loaded buffer is a reference. DO NOT MODIFY. We don't create a copy here for performance concern, but if modification is necessary, please make a copy.

Throws ArgumentError.notNull if buffer is null. Throws ArgumentError if the size of buffer in bytes and getTypeSize * flatSize do not match.

Implementation

void loadBuffer(ByteBuffer buffer, {List<int>? shape}) {
  SupportPreconditions.checkNotNull(buffer,
      message: "Byte Buffer cannot be null");

  int flatSize = computeFlatSize(shape ?? this.shape);

  SupportPreconditions.checkArgument(
      (ByteData.view(buffer).lengthInBytes == getTypeSize() * flatSize),
      errorMessage:
          "The size of byte buffer and the shape do not match. buffer: ${ByteData.view(buffer).lengthInBytes} shape: ${getTypeSize() * flatSize}");

  if (!_isDynamic) {
    SupportPreconditions.checkArgument(flatSize == this.flatSize,
        errorMessage:
            "The size of byte buffer and the size of the tensor buffer do not match.");
  } else {
    this.flatSize = flatSize;
  }

  this.shape = List<int>.from(shape ?? this.shape);
  this.byteData = ByteData.view(buffer);
}