Skip to content

Commit

Permalink
Reformatting code, fixing some documentation and adding code quality …
Browse files Browse the repository at this point in the history
…badge.
  • Loading branch information
kristoffer-paulsson committed May 3, 2022
1 parent 3c2d05e commit 3f29a93
Show file tree
Hide file tree
Showing 24 changed files with 56 additions and 36 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# angelos-project-buffer
A data-buffer made to be used without old legacy, in a modern asynchronous environment. With all necessary fundaments.

[![CircleCI](https://circleci.com/gh/angelos-project/angelos-project-buffer/tree/master.svg?style=shield)](https://circleci.com/gh/angelos-project/angelos-project-buffer/tree/master)
[![CircleCI](https://circleci.com/gh/angelos-project/angelos-project-buffer/tree/master.svg?style=shield)](https://circleci.com/gh/angelos-project/angelos-project-buffer/tree/master)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/0a19e154711047e19fef3daf79864d9a)](https://www.codacy.com/gh/angelos-project/angelos-project-buffer/dashboard?utm_source=github.com&utm_medium=referral&utm_content=angelos-project/angelos-project-buffer&utm_campaign=Badge_Grade)
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ abstract class AbstractBuffer internal constructor(
size: Int,
limit: Int,
position: Int,
endianness: Endianness
): Buffer {
endianness: Endianness,
) : Buffer {
override val size: Int = size.absoluteValue

private var _limit: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ abstract class AbstractByteBuffer internal constructor(
size: Int,
limit: Int,
position: Int,
endianness: Endianness
endianness: Endianness,
) : AbstractBuffer(size, limit, position, endianness) {

override fun getNextByte(): Byte {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ abstract class AbstractMutableByteBuffer internal constructor(
size: Int,
limit: Int,
position: Int,
endianness: Endianness
endianness: Endianness,
) : AbstractByteBuffer(size, limit, position, endianness), MutableBuffer {

override fun setNextByte(value: Byte) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ abstract class AbstractMutableNativeByteBuffer internal constructor(
size: Int,
limit: Int,
position: Int,
endianness: Endianness
endianness: Endianness,
) : AbstractMutableByteBuffer(size, limit, position, endianness)
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ abstract class AbstractNativeByteBuffer internal constructor(
size: Int,
limit: Int,
position: Int,
endianness: Endianness
endianness: Endianness,
) : AbstractByteBuffer(size, limit, position, endianness)
8 changes: 4 additions & 4 deletions buffer/src/commonMain/kotlin/org/angelos/io/buf/Buffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package org.angelos.io.buf
*
* @constructor Create empty Buffer
*/
interface Buffer: Gettable {
interface Buffer : Gettable {

/**
* Total size of the buffer.
Expand Down Expand Up @@ -156,20 +156,20 @@ interface Buffer: Gettable {

internal inline fun copyNonOptimized(
src: Buffer, srcOffset: Int,
dst: MutableBuffer, dstOffset: Int, length: Int
dst: MutableBuffer, dstOffset: Int, length: Int,
) {
for (index in 0 until length)
dst.saveByte(dstOffset + index, src.loadByte(srcOffset + index))
}

internal inline fun copySemiOptimized(
src: Buffer, srcOffset: Int,
dst: MutableBuffer, dstOffset: Int, length: Int
dst: MutableBuffer, dstOffset: Int, length: Int,
) = copyFullyOptimized(src, srcOffset, dst, dstOffset, length)

internal inline fun copyFullyOptimized(
src: Buffer, srcOffset: Int,
dst: MutableBuffer, dstOffset: Int, length: Int
dst: MutableBuffer, dstOffset: Int, length: Int,
) {
val breakPoint = length - length % LONG_SIZE
for (index in 0 until breakPoint step LONG_SIZE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ByteBuffer internal constructor(
size: Int,
limit: Int,
position: Int,
endianness: Endianness
endianness: Endianness,
) : AbstractByteBuffer(size, limit, position, endianness), ImmutableHeapBuffer {
private val _array = array
private val _view = _array.asUByteArray()
Expand Down
22 changes: 13 additions & 9 deletions buffer/src/commonMain/kotlin/org/angelos/io/buf/Ext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package org.angelos.io.buf
*/
fun byteBufferOf(
array: ByteArray,
endian: Endianness = Buffer.nativeEndianness
endian: Endianness = Buffer.nativeEndianness,
): ImmutableHeapBuffer = ByteBuffer(array, array.size, array.size, 0, endian)

/**
Expand All @@ -37,7 +37,7 @@ fun byteBufferOf(
fun mutableByteBufferOf(
array: ByteArray,
limit: Int = array.size,
endian: Endianness = Buffer.nativeEndianness
endian: Endianness = Buffer.nativeEndianness,
): MutableHeapBuffer = MutableByteBuffer(array, array.size, limit, 0, endian)

/**
Expand All @@ -51,7 +51,7 @@ fun mutableByteBufferOf(
fun mutableByteBufferOf(
size: Int,
limit: Int = size,
endian: Endianness = Buffer.nativeEndianness
endian: Endianness = Buffer.nativeEndianness,
): MutableHeapBuffer = MutableByteBuffer(ByteArray(size), size, limit, 0, endian)

/**
Expand All @@ -63,7 +63,7 @@ fun mutableByteBufferOf(
*/
fun nativeByteBufferOf(
size: Int,
endian: Endianness = Buffer.nativeEndianness
endian: Endianness = Buffer.nativeEndianness,
): ImmutableNativeBuffer = NativeByteBuffer(size, size, 0, endian)

/**
Expand All @@ -80,8 +80,10 @@ fun wrapIntoNativeByteBufferOf(
pointer: Long,
size: Int,
limit: Int = size,
endian: Endianness = Buffer.nativeEndianness
): ImmutableNativeBuffer { throw UnsupportedOperationException() }
endian: Endianness = Buffer.nativeEndianness,
): ImmutableNativeBuffer {
throw UnsupportedOperationException()
}

/**
* Create MutableNativeBuffer with an underlying blank ByteBuffer of certain size.
Expand All @@ -94,7 +96,7 @@ fun wrapIntoNativeByteBufferOf(
fun mutableNativeByteBufferOf(
size: Int,
limit: Int = size,
endian: Endianness = Buffer.nativeEndianness
endian: Endianness = Buffer.nativeEndianness,
): MutableNativeBuffer = MutableNativeByteBuffer(size, limit, 0, endian)

/**
Expand All @@ -111,8 +113,10 @@ fun wrapIntoMutableNativeByteBufferOf(
pointer: Long,
size: Int,
limit: Int = size,
endian: Endianness = Buffer.nativeEndianness
): MutableNativeBuffer { throw UnsupportedOperationException() }
endian: Endianness = Buffer.nativeEndianness,
): MutableNativeBuffer {
throw UnsupportedOperationException()
}

/**
* To mutable heap byte buffer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package org.angelos.io.buf
*
* @constructor Create empty Heap buffer
*/
interface HeapBuffer: Buffer {
interface HeapBuffer : Buffer {

/**
* Exposing the underlying ByteArray of the buffer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MutableByteBuffer internal constructor(
size: Int,
limit: Int,
position: Int,
endianness: Endianness
endianness: Endianness,
) : AbstractMutableByteBuffer(size, limit, position, endianness), MutableHeapBuffer {
private val _array = array
private val _view = _array.asUByteArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MutableNativeByteBuffer internal constructor(
size: Int,
limit: Int,
position: Int,
endianness: Endianness
endianness: Endianness,
) : AbstractMutableNativeByteBuffer(size, limit, position, endianness), MutableNativeBuffer {
private val _array = ByteArray(size)
private val _view = _array.asUByteArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package org.angelos.io.buf
*
* @constructor Create empty Native buffer
*/
interface NativeBuffer: Buffer {
interface NativeBuffer : Buffer {

/**
* Memory allocated externally outside the buffer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class NativeByteBuffer internal constructor(
size: Int,
limit: Int,
position: Int,
endianness: Endianness
endianness: Endianness,
) : AbstractNativeByteBuffer(size, limit, position, endianness), ImmutableNativeBuffer {
private val _array = ByteArray(size)
private val _view = _array.asUByteArray()
Expand Down
4 changes: 2 additions & 2 deletions buffer/src/commonTest/kotlin/org/angelos/io/buf/BufferTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ open class BufferTest {
assertEquals(buf.getNextUInt(), refUInt)
assertEquals(buf.getNextLong(), refLong)
assertEquals(buf.getNextULong(), refULong)
for(idx in buf.position until size){
for (idx in buf.position until size) {
val data = buf.getNextByte()
assertEquals(data, refRead[idx])
}
Expand All @@ -108,7 +108,7 @@ open class BufferTest {
buf.setNextUInt(refUInt)
buf.setNextLong(refLong)
buf.setNextULong(refULong)
for(idx in buf.position until size)
for (idx in buf.position until size)
buf.setNextByte(refWrite[idx])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import kotlin.test.Test
*
* @constructor Create empty Byte buffer test
*/
class ByteBufferTest: BufferTest() {
class ByteBufferTest : BufferTest() {

/**
* Running tests on the ByteBuffer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ open class AbstractEndianTest {
}

/**
* Runing a test for toString().
* Running a test for toString().
*
*/
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class MutableByteBufferTest : BufferTest() {
readAny(mnbuf)

val buf2 = mutableByteBufferOf(size)
for(idx in buf2.position until buf2.size) {
for (idx in buf2.position until buf2.size) {
buf2.setNextByte(idx.toByte())
}
buf2.rewind()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import kotlin.test.Test
*
* @constructor Create empty Native byte buffer test
*/
class MutableNativeByteBufferTest: BufferTest() {
class MutableNativeByteBufferTest : BufferTest() {

/**
* Running tests on the MutableNativeByteBuffer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import kotlin.test.Test
*
* @constructor Create empty Native byte buffer test
*/
class NativeByteBufferTest: BufferTest() {
class NativeByteBufferTest : BufferTest() {

/**
* Running tests on the NativeByteBuffer.
Expand Down
4 changes: 2 additions & 2 deletions buffer/src/jsMain/kotlin/org.angelos.io.buf/Internals.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import org.khronos.webgl.set
import kotlin.experimental.and

internal actual class Internals {
actual companion object{
actual companion object {
actual fun getEndian(): Int {
val array = Uint8Array(4)
val view = Uint32Array(array.buffer)
view[0] = 1
return when((view[0].toShort() and array[0].toShort()).countOneBits()) {
return when ((view[0].toShort() and array[0].toShort()).countOneBits()) {
0 -> 1
1 -> 2
else -> 0
Expand Down
5 changes: 5 additions & 0 deletions buffer/src/jsTest/kotlin/org/angelos/io/buf/EndiannessTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ package org.angelos.io.buf
import kotlin.test.Test
import kotlin.test.assertNotNull

/**
* Running tests for Endianness.
*
* @constructor Create empty Endianness test
*/
actual class EndiannessTest : AbstractEndianTest() {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ import java.nio.ByteOrder
import kotlin.test.Test
import kotlin.test.assertEquals

/**
* Running tests for Endianness.
*
* @constructor Create empty Endianness test
*/
actual class EndiannessTest : AbstractEndianTest() {

/**
* Running tests for nativeOrder on JVM checks against java.nio.ByteOrder for correctness.
*/
@Test
actual fun nativeOrder() {
when(Endianness.nativeOrder()) {
when (Endianness.nativeOrder()) {
Endianness.BIG_ENDIAN -> assertEquals(ByteOrder.nativeOrder(), ByteOrder.BIG_ENDIAN)
Endianness.LITTLE_ENDIAN -> assertEquals(ByteOrder.nativeOrder(), ByteOrder.LITTLE_ENDIAN)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import kotlin.native.Platform.isLittleEndian
import kotlin.test.Test
import kotlin.test.assertEquals

/**
* Running tests for Endianness.
*
* @constructor Create empty Endianness test
*/
actual class EndiannessTest : AbstractEndianTest() {

/**
Expand Down

0 comments on commit 3f29a93

Please sign in to comment.