Skip to content

Commit

Permalink
Merge pull request #8 from XYOracleNetwork/develop
Browse files Browse the repository at this point in the history
big fixes
  • Loading branch information
carterharrison authored Dec 11, 2018
2 parents 3d5ed54 + 2a56ccf commit 7b76e82
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 55 deletions.
11 changes: 8 additions & 3 deletions src/main/java/network/xyo/sdkobjectmodelkotlin/buffer/XyoBuff.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ abstract class XyoBuff {
val buffer = ByteBuffer.allocate(sizeToReadForSize)
buffer.put(item.copyOfRange(offset, offset + sizeToReadForSize))

// println(offset)
// println(item.copyOfRange(offset, offset + sizeToReadForSize).toHexString())

when (sizeToReadForSize) {
1 -> return buffer[0].toInt() and 0xFF
2 -> return buffer.getShort(0).toInt() and 0xFFFF
Expand Down Expand Up @@ -131,9 +134,11 @@ abstract class XyoBuff {
* @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))
val newSchema = schema.toNewSize(XyoNumberEncoder.getSmartSize(value.size))

val buffer = ByteBuffer.allocate(value.size + newSchema.sizeIdentifier + 2)
buffer.put(newSchema.header)
buffer.put(XyoNumberEncoder.createSize(value.size, newSchema.sizeIdentifier))
buffer.put(value)
return buffer.array()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,13 @@ abstract class XyoIterableObject : XyoBuff() {
*/
private var globalSchema : XyoObjectSchema? = null

/**
* The largest offset known currently. This is used so the array is not iterated over many times.
*/
private var biggestOffset : Int = 0

/**
* An array of the current offsets where items lie (a table of contents from index to offset for the item at that
* index.)
*/
private val offsets = ArrayList<Int>()

/**
* Gets an instance of a new iterator to illiterate over the set.
*
* @throws XyoObjectIteratorException If the bytes are malformed.
*/
open val iterator : Iterator<XyoBuff>
get() {
readHeaderIfNeeded()
return XyoObjectIterator(readOwnHeader())
}

Expand All @@ -46,28 +34,17 @@ abstract class XyoIterableObject : XyoBuff() {
*/
open val count : Int
get() {
readHeaderIfNeeded()
if (biggestOffset == sizeBytes + 2) {
return offsets.size
}

val sizeIt = XyoObjectIterator(biggestOffset)
while (sizeIt.hasNext()) {sizeIt.next()}
return offsets.size
val sizeIt = iterator
var i = 0
while (sizeIt.hasNext()) {
i++
sizeIt.next()
}
return i
}


/**
* Reads the current header of the array if the global offset is 0.
*
* @throws XyoObjectIteratorException If the bytes are malformed.
*/
private fun readHeaderIfNeeded () {
if (biggestOffset == 0) {
biggestOffset = readOwnHeader()
}
}

/**
* Reads the current item at an offset.
*
Expand Down Expand Up @@ -97,11 +74,6 @@ abstract class XyoIterableObject : XyoBuff() {
throw XyoObjectIteratorException("Size can not be 0. Value: ${item.toHexString()}")
}

if (biggestOffset <= startingOffset) {
offsets.add(startingOffset)
biggestOffset = startingOffset + sizeOfObject + 2
}

checkIndex(startingOffset + sizeOfObject + 2)

if (schemaOfItem.isIterable) {
Expand Down Expand Up @@ -132,11 +104,6 @@ abstract class XyoIterableObject : XyoBuff() {
throw XyoObjectIteratorException("Size can not be 0. Value: ${item.toHexString()}")
}

if (biggestOffset <= startingOffset) {
offsets.add(startingOffset)
biggestOffset = startingOffset + sizeOfObject
}

val buffer = ByteBuffer.allocate(sizeOfObject + 2)
checkIndex(startingOffset + sizeOfObject)
buffer.put(schemaOfItem.header)
Expand Down Expand Up @@ -169,13 +136,8 @@ abstract class XyoIterableObject : XyoBuff() {
* @throws XyoObjectIteratorException if the bytes are malformed or if the index is out of range.
*/
open operator fun get(index: Int): XyoBuff {
readHeaderIfNeeded()
if (index < offsets.size) {
return readItemAtOffset(offsets[index])
}

val it = XyoObjectIterator(biggestOffset)
var i = offsets.size
val it = iterator
var i = 0

while (it.hasNext()) {
val item = it.next()
Expand All @@ -199,8 +161,7 @@ abstract class XyoIterableObject : XyoBuff() {
* @throws XyoObjectIteratorException if the bytes are malformed.
*/
open operator fun get(type: Byte): Array<XyoBuff> {
readHeaderIfNeeded()
val it = XyoObjectIterator(readOwnHeader())
val it = iterator
val itemsThatFollowTheType = ArrayList<XyoBuff>()

while (it.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ object XyoNumberEncoder {
*/
fun createSize (sizeOfItem : Int, sizeOfSize : Int) : ByteArray {
val basBuffer = ByteBuffer.allocate(sizeOfSize)

when (sizeOfSize) {
1 -> basBuffer.put((sizeOfItem + 1).toByte())
2 -> basBuffer.putShort((sizeOfItem + 2).toShort())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,20 @@ abstract class XyoObjectSchema {

// masking the first two bits to get the result
// 0xC0 == 11000000

if (encodingCatalogue and 0xC0.toByte() == 0x00.toByte()) {
return 1
}

if (encodingCatalogue and 0xC0 .toByte() == 0x40.toByte()) {
if (encodingCatalogue and 0xC0.toByte() == 0x40.toByte()) {
return 2
}

if (encodingCatalogue and 0xC0.toByte() == 0x80.toByte()) {
return 4
}

if (encodingCatalogue and 0xC0.toByte() == 0xC0.toByte()) {
return 8
if (encodingCatalogue and 0xC0.toByte() == 0xC0.toByte()) { return 8
}

throw XyoSchemaException("Invalid Size: ${encodingCatalogue.toString(2)}")
Expand Down

0 comments on commit 7b76e82

Please sign in to comment.