read

fun InputStream.read(destination: ByteArray, destinationOffset: Int = 0, kapacity: Kapacity): Int

Reads up to the specified kapacity of bytes from this input stream into the given destination array.

This function safely guards against buffer overflows. It automatically calculates the available space in the destination array starting from the destinationOffset and ensures that the number of bytes read does not exceed this available space, even if the requested kapacity is larger.

Return

The total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

Parameters

destination

The byte array to which data is written.

destinationOffset

The starting offset in the destination array where the data will be written. Defaults to 0.

kapacity

The maximum number of bytes to read from the stream.

Throws

If destinationOffset is outside the bounds of the destination array, or if kapacity represents a negative value.