@@ -328,7 +328,13 @@ internal class ByteBufferChannel(
328
328
val consumed = readAsMuchAsPossible(dst, offset, length)
329
329
330
330
return when {
331
- consumed == 0 && closed != null -> - 1
331
+ consumed == 0 && closed != null -> {
332
+ if (state.capacity.flush()) {
333
+ return readAsMuchAsPossible(dst, offset, length)
334
+ } else {
335
+ - 1
336
+ }
337
+ }
332
338
consumed > 0 || length == 0 -> consumed
333
339
else -> readAvailableSuspend(dst, offset, length)
334
340
}
@@ -338,7 +344,13 @@ internal class ByteBufferChannel(
338
344
val consumed = readAsMuchAsPossible(dst)
339
345
340
346
return when {
341
- consumed == 0 && closed != null -> - 1
347
+ consumed == 0 && closed != null -> {
348
+ if (state.capacity.flush()) {
349
+ return readAsMuchAsPossible(dst)
350
+ } else {
351
+ - 1
352
+ }
353
+ }
342
354
consumed > 0 || ! dst.hasRemaining() -> consumed
343
355
else -> readAvailableSuspend(dst)
344
356
}
@@ -1268,11 +1280,12 @@ internal class ByteBufferChannel(
1268
1280
}
1269
1281
1270
1282
private tailrec suspend fun readSuspend (size : Int ): Boolean {
1271
- if (state.capacity.availableForRead >= size) return true
1283
+ val capacity = state.capacity
1284
+ if (capacity.availableForRead >= size) return true
1272
1285
1273
1286
closed?.let { c ->
1274
- if (c.cause == null ) return false
1275
- throw c.cause
1287
+ if (c.cause != null ) throw c.cause
1288
+ return capacity.flush() && capacity.availableForRead >= size
1276
1289
}
1277
1290
1278
1291
if (! readSuspendImpl(size)) return false
@@ -1291,13 +1304,12 @@ internal class ByteBufferChannel(
1291
1304
}
1292
1305
1293
1306
closed?.let {
1294
- if (it.cause == null && state.capacity.availableForRead == 0 ) {
1295
- c.resume(false )
1296
- return @suspendCancellableCoroutine
1297
- } else if (it.cause != null ) {
1307
+ if (it.cause != null ) {
1298
1308
c.resumeWithException(it.cause)
1299
- return @suspendCancellableCoroutine
1309
+ } else {
1310
+ c.resume(state.capacity.flush() && state.capacity.availableForRead >= size)
1300
1311
}
1312
+ return @suspendCancellableCoroutine
1301
1313
}
1302
1314
} while (! setContinuation({ readOp }, ReadOp , c, { closed == null && state.capacity.availableForRead < size }))
1303
1315
}
0 commit comments