Skip to content

Commit

Permalink
Fix package name
Browse files Browse the repository at this point in the history
  • Loading branch information
CattenLinger committed Jan 12, 2024
1 parent 3e3a628 commit 0194f0c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
31 changes: 15 additions & 16 deletions src/main/kotlin/com/shinonometn/bt/codec/BDecoder.kt
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
package com.shinonometn.bt.codec

import net.catten.pt.hamster.bcodec.InvalidBEncodingException
import java.io.EOFException
import java.io.InputStream
import java.math.BigInteger
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import java.util.*

class BDecoder(val input : InputStream, val charset : Charset = StandardCharsets.UTF_8) {
class BDecoder(val input: InputStream, val charset: Charset = StandardCharsets.UTF_8) {

private var indicator = 0
private fun nextIndicator() : Int {
private fun nextIndicator(): Int {
if (indicator == 0) indicator = input.read()
return indicator
}

fun nextValue() : BValue? {
fun nextValue(): BValue? {
if (nextIndicator() == -1) return null

return when (indicator) {
in ('0'.code .. '9'.code) -> decodeBytes()
in ('0'.code..'9'.code) -> decodeBytes()
'i'.code -> decodeInteger()
'l'.code -> decodeList()
'd'.code -> decodeDictionary()
Expand All @@ -29,15 +28,15 @@ class BDecoder(val input : InputStream, val charset : Charset = StandardCharsets

}

private fun decodeBytes() : BValue.BBytes {
var byte = nextIndicator().takeIf { (it - '0'.code) in (0 .. 9) }
private fun decodeBytes(): BValue.BBytes {
var byte = nextIndicator().takeIf { (it - '0'.code) in (0..9) }
?: throw InvalidBEncodingException("Expected Number but got '${indicator.toChar()}'")

indicator = 0

byte -= '0'.code
var length = 0
while(byte in (0 .. 9)) {
while (byte in (0..9)) {
length = length * 10 + byte
byte = readOrError() - '0'.code
}
Expand All @@ -46,7 +45,7 @@ class BDecoder(val input : InputStream, val charset : Charset = StandardCharsets
return BValue.wrap(readBytes(length))
}

private fun decodeInteger() : BValue.BInteger {
private fun decodeInteger(): BValue.BInteger {
nextIndicator().takeIf { it == 'i'.code }
?: throw InvalidBEncodingException("Expected 'i' for integer but got '${indicator.toChar()}'")
indicator = 0
Expand All @@ -65,9 +64,9 @@ class BDecoder(val input : InputStream, val charset : Charset = StandardCharsets
byte = readOrError().takeIf { it != '0'.code } ?: throw InvalidBEncodingException("Negative zero is illegal")
}

if (byte !in ('0'.code .. '9'.code)) throw InvalidBEncodingException("Digits required but got '${byte.toChar()}'")
if (byte !in ('0'.code..'9'.code)) throw InvalidBEncodingException("Digits required but got '${byte.toChar()}'")

while(byte in ('0'.code .. '9'.code) && counter <= 256) {
while (byte in ('0'.code..'9'.code) && counter <= 256) {
sb.append(byte.toChar())
counter++
byte = readOrError()
Expand All @@ -77,14 +76,14 @@ class BDecoder(val input : InputStream, val charset : Charset = StandardCharsets
return BValue.wrap(sb.toString().toBigInteger())
}

private fun decodeList() : BValue.BList {
private fun decodeList(): BValue.BList {
nextIndicator().takeIf { it == 'l'.code }
?: throw InvalidBEncodingException("Expected 'l' but got '${indicator.toChar()}'")
indicator = 0

val list = LinkedList<BValue>()
var newIndicator = nextIndicator()
while(newIndicator != 'e'.code) {
while (newIndicator != 'e'.code) {
list.add(nextValue() ?: break)
newIndicator = nextIndicator()
}
Expand All @@ -93,14 +92,14 @@ class BDecoder(val input : InputStream, val charset : Charset = StandardCharsets
return BValue.wrap(list)
}

private fun decodeDictionary() : BValue.BDictionary {
private fun decodeDictionary(): BValue.BDictionary {
nextIndicator().takeIf { it == 'd'.code }
?: throw InvalidBEncodingException("Expected 'd' but got '${indicator.toChar()}'")
indicator = 0

val result = mutableMapOf<String, BValue>()
var newIndicator = nextIndicator()
while(newIndicator != 'e'.code) {
while (newIndicator != 'e'.code) {
val key = nextValue() ?: throw InvalidBEncodingException("Unexpected end of dictionary")
if (key !is BValue.BBytes) throw InvalidBEncodingException("BByte expected but got ${key::class.simpleName}")
result[key.asString(charset)] = nextValue() ?: throw InvalidBEncodingException("Unexpected key has no matching value")
Expand All @@ -112,7 +111,7 @@ class BDecoder(val input : InputStream, val charset : Charset = StandardCharsets
}

private fun readOrError() = input.read().takeIf { it > 0 } ?: throw EOFException("Unexpected stream ended.")
private fun readBytes(count : Int) : ByteArray {
private fun readBytes(count: Int): ByteArray {
val buffer = ByteArray(count)
input.read(buffer)
return buffer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package net.catten.pt.hamster.bcodec
package com.shinonometn.bt.codec

class InvalidBEncodingException(message : String) : Exception(message)
3 changes: 1 addition & 2 deletions src/test/kotlin/com/shinonometn/bt/codec/BDecoderTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.shinonometn.bt.codec

import net.catten.pt.hamster.bcodec.InvalidBEncodingException
import org.junit.jupiter.api.Test
import java.io.ByteArrayInputStream
import kotlin.test.assertEquals
Expand Down Expand Up @@ -50,7 +49,7 @@ class BDecoderTest {
// Pass
return
}
fail("Value $number is incorrect by BEP specification but is was parsed correctly")
fail("Value $number is incorrect by BEP specification but was parsed correctly")
}

private fun numberToBEPBytes(value: Long): ByteArray {
Expand Down

0 comments on commit 0194f0c

Please sign in to comment.