Skip to content

Commit 70a9a7d

Browse files
committed
feat(encoder): add util createBytesWriter
1 parent b1f8f70 commit 70a9a7d

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/commonMain/kotlin/space/iseki/bencoding/BWriter.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ internal interface BWriter {
77
writeByte('e'.code)
88
}
99

10+
fun getByteArray(): ByteArray = throw UnsupportedOperationException("Not implemented")
11+
1012
fun writeDictBegin() {
1113
writeByte('d'.code)
1214
}

src/commonMain/kotlin/space/iseki/bencoding/Utils.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ internal expect fun string2BytesIso88591(s: String): ByteArray
1212
@OptIn(ExperimentalSerializationApi::class)
1313
internal fun SerialDescriptor.binaryStringAnnotation(childIndex: Int) =
1414
getElementAnnotations(childIndex).firstOrNull { it is BinaryString } as BinaryString?
15+
16+
internal expect fun createBytesWriter(): BWriter
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
package space.iseki.bencoding
22

3+
import java.io.ByteArrayOutputStream
34
import java.nio.charset.StandardCharsets
45

56
internal actual fun bytes2Long(bytes: ByteArray, off: Int, len: Int): Long = String(bytes, off, len).toLong()
67
internal actual fun bytes2StringIso88591(bytes: ByteArray, off: Int, len: Int): String =
78
String(bytes, off, len, StandardCharsets.ISO_8859_1)
8-
99
internal actual fun string2BytesIso88591(s: String): ByteArray = s.toByteArray(StandardCharsets.ISO_8859_1)
10+
11+
internal actual fun createBytesWriter(): BWriter = object : BWriter {
12+
private val bytes = ByteArrayOutputStream()
13+
override fun writeByte(b: Int) {
14+
bytes.write(b)
15+
}
16+
17+
override fun writeBytes(b: ByteArray) {
18+
bytes.writeBytes(b)
19+
}
20+
21+
override fun getByteArray(): ByteArray = bytes.toByteArray()
22+
}

0 commit comments

Comments
 (0)