Skip to content

Commit aae062d

Browse files
author
Sergey Mashkov
committed
IO: add bytes counter to byte channel
1 parent 8a30b45 commit aae062d

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

core/kotlinx-coroutines-io/src/main/kotlin/kotlinx/coroutines/experimental/io/ByteBufferChannel.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ internal class ByteBufferChannel(
5757
override val isClosedForWrite: Boolean
5858
get() = closed != null
5959

60+
@Volatile
61+
override var totalBytesRead: Long = 0L
62+
private set
63+
64+
@Volatile
65+
override var totalBytesWritten: Long = 0L
66+
private set
67+
6068
override fun close(cause: Throwable?): Boolean {
6169
if (closed != null) return false
6270
val newClosed = if (cause == null) ClosedElement.EmptyCause else ClosedElement(cause)
@@ -563,13 +571,15 @@ internal class ByteBufferChannel(
563571

564572
writePosition = carryIndex(writePosition + n)
565573
c.completeWrite(n)
574+
totalBytesWritten += n
566575
}
567576

568577
private fun ByteBuffer.bytesRead(c: RingBufferCapacity, n: Int) {
569578
require(n >= 0)
570579

571580
readPosition = carryIndex(readPosition + n)
572581
c.completeRead(n)
582+
totalBytesRead += n
573583
resumeWriteOp()
574584
}
575585

core/kotlinx-coroutines-io/src/main/kotlin/kotlinx/coroutines/experimental/io/ByteReadChannel.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public interface ByteReadChannel {
3030
*/
3131
public var readByteOrder: ByteOrder
3232

33+
/**
34+
* Number of bytes read from the channel.
35+
* It is not guaranteed to be atomic so could be updated in the middle of long running read operation.
36+
*/
37+
public val totalBytesRead: Long
38+
3339
/**
3440
* Reads all available bytes to [dst] buffer and returns immediately or suspends if no bytes available
3541
* @return number of bytes were read or `-1` if the channel has been closed

core/kotlinx-coroutines-io/src/main/kotlin/kotlinx/coroutines/experimental/io/ByteWriteChannel.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public interface ByteWriteChannel {
3636
*/
3737
public var writeByteOrder: ByteOrder
3838

39+
/**
40+
* Number of bytes written to the channel.
41+
* It is not guaranteed to be atomic so could be updated in the middle of write operation.
42+
*/
43+
public val totalBytesWritten: Long
44+
3945
/**
4046
* Writes as much as possible and only suspends if buffer is full
4147
*/

0 commit comments

Comments
 (0)