getBytes method

List<int> getBytes(
  1. List<int> data
)

Extracts the selected bytes from the given data.

Returns a sublist of data containing only the selected bytes. If the selection is out of bounds, returns an empty list or truncates to available data.

Example:

final data = [0x48, 0x65, 0x6C, 0x6C, 0x6F]; // "Hello"
final sel = ByteSelection.range(0, 2);
final bytes = sel.getBytes(data); // [0x48, 0x65, 0x6C]

Implementation

List<int> getBytes(List<int> data) {
  if (startOffset < 0 || startOffset >= data.length) {
    return [];
  }
  // Use min to avoid integer overflow when endOffset is near int.maxValue
  final end = min(endOffset + 1, data.length);
  return data.sublist(startOffset, end);
}