Skip to content

Commit

Permalink
IO: Implement ByteOrder with typealias
Browse files Browse the repository at this point in the history
  • Loading branch information
elizarov committed Aug 15, 2017
1 parent 8376203 commit fccac1d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class ByteBufferChannel internal constructor(
val bufferLimit = capacity() - reservedSize
val virtualLimit = position + available

order(order.forNio)
order(order)
limit(virtualLimit.coerceAtMost(bufferLimit))
position(position)
}
Expand Down Expand Up @@ -1249,8 +1249,8 @@ class ByteBufferChannel internal constructor(
private fun newBuffer(): ReadWriteBufferState.Initial {
val result = pool.borrow()

result.readBuffer.order(readByteOrder.forNio)
result.writeBuffer.order(writeByteOrder.forNio)
result.readBuffer.order(readByteOrder)
result.writeBuffer.order(writeByteOrder)
result.capacity.reset()

return result
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
package kotlinx.coroutines.experimental.io

enum class ByteOrder {
BIG_ENDIAN,
LITTLE_ENDIAN;

companion object {
val native: ByteOrder = when (java.nio.ByteOrder.nativeOrder()) {
java.nio.ByteOrder.BIG_ENDIAN -> BIG_ENDIAN
java.nio.ByteOrder.LITTLE_ENDIAN -> LITTLE_ENDIAN
else -> throw UnsupportedOperationException()
}
}
}
/**
* Byte order.
*/
typealias ByteOrder = java.nio.ByteOrder
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package kotlinx.coroutines.experimental.io.internal

import kotlinx.coroutines.experimental.io.ByteOrder
import java.nio.ByteBuffer
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater
import java.util.concurrent.atomic.AtomicLongFieldUpdater
Expand All @@ -9,12 +8,6 @@ import kotlin.reflect.KProperty1

internal fun ByteBuffer.isEmpty() = !hasRemaining()

internal val ByteOrder.forNio: java.nio.ByteOrder
get() = when (this) {
ByteOrder.BIG_ENDIAN -> java.nio.ByteOrder.BIG_ENDIAN
ByteOrder.LITTLE_ENDIAN -> java.nio.ByteOrder.LITTLE_ENDIAN
}

internal inline fun <reified Owner : Any> longUpdater(p: KProperty1<Owner, Long>): AtomicLongFieldUpdater<Owner> {
return AtomicLongFieldUpdater.newUpdater(Owner::class.java, p.name)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,16 @@ class ByteBufferChannelTest {
}
}



@Test
fun testEndianMix() {
val byteOrders = listOf(ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN)
runBlocking {
for (writeOrder in ByteOrder.values()) {
for (writeOrder in byteOrders) {
ch.writeByteOrder = writeOrder

for (readOrder in ByteOrder.values()) {
for (readOrder in byteOrders) {
ch.readByteOrder = readOrder

assertEquals(0, ch.remaining)
Expand Down

0 comments on commit fccac1d

Please sign in to comment.