How to use Int.toBase64 method of org.spekframework.spek2.runtime.util.Base64 class

Best Spek code snippet using org.spekframework.spek2.runtime.util.Base64.Int.toBase64

Base64.kt

Source:Base64.kt Github

copy

Full Screen

1package org.spekframework.spek2.runtime.util2private val BASE64_ALPHABET: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"3private val BASE64_MASK: Byte = 0x3f4private val BASE64_INVERSE_MASK: Int = 0b1111_11115private val BASE64_PAD: Char = '='6private val BASE64_PAD_BYTE: Byte = BASE64_PAD.toByte()7private val BASE64_INVERSE_ALPHABET = IntArray(256) {8 BASE64_ALPHABET.indexOf(it.toChar())9}10private fun Int.toBase64(): Char = BASE64_ALPHABET[this]11actual object Base64 {12 actual fun encodeToString(text: String): String {13 val encoded = encode(text.toByteArray())14 return buildString(encoded.size) {15 encoded.forEach { append(it.toChar()) }16 }17 }18 actual fun decodeToString(encodedText: String): String {19 val decoded = decode(encodedText.toByteArray())20 return buildString(decoded.size) {21 decoded.forEach { append(it.toChar()) }22 }23 }24 private fun encode(src: ByteArray): ByteArray {25 fun ByteArray.getOrZero(index: Int): Int = if (index >= size) 0 else get(index).toInt()26 // 4n / 3 is expected Base64 payload27 val result = ArrayList<Byte>(4 * src.size / 3)28 var index = 029 while (index < src.size) {30 val symbolsLeft = src.size - index31 val padSize = if (symbolsLeft >= 3) 0 else (3 - symbolsLeft) * 8 / 632 val chunk = (src.getOrZero(index) shl 16) or (src.getOrZero(index + 1) shl 8) or src.getOrZero(index + 2)33 index += 334 for (i in 3 downTo padSize) {35 val char = (chunk shr (6 * i)) and BASE64_MASK.toInt()36 result.add(char.toBase64().toByte())37 }38 // Fill the pad with '='39 repeat(padSize) { result.add(BASE64_PAD_BYTE) }40 }41 return result.toByteArray()42 }43 fun decode(src: ByteArray): ByteArray {44 if (src.size % 4 != 0) {45 throw IllegalArgumentException("Invalid Base64 encoded data.")46 }47 if (src.size == 0) {48 return ByteArray(0)49 }50 val last = src.last()51 val secondLast = src.dropLast(1).last()52 val paddingSize = if (secondLast == BASE64_PAD_BYTE && last == BASE64_PAD_BYTE) {53 254 } else if (last == BASE64_PAD_BYTE) {55 156 } else {57 058 }59 val size = (3 * src.size / 4) - paddingSize60 val result = ArrayList<Byte>(size)61 var index = 062 while (index < src.size) {63 val isLastChunk = index == src.size - 464 val first = decodeByte(src.get(index))65 val second = decodeByte(src.get(index + 1))66 val third = if (isLastChunk && paddingSize == 2) 0 else decodeByte(src.get(index + 2))67 val fourth = if (isLastChunk && paddingSize >= 1) 0 else decodeByte(src.get(index + 3))68 val chunk = (first shl 18) or (second shl 12) or (third shl 6) or fourth69 val paddingChars = if (isLastChunk) paddingSize else 070 for (i in 2 downTo paddingChars) {71 val char = (chunk shr (8 * i)) and BASE64_INVERSE_MASK72 result.add(char.toByte())73 }74 index += 475 }76 return result.toByteArray()77 }78 private fun decodeByte(byte: Byte): Int {79 val inverse = BASE64_INVERSE_ALPHABET[byte.toInt()]80 if (inverse == -1) {81 throw IllegalArgumentException("Invalid Base64 encoded data.")82 }83 return inverse84 }85}86private fun String.toByteArray() = ByteArray(length) {87 get(it).toByte()88}...

Full Screen

Full Screen

Int.toBase64

Using AI Code Generation

copy

Full Screen

1 val base64 = Int.toBase64(123)2 println(base64)3 val int = Int.fromBase64(base64)4 println(int)5}6test {7 useJUnitPlatform {8 }9}

Full Screen

Full Screen

Int.toBase64

Using AI Code Generation

copy

Full Screen

1 private fun encodeData(data: String): String {2 return Base64.encode(data)3 }4 private fun decodeData(data: String): String {5 return Base64.decode(data)6 }7 private fun encodeData(data: ByteArray): String {8 return Base64.encode(data)9 }10 private fun decodeData(data: String): ByteArray {11 return Base64.decode(data)12 }13 private fun encodeData(data: ByteArray): ByteArray {14 return Base64.encode(data)15 }16 private fun decodeData(data: ByteArray): ByteArray {17 return Base64.decode(data)18 }19 private fun encodeData(data: InputStream): String {20 return Base64.encode(data)21 }22 private fun decodeData(data: String): InputStream {23 return Base64.decode(data)24 }25 private fun encodeData(data: InputStream): ByteArray {26 return Base64.encode(data)27 }

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Spek automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful