put method

ByteBuffer put(
  1. dynamic input, [
  2. int offset = 0,
  3. int? length
])

Writes a byte or bytes into this buffer

Implementation

ByteBuffer put(dynamic input, [int offset = 0, int? length]) {
  if (input is int) {
    if (_pos >= _buf.length) {
      throw RangeError('Buffer overflow');
    }
    _buf[_pos++] = input & 0xFF;
    return this;
  }

  if (input is Uint8List) {
    final srcOffset = offset;
    final srcLength = length ?? input.length;

    if (srcOffset < 0 || srcOffset > input.length) {
      throw RangeError('Invalid offset');
    }
    if (srcLength < 0 || srcOffset + srcLength > input.length) {
      throw RangeError('Invalid length');
    }
    if (_pos + srcLength > _buf.length) {
      throw RangeError('Buffer overflow');
    }

    _buf.setRange(_pos, _pos + srcLength, input, srcOffset);
    _pos += srcLength;
    return this;
  }

  throw ArgumentError('Input must be an int or Uint8List');
}