Skip to content

Commit de94a64

Browse files
author
Sergey Mashkov
committed
Fix empty chunk view creation
1 parent fc783a4 commit de94a64

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
- Added a cpointer constructor to native IoBuffer so that IoBuffer can be used to read AND write on a memory chunk
99
- Made ByteChannel pass original cause from the owner job
1010
- Fixed reading UTF-8 lines
11+
- Fixed empty chunk view creation
12+
- Utility functions takeWhile* improvements
1113

1214
# 0.1.0
1315
> Published 15 Nov 2018

kotlinx-io-js/src/main/kotlin/kotlinx/io/core/IoBufferJS.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,8 @@ actual class IoBuffer internal constructor(
957957
actual fun isExclusivelyOwned(): Boolean = refCount == 1
958958

959959
actual fun makeView(): IoBuffer {
960+
if (this === Empty) return this
961+
960962
val o = origin ?: this
961963
o.acquire()
962964

kotlinx-io-jvm/src/main/kotlin/kotlinx/io/core/IoBufferJVM.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,8 @@ actual class IoBuffer private constructor(
739739
* Creates a new view to the same actual buffer with independant read and write positions and gaps
740740
*/
741741
actual fun makeView(): IoBuffer {
742+
if (this === Empty) return this
743+
742744
val newOrigin = origin ?: this
743745
newOrigin.acquire()
744746

@@ -863,12 +865,16 @@ actual class IoBuffer private constructor(
863865
}
864866

865867
private fun releaseRefCount(): Boolean {
866-
if (this === Empty) throw IllegalArgumentException("Attempted to release empty")
868+
if (this === Empty) {
869+
throw IllegalArgumentException("Attempted to release empty")
870+
}
867871
while (true) {
868872
val value = refCount
869873
val newValue = value - 1
870874

871-
if (value == 0L) throw IllegalStateException("Unable to release: already released")
875+
if (value == 0L) {
876+
throw IllegalStateException("Unable to release: already released")
877+
}
872878
if (RefCount.compareAndSet(this, value, newValue)) {
873879
return newValue == 0L
874880
}

kotlinx-io-native/src/main/kotlin/kotlinx/io/core/IoBufferNative.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,8 @@ actual class IoBuffer internal constructor(
980980
actual fun isExclusivelyOwned(): Boolean = refCount == 1
981981

982982
actual fun makeView(): IoBuffer {
983+
if (this === Empty) return this
984+
983985
val o = origin ?: this
984986
o.acquire()
985987

0 commit comments

Comments
 (0)