Repository: ktorio/ktor
File: ktor-io/common/src/io/ktor/utils/io/core/ByteReadPacket.kt (Kotlin)

Code before:
    ReplaceWith("Buffer()", "kotlinx.io.Buffer")
)
public fun Sink(pool: ObjectPool<*>): kotlinx.io.Buffer = kotlinx.io.Buffer()

@Deprecated(
    "Use Buffer instead",
    ReplaceWith("Buffer()", "kotlinx.io.Buffer")
)
public fun Sink(): kotlinx.io.Buffer = kotlinx.io.Buffer()

@OptIn(InternalIoApi::class)
public fun Source.readAvailable(out: kotlinx.io.Buffer): Int {
    val result = buffer.size
    out.transferFrom(this)
    return result.toInt()
}

@OptIn(InternalIoApi::class)
public fun Source.copy(): Source = buffer.copy()

@OptIn(InternalIoApi::class)
public fun Source.readShortLittleEndian(): Short {
    return buffer.readShortLe()
}

@OptIn(InternalIoApi::class)
public fun Source.discard(count: Long = Long.MAX_VALUE): Long {
    request(count)
    val countToDiscard = minOf(count, remaining)
    buffer.skip(countToDiscard)
    return countToDiscard
}

@OptIn(InternalIoApi::class)
public fun Source.takeWhile(block: (kotlinx.io.Buffer) -> Boolean) {
    while (!exhausted() && block(buffer)) {
    }
}

public fun Source.readFully(out: ByteArray, offset: Int = 0, length: Int = out.size - offset) {
    readTo(out, offset, offset + length)
}

@OptIn(InternalIoApi::class)
public fun <T> Source.preview(function: (Source) -> T): T {
    return buffer.peek().use(function)
}

@OptIn(InternalIoApi::class)
public fun <T> Sink.preview(function: (Source) -> T): T {
    return buffer.peek().use(function)

Review comment (suggestion):
@bjhham This change caused some issues in my project. After a `Source.copy()` call, then closing the original Source, the copied source is now no longer valid. It was valid before. I don't think it makes sense to call a function and its description 'copy' if it does not actually copy. This is very unclear.

Code after:
    ReplaceWith("Buffer()", "kotlinx.io.Buffer")
)
public fun Sink(pool: ObjectPool<*>): kotlinx.io.Buffer = kotlinx.io.Buffer()

@Deprecated(
    "Use Buffer instead",
    ReplaceWith("Buffer()", "kotlinx.io.Buffer")
)
public fun Sink(): kotlinx.io.Buffer = kotlinx.io.Buffer()

@OptIn(InternalIoApi::class)
public fun Source.readAvailable(out: kotlinx.io.Buffer): Int {
    val result = buffer.size
    out.transferFrom(this)
    return result.toInt()
}

/**
 * Returns a copy of the current buffer attached to this Source.
 */
@Deprecated(
    "Use peek() or buffer.copy() instead, depending on your use case.",
    ReplaceWith("peek()", "kotlinx.io.Source")
)
@OptIn(InternalIoApi::class)
public fun Source.copy(): Source = peek()

@OptIn(InternalIoApi::class)
public fun Source.readShortLittleEndian(): Short {
    return buffer.readShortLe()
}

@OptIn(InternalIoApi::class)
public fun Source.discard(count: Long = Long.MAX_VALUE): Long {
    request(count)
    val countToDiscard = minOf(count, remaining)
    buffer.skip(countToDiscard)
    return countToDiscard
}

@OptIn(InternalIoApi::class)
public fun Source.takeWhile(block: (kotlinx.io.Buffer) -> Boolean) {
    while (!exhausted() && block(buffer)) {
    }
}

public fun Source.readFully(out: ByteArray, offset: Int = 0, length: Int = out.size - offset) {
    readTo(out, offset, offset + length)
}

@OptIn(InternalIoApi::class)