Skip to content

Commit

Permalink
Added missing documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
kristoffer-paulsson committed May 3, 2022
1 parent df67462 commit 3c2d05e
Show file tree
Hide file tree
Showing 22 changed files with 239 additions and 0 deletions.
11 changes: 11 additions & 0 deletions buffer/src/commonMain/kotlin/org/angelos/io/buf/AbstractBuffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ package org.angelos.io.buf
import kotlin.math.absoluteValue
import kotlin.math.min

/**
* Abstract buffer from which all buffer implementations must inherit.
* Implements the basic logic regarding size, position and limit of reading and writing space.
*
* @constructor
*
* @param size max size of the buffer
* @param limit initial limit if partial data already exists
* @param position initial position in an already existing data stream
* @param endianness endian of the buffered data
*/
abstract class AbstractBuffer internal constructor(
size: Int,
limit: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
*/
package org.angelos.io.buf

/**
* Abstract byte buffer implements the read and endianness logic directly for use.
*
* @constructor
*
* @param size
* @param limit
* @param position
* @param endianness
*/
abstract class AbstractByteBuffer internal constructor(
size: Int,
limit: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
*/
package org.angelos.io.buf

/**
* Abstract mutable byte buffer implements the logic for write operations.
*
* @constructor
*
* @param size
* @param limit
* @param position
* @param endianness
*/
abstract class AbstractMutableByteBuffer internal constructor(
size: Int,
limit: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
*/
package org.angelos.io.buf

/**
* Abstract mutable native byte buffer.
*
* @constructor
*
* @param size
* @param limit
* @param position
* @param endianness
*/
abstract class AbstractMutableNativeByteBuffer internal constructor(
size: Int,
limit: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
*/
package org.angelos.io.buf

/**
* Abstract native byte buffer.
*
* @constructor
*
* @param size
* @param limit
* @param position
* @param endianness
*/
abstract class AbstractNativeByteBuffer internal constructor(
size: Int,
limit: Int,
Expand Down
8 changes: 8 additions & 0 deletions buffer/src/commonMain/kotlin/org/angelos/io/buf/Buffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ interface Buffer: Gettable {
*/
fun hasRemaining(size: Int) = hasRemaining(this, size)

/**
* Copy into a mutable buffer.
*
* @param destination destination mutable buffer to copy into
* @param destinationOffset offset where to start inside mutable buffer
* @param startIndex where to start copy from in source buffer
* @param endIndex when to stop copying from the source buffer
*/
fun copyInto(destination: MutableBuffer, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) {
if (destination == this)
throw IllegalArgumentException("It's not allowed for a buffer to copy into itself.")
Expand Down
11 changes: 11 additions & 0 deletions buffer/src/commonMain/kotlin/org/angelos/io/buf/ByteBuffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
*/
package org.angelos.io.buf

/**
* Byte buffer implemented on the heap, as immutable.
*
* @constructor
*
* @param array ByteArray to wrap into a buffer
* @param size
* @param limit
* @param position
* @param endianness
*/
@Suppress("OVERRIDE_BY_INLINE")
@OptIn(ExperimentalUnsignedTypes::class)
class ByteBuffer internal constructor(
Expand Down
40 changes: 40 additions & 0 deletions buffer/src/commonMain/kotlin/org/angelos/io/buf/Endianness.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,61 @@
*/
package org.angelos.io.buf

/**
* Representing endianness in systems and data buffers or streams.
*
* @property endian
* @constructor Create empty Endianness
*/
enum class Endianness(val endian: Boolean) {

/**
* Big endian enumeration value.
*
* @constructor Create empty Big Endian
*/
BIG_ENDIAN(false),

/**
* Little endian enumeration value.
*
* @constructor Create empty Little Endian
*/
LITTLE_ENDIAN(true);

/**
* Checks if the endianness is big.
*
* @return
*/
fun isBig(): Boolean = endian == BIG_ENDIAN.endian

/**
* Checks if the endianness is little.
*
* @return
*/
fun isLittle(): Boolean = endian == LITTLE_ENDIAN.endian

companion object {

/**
* Native order of the running environment.
*
* @return the endian
*/
fun nativeOrder(): Endianness = when (Internals.getEndian()) {
1 -> BIG_ENDIAN
2 -> LITTLE_ENDIAN
else -> throw BufferException("Unknown type of endian.")
}
}

/**
* String representation of the endianness.
*
* @return human readable endianness
*/
override fun toString(): String = when (endian) {
false -> "BIG_ENDIAN"
true -> "LITTLE_ENDIAN"
Expand Down
5 changes: 5 additions & 0 deletions buffer/src/commonMain/kotlin/org/angelos/io/buf/Gettable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
*/
package org.angelos.io.buf

/**
* Gettable interface to read data from a stream or buffer.
*
* @constructor Create empty Gettable
*/
interface Gettable {
/**
* Get next byte.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
*/
package org.angelos.io.buf

/**
* Mutable byte buffer implemented on the heap, as mutable.
*
* @constructor
*
* @param array
* @param size
* @param limit
* @param position
* @param endianness
*/
@Suppress("OVERRIDE_BY_INLINE")
@OptIn(ExperimentalUnsignedTypes::class)
class MutableByteBuffer internal constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
*/
package org.angelos.io.buf

/**
* Mutable native byte buffer implemented outside save memory environment as mutable.
*
* @constructor
*
* @param size
* @param limit
* @param position
* @param endianness
*/
@Suppress("OVERRIDE_BY_INLINE")
@OptIn(ExperimentalUnsignedTypes::class)
class MutableNativeByteBuffer internal constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
*/
package org.angelos.io.buf

/**
* Native byte buffer implemented outside save memory environment as immutable.
*
* @constructor
*
* @param size
* @param limit
* @param position
* @param endianness
*/
@Suppress("OVERRIDE_BY_INLINE")
@OptIn(ExperimentalUnsignedTypes::class)
class NativeByteBuffer internal constructor(
Expand Down
5 changes: 5 additions & 0 deletions buffer/src/commonMain/kotlin/org/angelos/io/buf/Settable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
*/
package org.angelos.io.buf

/**
* Settable interface to write data to a stream or buffer.
*
* @constructor Create empty Settable
*/
interface Settable {
/**
* Set next byte.
Expand Down
16 changes: 16 additions & 0 deletions buffer/src/commonTest/kotlin/org/angelos/io/buf/BufferTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ package org.angelos.io.buf

import kotlin.test.assertEquals

/**
* Containing tests for asserting and running all getNext*() and setNext*() methods in buffer implementations.
*
* @constructor Create empty Buffer test
*/
open class BufferTest {
val refRead = byteArrayOf(
0B1010101, 0B10101010u.toByte(),
Expand Down Expand Up @@ -68,6 +73,11 @@ open class BufferTest {

val size: Int = refWrite.size

/**
* Read any buffer and make asserts on all getNext*() methods.
*
* @param buf
*/
fun readAny(buf: Buffer) {
assertEquals(buf.getNextByte(), refByte)
assertEquals(buf.getNextUByte(), refUByte)
Expand All @@ -83,6 +93,12 @@ open class BufferTest {
}
}

/**
* Write any buffer with data using all setNext*() methods.
* Verify by running the same buffer through readAny().
*
* @param buf
*/
fun writeAny(buf: MutableBuffer) {
buf.setNextByte(refByte)
buf.setNextUByte(refUByte)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ package org.angelos.io.buf

import kotlin.test.Test

/**
* Testing the ByteBuffer
*
* @constructor Create empty Byte buffer test
*/
class ByteBufferTest: BufferTest() {

/**
* Running tests on the ByteBuffer.
*/
@Test
fun byteBuffer() {
val buf = byteBufferOf(refRead.copyOf())
Expand Down
27 changes: 27 additions & 0 deletions buffer/src/commonTest/kotlin/org/angelos/io/buf/EndiannessTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,54 @@ import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

/**
* Containing common tests for Endianness.
*
* @constructor Create empty Abstract endian test
*/
open class AbstractEndianTest {

/**
* Running a test for isBig().
*
*/
@Test
fun isBig() {
val endianness = Endianness.BIG_ENDIAN
assertTrue(endianness.isBig())
}

/**
* Running a test for isLittle().
*
*/
@Test
fun isLittle() {
val endianness = Endianness.LITTLE_ENDIAN
assertTrue(endianness.isLittle())
}

/**
* Runing a test for toString().
*
*/
@Test
fun testToString() {
val endianness = Endianness.nativeOrder()
assertNotEquals(endianness.toString(), "UNKNOWN_ENDIAN")
}
}

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

/**
* Testing the native order compared to the real running environment.
*/
@Test
fun nativeOrder()
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ package org.angelos.io.buf

import kotlin.test.Test

/**
* Testing the MutableByteBuffer
*
* @constructor Create empty Mutable byte buffer test
*/
class MutableByteBufferTest : BufferTest() {

/**
* Running tests on the MutableByteBuffer.
*/
@Test
fun mutableByteBuffer() {
val buf = mutableByteBufferOf(refWrite.copyOf())
Expand Down
Loading

0 comments on commit 3c2d05e

Please sign in to comment.