some texts fixed
This commit is contained in:
parent
641188e92b
commit
6ae2e9f885
@ -32,7 +32,7 @@ used automatically when serializing integers. It is slightly more sophisticated
|
|||||||
### - do not reveal information about stored data
|
### - do not reveal information about stored data
|
||||||
|
|
||||||
Many extendable formats, like JSON, BSON, BOSS and may others are keeping data in key-value pairs. While it is good in
|
Many extendable formats, like JSON, BSON, BOSS and may others are keeping data in key-value pairs. While it is good in
|
||||||
many aspets, it has a clear disadvantages: it uses more space and it reveals inner data structure to the world. It is
|
many aspets, it has a clear disadvantages: it uses more space, and it reveals inner data structure to the world. It is
|
||||||
possible to unpack such formats with zero information about inner structure.
|
possible to unpack such formats with zero information about inner structure.
|
||||||
|
|
||||||
Bipack does not store field names, so it is not possible to unpack or interpret it without knowledge of the data
|
Bipack does not store field names, so it is not possible to unpack or interpret it without knowledge of the data
|
||||||
@ -100,7 +100,7 @@ This __field annontation__ allows to store __integer fields__ of any size more c
|
|||||||
|
|
||||||
## @FixedSize(size)
|
## @FixedSize(size)
|
||||||
|
|
||||||
Use it with fixed-size collections (like hashes, keys, etc) to not to keep collection size in the packed binary. It saves at least one byte.
|
Use it with fixed-size collections (like hashes, keys, etc.) to not keep collection size in the packed binary. It saves at least one byte.
|
||||||
|
|
||||||
## @Fixed
|
## @Fixed
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ This library contains a `Bipack` binary format serializer, see [net.sergeych.bip
|
|||||||
|
|
||||||
# Package net.sergeych.bipack
|
# Package net.sergeych.bipack
|
||||||
|
|
||||||
Bipack ais a common kotlinx serializer that works pretty much like any other `kotlinx.serialization` format. You just mark your class as `@Serializable` and it does the rest.
|
Bipack is a common kotlinx serializer that works pretty much like any other `kotlinx.serialization` format. You just mark your class as `@Serializable` and it does the rest.
|
||||||
|
|
||||||
- [BipackEncoder] to serializes anything to bipack format.
|
- [BipackEncoder] to serializes anything to bipack format.
|
||||||
- [BipackDecoder] deserializes from bipack back.
|
- [BipackDecoder] deserializes from bipack back.
|
||||||
|
@ -53,7 +53,7 @@ fun bytesToLong(b: ByteArray): Long {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun bytesToInt(b: ByteArray): Int {
|
fun bytesToInt(b: ByteArray): Int {
|
||||||
var result: Int = 0
|
var result = 0
|
||||||
for (i in 0 until 4) {
|
for (i in 0 until 4) {
|
||||||
result = result shl 8
|
result = result shl 8
|
||||||
result = result or (b[i].toInt() and 0xFF)
|
result = result or (b[i].toInt() and 0xFF)
|
||||||
@ -62,7 +62,7 @@ fun bytesToInt(b: ByteArray): Int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun bytesToShort(b: ByteArray): Short {
|
fun bytesToShort(b: ByteArray): Short {
|
||||||
var result: Int = 0
|
var result = 0
|
||||||
for (i in 0 until 2) {
|
for (i in 0 until 2) {
|
||||||
result = result shl 8
|
result = result shl 8
|
||||||
result = result or (b[i].toInt() and 0xFF)
|
result = result or (b[i].toInt() and 0xFF)
|
||||||
@ -85,7 +85,7 @@ private val hexDigits = "0123456789ABCDEF"
|
|||||||
fun Long.encodeToHex(length: Int = 0): String {
|
fun Long.encodeToHex(length: Int = 0): String {
|
||||||
var result = ""
|
var result = ""
|
||||||
var value = this
|
var value = this
|
||||||
var end = if( value >= 0 ) 0L else -1L
|
val end = if( value >= 0 ) 0L else -1L
|
||||||
// if (value < 0) throw IllegalArgumentException("cant convert to hex negative (ambiguous)")
|
// if (value < 0) throw IllegalArgumentException("cant convert to hex negative (ambiguous)")
|
||||||
do {
|
do {
|
||||||
result = hexDigits[(value and 0x0f).toInt()] + result
|
result = hexDigits[(value and 0x0f).toInt()] + result
|
||||||
@ -98,12 +98,16 @@ fun Long.encodeToHex(length: Int = 0): String {
|
|||||||
fun Int.encodeToHex(length: Int = 0) = (toLong() and 0xFFFFffff).encodeToHex(length)
|
fun Int.encodeToHex(length: Int = 0) = (toLong() and 0xFFFFffff).encodeToHex(length)
|
||||||
@Suppress("unused")
|
@Suppress("unused")
|
||||||
fun UInt.encodeToHex(length: Int = 0) = toLong().encodeToHex(length)
|
fun UInt.encodeToHex(length: Int = 0) = toLong().encodeToHex(length)
|
||||||
|
@Suppress("unused")
|
||||||
fun Byte.encodeToHex(length: Int = 0) = (toLong() and 0xFF).encodeToHex(length)
|
fun Byte.encodeToHex(length: Int = 0) = (toLong() and 0xFF).encodeToHex(length)
|
||||||
|
@Suppress("unused")
|
||||||
fun UByte.encodeToHex(length: Int = 0) = toLong().encodeToHex(length)
|
fun UByte.encodeToHex(length: Int = 0) = toLong().encodeToHex(length)
|
||||||
@Suppress("unused")
|
@Suppress("unused")
|
||||||
fun ULong.encodeToHex(length: Int = 0) = toLong().encodeToHex(length)
|
fun ULong.encodeToHex(length: Int = 0) = toLong().encodeToHex(length)
|
||||||
|
|
||||||
fun ByteArray.encodeToHex(separator: String = " "): String = joinToString(separator) { it.toUByte().encodeToHex(2) }
|
fun ByteArray.encodeToHex(separator: String = " "): String = joinToString(separator) { it.toUByte().encodeToHex(2) }
|
||||||
|
|
||||||
|
@Suppress("unused")
|
||||||
fun Collection<Byte>.encodeToHex(separator: String = " "): String = joinToString(separator) { it.toUByte().encodeToHex(2) }
|
fun Collection<Byte>.encodeToHex(separator: String = " "): String = joinToString(separator) { it.toUByte().encodeToHex(2) }
|
||||||
|
|
||||||
fun ByteArray.toDump(wide: Boolean = false): String = toDumpLines(wide).joinToString("\n")
|
fun ByteArray.toDump(wide: Boolean = false): String = toDumpLines(wide).joinToString("\n")
|
||||||
@ -168,6 +172,7 @@ fun ByteArray.toDumpLines(wide: Boolean = false): List<String> {
|
|||||||
return lines
|
return lines
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Suppress("unused")
|
||||||
fun String.decodeHex(): ByteArray {
|
fun String.decodeHex(): ByteArray {
|
||||||
val source = this.trim().uppercase()
|
val source = this.trim().uppercase()
|
||||||
val result = arrayListOf<Byte>()
|
val result = arrayListOf<Byte>()
|
||||||
@ -197,4 +202,5 @@ fun ByteArray.flipSelf() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Suppress("unused")
|
||||||
fun ByteArray.flip(): ByteArray = copyOf().also { it.flipSelf() }
|
fun ByteArray.flip(): ByteArray = copyOf().also { it.flipSelf() }
|
||||||
|
@ -2,10 +2,8 @@
|
|||||||
|
|
||||||
package net.sergeych.bintools
|
package net.sergeych.bintools
|
||||||
|
|
||||||
import net.sergeych.bintools.*
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Smart variable-length long encoding tools, async. It gives byte-size gain from 64 bits numbers
|
* Smart variable-length long encoding tools, async. It gives byte-size gain from 64 bits numbers,
|
||||||
* so it is very useful when encoding big numbers or at least very bui long values. In other cases
|
* so it is very useful when encoding big numbers or at least very bui long values. In other cases
|
||||||
* [Varint] works faster, and extra bits it uses does not play
|
* [Varint] works faster, and extra bits it uses does not play
|
||||||
*
|
*
|
||||||
|
@ -3,8 +3,8 @@ package net.sergeych.bipack
|
|||||||
import kotlinx.serialization.SerialInfo
|
import kotlinx.serialization.SerialInfo
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If this annotation is presented in some @Serializable class defition, its instances
|
* If this annotation is presented in some @Serializable class definition, its instances
|
||||||
* will be serialized with leadinf number of fields. This allows to extend class later
|
* will be serialized with leading number of fields. This allows to extend class later
|
||||||
* providing new parameters __to the end of the class__ and _with default values__.
|
* providing new parameters __to the end of the class__ and _with default values__.
|
||||||
*
|
*
|
||||||
* Whe deserializing such instances from previous version binaries, the new parameters
|
* Whe deserializing such instances from previous version binaries, the new parameters
|
||||||
@ -39,9 +39,9 @@ annotation class Framed
|
|||||||
annotation class CrcProtected
|
annotation class CrcProtected
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allow marking data fields as being serialized as usnsigned (applyable also to signed fields lite Int, Long and
|
* Allow marking data fields as being serialized as unsigned (applicable also to signed fields lite Int, Long and
|
||||||
* Short, if you are shure they will not be negative). As unsigned types are not cully supported by kotlinx.serialization
|
* Short, if you are sure they will not be negative). As unsigned types are not cully supported by `kotlinx.serialization`
|
||||||
* it is the conly way to tell the serialized to use more compact unsigned variable length encoding.
|
* it is the only way to tell the serialized to use more compact unsigned variable length encoding.
|
||||||
*/
|
*/
|
||||||
@SerialInfo
|
@SerialInfo
|
||||||
@Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY)
|
@Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY)
|
||||||
@ -51,7 +51,7 @@ annotation class Unsigned
|
|||||||
* Fixed size collection of a given size. __Use it only with collections!__
|
* Fixed size collection of a given size. __Use it only with collections!__
|
||||||
*
|
*
|
||||||
* Use it with collection of fixed size, to read/write exact number of items (for example, bytes),
|
* Use it with collection of fixed size, to read/write exact number of items (for example, bytes),
|
||||||
* like hash digest, key bits and so on. Does not stores/loades the collection
|
* like hash digest, key bits and so on. Does not store/load the collection
|
||||||
* size what reduces packed size to at least one byte. depending on the actual
|
* size what reduces packed size to at least one byte. depending on the actual
|
||||||
* collection size. As for nowonly collection types (e.g. ByteArray, List<T>m etc) are supported.
|
* collection size. As for nowonly collection types (e.g. ByteArray, List<T>m etc) are supported.
|
||||||
* Note that if the actual collection size differs from [size], [BipackEncoder] will throw
|
* Note that if the actual collection size differs from [size], [BipackEncoder] will throw
|
||||||
|
Loading…
Reference in New Issue
Block a user