implement decode exception
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
package com.infendro.encoding.exception
|
||||
|
||||
abstract class DecodeException : Exception() {
|
||||
abstract override val message: String
|
||||
}
|
||||
@@ -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)."
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user