implement decode exception

This commit is contained in:
2026-01-18 21:48:05 +01:00
parent a347bd617c
commit 24c667bc9c
4 changed files with 25 additions and 3 deletions

View File

@@ -0,0 +1,5 @@
package com.infendro.encoding.exception
abstract class DecodeException : Exception() {
abstract override val message: String
}

View File

@@ -0,0 +1,12 @@
package com.infendro.encoding.exception
class IllegalByteException(
val byte: Byte,
) : DecodeException() {
override val message: String
get() {
val hex = "0x${byte.toHexString().padStart(2, '0')}"
val char = "'${byte.toInt().toChar()}'"
return "Illegal byte $hex ($char)."
}
}

View File

@@ -1,6 +1,7 @@
package com.infendro.encoding.numeric
import com.infendro.encoding.Encoding
import com.infendro.encoding.exception.IllegalByteException
import com.infendro.encoding.util.BigInt
import com.infendro.encoding.util.log2
import kotlin.math.ceil
@@ -30,8 +31,11 @@ open class NumericEncoding(
var value = BigInt.zero
for (i in 0..<bytes.size) {
val index = alphabet.indexOf(bytes[i])
if (index == -1) throw Exception() //TODO
val byte = bytes[i]
val index = alphabet.indexOf(byte)
if (index == -1) throw IllegalByteException(byte)
value = value.timesAdd(base, index)
}

View File

@@ -1,6 +1,7 @@
package com.infendro.encoding.stream
import com.infendro.encoding.Encoding
import com.infendro.encoding.exception.IllegalByteException
import com.infendro.encoding.util.align
import com.infendro.encoding.util.divCeil
import com.infendro.encoding.util.lcm
@@ -76,7 +77,7 @@ open class StreamEncoding(
if (byte == padding) break
val index = alphabet.indexOf(byte)
if (index == -1) throw Exception() //TODO
if (index == -1) throw IllegalByteException(byte)
buffer = (buffer shl bits) + index.toByte()
bufferBits += bits