@@ -294,7 +294,7 @@ internal class ByteBufferChannel(
294
294
private fun ByteBuffer.carryIndex (idx : Int ) = if (idx >= capacity() - reservedSize) idx - (capacity() - reservedSize) else idx
295
295
296
296
private inline fun writing (block : ByteBufferChannel .(ByteBuffer , RingBufferCapacity ) -> Unit ) {
297
- val current = resolveDelegation(this )
297
+ val current = joining?. let { resolveDelegation(this , it) } ? : this
298
298
val buffer = current.setupStateForWrite() ? : return
299
299
val capacity = current.state.capacity
300
300
@@ -754,9 +754,13 @@ internal class ByteBufferChannel(
754
754
resumeWriteOp()
755
755
}
756
756
757
- private tailrec fun resolveDelegation (current : ByteBufferChannel ): ByteBufferChannel {
758
- val joined = current.joining ? : return current
759
- if (current.state == = ReadWriteBufferState .Terminated ) return resolveDelegation(joined.delegatedTo)
757
+ private tailrec fun resolveDelegation (current : ByteBufferChannel , joining : JoiningState ): ByteBufferChannel {
758
+ if (current.state == = ReadWriteBufferState .Terminated ) {
759
+ val joinedTo = joining.delegatedTo
760
+ val nextJoining = joinedTo.joining ? : return joinedTo
761
+ return resolveDelegation(joinedTo, nextJoining)
762
+ }
763
+
760
764
return current
761
765
}
762
766
@@ -774,8 +778,7 @@ internal class ByteBufferChannel(
774
778
}
775
779
776
780
suspend override fun writeByte (b : Byte ) {
777
- val delegated = resolveDelegation(this )
778
- if (delegated != = this ) return delegated.writeByte(b)
781
+ joining?.let { return resolveDelegation(this , it).writeByte(b) }
779
782
780
783
val buffer = setupStateForWrite() ? : return delegateByte(b)
781
784
val c = state.capacity
@@ -823,8 +826,7 @@ internal class ByteBufferChannel(
823
826
}
824
827
825
828
suspend override fun writeShort (s : Short ) {
826
- val delegated = resolveDelegation(this )
827
- if (delegated != = this ) return delegated.writeShort(s)
829
+ joining?.let { return resolveDelegation(this , it).writeShort(s) }
828
830
829
831
val buffer = setupStateForWrite() ? : return delegateShort(s)
830
832
val c = state.capacity
@@ -904,7 +906,7 @@ internal class ByteBufferChannel(
904
906
suspend override fun writeInt (i : Int ) {
905
907
val buffer = setupStateForWrite()
906
908
if (buffer == null ) {
907
- val delegation = resolveDelegation(this )
909
+ val delegation = resolveDelegation(this , joining !! )
908
910
if (delegation != = this ) return delegation.writeInt(i)
909
911
else return delegateSuspend(joining!! , { writeInt(i) })
910
912
}
@@ -963,8 +965,7 @@ internal class ByteBufferChannel(
963
965
}
964
966
965
967
suspend override fun writeLong (l : Long ) {
966
- val delegated = resolveDelegation(this )
967
- if (delegated != = this ) return delegated.writeLong(l)
968
+ joining?.let { return resolveDelegation(this , it).writeLong(l) }
968
969
969
970
val buffer = setupStateForWrite() ? : return delegateLong(l)
970
971
val c = state.capacity
@@ -1003,43 +1004,43 @@ internal class ByteBufferChannel(
1003
1004
}
1004
1005
1005
1006
suspend override fun writeAvailable (src : ByteBuffer ): Int {
1006
- val delegated = resolveDelegation(this )
1007
- if (delegated != = this ) return delegated.writeAvailable(src)
1007
+ joining?.let { return resolveDelegation(this , it).writeAvailable(src) }
1008
1008
1009
1009
val copied = writeAsMuchAsPossible(src)
1010
1010
if (copied > 0 ) return copied
1011
1011
1012
- return resolveDelegation(this ).writeAvailableSuspend(src)
1012
+ joining?.let { return resolveDelegation(this , it).writeAvailableSuspend(src) }
1013
+ return writeAvailableSuspend(src)
1013
1014
}
1014
1015
1015
1016
suspend override fun writeAvailable (src : BufferView ): Int {
1017
+ joining?.let { return resolveDelegation(this , it).writeAvailable(src) }
1018
+
1016
1019
val copied = writeAsMuchAsPossible(src)
1017
1020
if (copied > 0 ) return copied
1018
1021
1019
- return resolveDelegation(this ).writeAvailableSuspend(src)
1022
+ joining?.let { return resolveDelegation(this , it).writeAvailableSuspend(src) }
1023
+ return writeAvailableSuspend(src)
1020
1024
}
1021
1025
1022
1026
private suspend fun writeAvailableSuspend (src : ByteBuffer ): Int {
1023
1027
writeSuspend(1 ) // here we don't need to restoreStateAfterWrite as write copy loop doesn't hold state
1024
1028
1025
- val delegated = resolveDelegation(this )
1026
- if (delegated != = this ) return delegated.writeAvailableSuspend(src)
1029
+ joining?.let { return resolveDelegation(this , it).writeAvailableSuspend(src) }
1027
1030
1028
1031
return writeAvailable(src)
1029
1032
}
1030
1033
1031
1034
private suspend fun writeAvailableSuspend (src : BufferView ): Int {
1032
1035
writeSuspend(1 )
1033
1036
1034
- val delegated = resolveDelegation(this )
1035
- if (delegated != = this ) return delegated.writeAvailableSuspend(src)
1037
+ joining?.let { return resolveDelegation(this , it).writeAvailableSuspend(src) }
1036
1038
1037
1039
return writeAvailable(src)
1038
1040
}
1039
1041
1040
1042
suspend override fun writeFully (src : ByteBuffer ) {
1041
- val delegated = resolveDelegation(this )
1042
- if (delegated != = this ) return delegated.writeFully(src)
1043
+ joining?.let { return resolveDelegation(this , it).writeFully(src) }
1043
1044
1044
1045
writeAsMuchAsPossible(src)
1045
1046
if (! src.hasRemaining()) return
@@ -1058,8 +1059,7 @@ internal class ByteBufferChannel(
1058
1059
while (src.hasRemaining()) {
1059
1060
writeSuspend(1 )
1060
1061
1061
- val delegated = resolveDelegation(this )
1062
- if (delegated != = this ) return delegated.writeFullySuspend(src)
1062
+ joining?.let { return resolveDelegation(this , it).writeFully(src) }
1063
1063
1064
1064
writeAsMuchAsPossible(src)
1065
1065
}
@@ -1069,8 +1069,7 @@ internal class ByteBufferChannel(
1069
1069
while (src.canRead()) {
1070
1070
writeSuspend(1 )
1071
1071
1072
- val delegated = resolveDelegation(this )
1073
- if (delegated != = this ) return delegated.writeFullySuspend(src)
1072
+ joining?.let { return resolveDelegation(this , it).writeFully(src) }
1074
1073
1075
1074
writeAsMuchAsPossible(src)
1076
1075
}
@@ -1314,8 +1313,7 @@ internal class ByteBufferChannel(
1314
1313
}
1315
1314
1316
1315
suspend override fun writeFully (src : ByteArray , offset : Int , length : Int ) {
1317
- val delegated = resolveDelegation(this )
1318
- if (delegated != = this ) return delegated.writeFully(src, offset, length)
1316
+ joining?.let { return resolveDelegation(this , it).writeFully(src, offset, length) }
1319
1317
1320
1318
var rem = length
1321
1319
var off = offset
@@ -1340,8 +1338,7 @@ internal class ByteBufferChannel(
1340
1338
}
1341
1339
1342
1340
suspend override fun writeAvailable (src : ByteArray , offset : Int , length : Int ): Int {
1343
- val delegated = resolveDelegation(this )
1344
- if (delegated != = this ) return delegated.writeAvailable(src, offset, length)
1341
+ joining?.let { return resolveDelegation(this , it).writeAvailable(src, offset, length) }
1345
1342
1346
1343
val size = writeAsMuchAsPossible(src, offset, length)
1347
1344
if (size > 0 ) return size
@@ -1352,8 +1349,7 @@ internal class ByteBufferChannel(
1352
1349
while (true ) {
1353
1350
writeSuspend(1 )
1354
1351
1355
- val delegated = resolveDelegation(this )
1356
- if (delegated != = this ) return delegated.writeSuspend(src, offset, length)
1352
+ joining?.let { return resolveDelegation(this , it).writeSuspend(src, offset, length) }
1357
1353
1358
1354
val size = writeAsMuchAsPossible(src, offset, length)
1359
1355
if (size > 0 ) return size
@@ -1387,7 +1383,8 @@ internal class ByteBufferChannel(
1387
1383
1388
1384
private suspend fun writeBlockSuspend (min : Int , block : (ByteBuffer ) -> Unit ) {
1389
1385
writeSuspend(min)
1390
- resolveDelegation(this ).write(min, block)
1386
+ joining?.let { return resolveDelegation(this , it).write(min, block) }
1387
+ return write(min, block)
1391
1388
}
1392
1389
1393
1390
override suspend fun read (min : Int , block : (ByteBuffer ) -> Unit ) {
@@ -1420,8 +1417,7 @@ internal class ByteBufferChannel(
1420
1417
}
1421
1418
1422
1419
suspend override fun writePacket (packet : ByteReadPacket ) {
1423
- val delegated = resolveDelegation(this )
1424
- if (delegated != = this ) return delegated.writePacket(packet)
1420
+ joining?.let { return resolveDelegation(this , it).writePacket(packet) }
1425
1421
1426
1422
try {
1427
1423
while (! packet.isEmpty) {
@@ -1433,16 +1429,17 @@ internal class ByteBufferChannel(
1433
1429
}
1434
1430
1435
1431
if (packet.remaining > 0 ) {
1436
- return resolveDelegation(this ).writePacketSuspend(packet)
1432
+ joining?.let { return resolveDelegation(this , it).writePacket(packet) }
1433
+ return writePacketSuspend(packet)
1437
1434
}
1438
1435
}
1439
1436
1440
1437
private suspend fun writePacketSuspend (packet : ByteReadPacket ) {
1441
1438
try {
1442
1439
while (! packet.isEmpty) {
1443
1440
writeSuspend(1 )
1444
- val delegated = resolveDelegation( this )
1445
- if (delegated != = this ) return delegated.writePacketSuspend (packet)
1441
+
1442
+ joining?. let { return resolveDelegation( this , it).writePacket (packet) }
1446
1443
tryWritePacketPart(packet)
1447
1444
}
1448
1445
} finally {
0 commit comments