-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,180 additions
and
636 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.ktor.io | ||
|
||
public val Buffer.isEmpty: Boolean get() = availableForRead == 0 | ||
public val Buffer.isNotEmpty: Boolean get() = !isEmpty | ||
|
||
public val Buffer.isFull: Boolean get() = availableForWrite == 0 | ||
public val Buffer.isNotFull: Boolean get() = !isFull | ||
|
||
public val Buffer.availableForRead: Int get() = writeIndex - readIndex | ||
public val Buffer.availableForWrite: Int get() = capacity - writeIndex | ||
|
||
internal fun Buffer.reset() { | ||
readIndex = 0 | ||
writeIndex = 0 | ||
} | ||
|
||
public operator fun Buffer.get(index: Int): Byte = getByteAt(index) | ||
|
||
public operator fun Buffer.set(index: Int, value: Byte) { | ||
setByteAt(index, value) | ||
} | ||
|
||
/** | ||
* Check if the Buffer has [count] bytes to read. | ||
* | ||
* @throws IndexOutOfBoundsException if the [count] is greater [availableForRead]. | ||
*/ | ||
public fun Buffer.ensureCanRead(count: Int) { | ||
if (availableForRead < count) { | ||
throw IndexOutOfBoundsException("Can't read $count bytes. Available: $availableForRead.") | ||
} | ||
} | ||
|
||
internal fun Buffer.ensureCanRead(index: Int, count: Int) { | ||
if (index + count > capacity) { | ||
throw IndexOutOfBoundsException("Can't read $count bytes at index $index. Capacity: $capacity.") | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Check if the Buffer has space to write [count] bytes. | ||
* | ||
* @throws IndexOutOfBoundsException if the [count] is greater [availableForWrite]. | ||
*/ | ||
public fun Buffer.ensureCanWrite(count: Int) { | ||
if (availableForWrite < count) { | ||
throw IndexOutOfBoundsException("Can't write $count bytes. Available space: $availableForWrite.") | ||
} | ||
} | ||
|
||
internal fun Buffer.ensureCanWrite(index: Int, count: Int) { | ||
if (index + count > capacity) { | ||
throw IndexOutOfBoundsException("Can't write $count bytes at index $index. Capacity: $capacity.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.