-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from XYOracleNetwork/develop
Updates for XyoBuff
- Loading branch information
Showing
16 changed files
with
784 additions
and
502 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
src/main/java/network/xyo/sdkobjectmodelkotlin/XyoCache.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package network.xyo.sdkobjectmodelkotlin | ||
|
||
import java.lang.ref.WeakReference | ||
|
||
/** | ||
* A simple caching file that used weak references to cache any item. | ||
* | ||
* @type T Is the type of the object wishing to cache, this also but me the type of the XyoCacheAble interface. | ||
* @property itemToCache The XyoCacheAble interface to get a FRESH item, to cache. | ||
*/ | ||
class XyoCache<T> (private val itemToCache : XyoCacheAble<T>) { | ||
private var reference : WeakReference<T>? = null | ||
|
||
/** | ||
* The cache-able interface to cache any item. | ||
* | ||
* @type iT The type that the get get function returns. (The type of the item to cache) | ||
*/ | ||
interface XyoCacheAble<iT> { | ||
|
||
/** | ||
* Gets a clean instance of the object. | ||
* | ||
* @return iT The clean (not cached) item. | ||
*/ | ||
fun get() : iT | ||
} | ||
|
||
/** | ||
* Gets the item from the cache, if not in the cache it item will be collected freshly and cached. | ||
* | ||
* @return The cached or clean item of type T. | ||
*/ | ||
fun get () : T { | ||
val cachedItem = reference?.get() | ||
|
||
if (cachedItem != null) { | ||
return cachedItem | ||
} | ||
|
||
val newItem = itemToCache.get() | ||
reference = WeakReference(newItem) | ||
return newItem | ||
} | ||
|
||
/** | ||
* Clears the cache, the next time the this.get() function is called, the cache will be updated. | ||
*/ | ||
fun clear () { | ||
reference = null | ||
} | ||
} |
130 changes: 130 additions & 0 deletions
130
src/main/java/network/xyo/sdkobjectmodelkotlin/buffer/XyoBuff.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package network.xyo.sdkobjectmodelkotlin.buffer | ||
|
||
import network.xyo.sdkobjectmodelkotlin.exceptions.XyoObjectException | ||
import network.xyo.sdkobjectmodelkotlin.objects.XyoNumberEncoder | ||
import network.xyo.sdkobjectmodelkotlin.objects.toHexString | ||
import network.xyo.sdkobjectmodelkotlin.schema.XyoObjectSchema | ||
import java.nio.* | ||
|
||
/** | ||
* A base class for <i>XyoObjects</i>. This is used for obtaining the schema, value, and size of the item. | ||
*/ | ||
abstract class XyoBuff { | ||
/** | ||
* The primary data input for the XyoBuff. This buffer will not be read before the allowedOffset. | ||
*/ | ||
protected abstract var item : ByteArray | ||
|
||
/** | ||
* The sizes of the headers to read. This should align with XyoObjectSchema. This value should be set to 0 when | ||
* dealing with typed elements in a typed array. | ||
*/ | ||
protected open val headerSize : Int = 2 | ||
|
||
/** | ||
* The starting offset of where to read. This buffer will not be read past this buffer. | ||
*/ | ||
abstract val allowedOffset : Int | ||
|
||
/** | ||
* The XyoObjectSchema of the XyoBuff | ||
*/ | ||
open val schema : XyoObjectSchema | ||
get() { | ||
return XyoObjectSchema.createFromHeader(item.copyOfRange(allowedOffset, allowedOffset + headerSize)) | ||
} | ||
|
||
/** | ||
* The size of the object, in bytes. | ||
* | ||
* NOTE: This does not include the first two header bytes. | ||
*/ | ||
open val sizeBytes : Int | ||
get() { | ||
return readSizeOfObject(schema.sizeIdentifier, allowedOffset + headerSize) | ||
} | ||
|
||
/** | ||
* The value of the object. The value of the object is the object without the size, or the 2 byte header. | ||
*/ | ||
open val valueCopy : ByteArray | ||
get() { | ||
return item.copyOfRange( | ||
headerSize + schema.sizeIdentifier + allowedOffset, | ||
headerSize + allowedOffset + sizeBytes | ||
) | ||
} | ||
|
||
/** | ||
* All of the bytes for the object including the header and size. | ||
*/ | ||
open val bytesCopy : ByteArray | ||
get() { | ||
return item.copyOfRange(allowedOffset, allowedOffset + sizeBytes + headerSize) | ||
} | ||
|
||
|
||
/** | ||
* Reads the size of the object at a current index. | ||
* | ||
* @param sizeToReadForSize The number of bytes to read for the size. | ||
* @param offset The offset at which to read the size. | ||
* @throws XyoObjectException Ig the sizeToReadForSize is not [1, 2, 4] | ||
*/ | ||
protected fun readSizeOfObject (sizeToReadForSize : Int, offset: Int) : Int { | ||
val buffer = ByteBuffer.allocate(sizeToReadForSize) | ||
buffer.put(item.copyOfRange(offset, offset + sizeToReadForSize)) | ||
|
||
when (sizeToReadForSize) { | ||
1 -> return buffer[0].toInt() and 0xFF | ||
2 -> return buffer.getShort(0).toInt() and 0xFFFF | ||
4 -> return buffer.getInt(0) | ||
} | ||
|
||
throw XyoObjectException("Stub for long count. Value: ${item.toHexString()}") | ||
} | ||
|
||
companion object { | ||
/** | ||
* Creates a XyoBuff with a schema and a value. | ||
* | ||
* @param schema The schema to create the object with. | ||
* @param value The value of the object to encode. This does NOT include size. | ||
*/ | ||
fun newInstance (schema : XyoObjectSchema, value : ByteArray) : XyoBuff { | ||
return object : XyoBuff() { | ||
override val schema: XyoObjectSchema = schema | ||
override var item: ByteArray = getObjectEncoded(schema, value) | ||
override val valueCopy: ByteArray = value | ||
override val allowedOffset: Int = 0 | ||
} | ||
} | ||
|
||
/** | ||
* Wraps a given XyoBuff in byte form and creates a XyoBuff. | ||
* | ||
* @param buff The encoded XyoBuffer, this value can be obtained from myBuff.bytesCopy | ||
* @return The represented XyoBuff. | ||
*/ | ||
fun wrap (buff : ByteArray) : XyoBuff { | ||
return object : XyoBuff() { | ||
override val allowedOffset: Int = 0 | ||
override var item: ByteArray = buff | ||
} | ||
} | ||
|
||
/** | ||
* Encodes a XyoBuff given a value and schema. | ||
* | ||
* @param schema The schema of the XyoBuff to create. | ||
* @param value The value of the XyoBuff to create. This does NOT include size. | ||
*/ | ||
fun getObjectEncoded (schema: XyoObjectSchema, value: ByteArray) : ByteArray { | ||
val buffer = ByteBuffer.allocate(value.size + schema.sizeIdentifier + 2) | ||
buffer.put(schema.header) | ||
buffer.put(XyoNumberEncoder.createSize(value.size, schema.sizeIdentifier)) | ||
buffer.put(value) | ||
return buffer.array() | ||
} | ||
} | ||
} |
8 changes: 0 additions & 8 deletions
8
src/main/java/network/xyo/sdkobjectmodelkotlin/exceptions/XyoObjectExceotion.kt
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
src/main/java/network/xyo/sdkobjectmodelkotlin/exceptions/XyoObjectException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package network.xyo.sdkobjectmodelkotlin.exceptions | ||
|
||
import java.lang.Exception | ||
|
||
/** | ||
* A base exception for all all XyoObject related items (all internal functions). | ||
* | ||
* @property message The message of the exception. | ||
*/ | ||
open class XyoObjectException (override val message: String?) : Exception() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/network/xyo/sdkobjectmodelkotlin/objects/XyoByteArrayToHexString.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package network.xyo.sdkobjectmodelkotlin.objects | ||
|
||
import java.lang.StringBuilder | ||
|
||
/** | ||
* A function to encode ByteArrays into strings. | ||
* | ||
* [0x13, 0x37] -> 0x1337 | ||
* | ||
* @return A string in base 16 (hex) of the ByteArray. All uppercase. | ||
*/ | ||
fun ByteArray.toHexString(): String { | ||
val builder = StringBuilder() | ||
val it = this.iterator() | ||
builder.append("0x") | ||
while (it.hasNext()) { | ||
builder.append(String.format("%02X", it.next())) | ||
} | ||
|
||
return builder.toString() | ||
} |
Oops, something went wrong.