toBytes method

Uint8List toBytes()

Reads the entire content of the BIO into Uint8List.

Implementation

Uint8List toBytes() {
  final pp = calloc<Pointer<Char>>();
  try {
    // BIO_get_mem_data sets pp to verify the internal buffer and returns length.
    // NOTE: This usually only works for memory BIOs.
    final len = _context.bindings.BIO_ctrl(handle, BIO_CTRL_INFO, 0, pp.cast());
    if (len < 0) {
       throw OpenSslException('BIO_ctrl failed to get data');
    }

    final charPtr = pp.value;
    if (charPtr == nullptr || len == 0) return Uint8List(0);

    final result = Uint8List(len);
    result.setAll(0, charPtr.cast<Uint8>().asTypedList(len));
    return result;
  } finally {
    calloc.free(pp);
  }
}