diff --git a/runtime/src/main/cpp/Arrays.cpp b/runtime/src/main/cpp/Arrays.cpp index cc2a956ea69..91546c7704b 100644 --- a/runtime/src/main/cpp/Arrays.cpp +++ b/runtime/src/main/cpp/Arrays.cpp @@ -467,17 +467,18 @@ KInt Kotlin_NativePtrArray_getArrayLength(KConstRef thiz) { return array->count_; } -OBJ_GETTER(Kotlin_ImmutableBlob_toByteArray, KConstRef thiz, KInt start, KInt count) { - const ArrayHeader* array = thiz->array(); - if (start < 0 || count < 0 || start > array->count_ - count) { - ThrowArrayIndexOutOfBoundsException(); - } - ArrayHeader* result = AllocArrayInstance( - theByteArrayTypeInfo, count, OBJ_RESULT)->array(); - memcpy(PrimitiveArrayAddressOfElementAt(result, 0), - PrimitiveArrayAddressOfElementAt(array, start), - count); - RETURN_OBJ(result->obj()); +OBJ_GETTER(Kotlin_ImmutableBlob_toByteArray, KConstRef thiz, KInt startIndex, KInt endIndex) { + const ArrayHeader* array = thiz->array(); + if (startIndex < 0 || endIndex > array->count_ || startIndex > endIndex) { + ThrowArrayIndexOutOfBoundsException(); + } + KInt count = endIndex - startIndex; + ArrayHeader* result = AllocArrayInstance( + theByteArrayTypeInfo, count, OBJ_RESULT)->array(); + memcpy(PrimitiveArrayAddressOfElementAt(result, 0), + PrimitiveArrayAddressOfElementAt(array, startIndex), + count); + RETURN_OBJ(result->obj()); } KNativePtr Kotlin_ImmutableBlob_asCPointerImpl(KRef thiz, KInt offset) { diff --git a/runtime/src/main/kotlin/kotlin/native/Blob.kt b/runtime/src/main/kotlin/kotlin/native/Blob.kt index 84c9fe9ca97..5e02288bc77 100644 --- a/runtime/src/main/kotlin/kotlin/native/Blob.kt +++ b/runtime/src/main/kotlin/kotlin/native/Blob.kt @@ -42,19 +42,23 @@ private class ImmutableBlobIteratorImpl(val blob: ImmutableBlob) : ByteIterator( } /** - * Allocates new [ByteArray] and copies the data from blob starting from [start] - * and with [count] amount of bytes. + * Copies the data from this blob into a new [ByteArray]. + * + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this blob by default. */ @SymbolName("Kotlin_ImmutableBlob_toByteArray") -public external fun ImmutableBlob.toByteArray(start: Int = 0, count: Int = size): ByteArray +public external fun ImmutableBlob.toByteArray(startIndex: Int = 0, endIndex: Int = size): ByteArray /** - * Allocates new [UByteArray] and copies the data from blob starting from [start] - * and with [count] amount of bytes. + * Copies the data from this blob into a new [UByteArray]. + * + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this blob by default. */ @ExperimentalUnsignedTypes @SymbolName("Kotlin_ImmutableBlob_toByteArray") -public external fun ImmutableBlob.toUByteArray(start: Int = 0, count: Int = size): UByteArray +public external fun ImmutableBlob.toUByteArray(startIndex: Int = 0, endIndex: Int = size): UByteArray /** * Returns stable C pointer to data at certain [offset], useful as a way to pass resource