How to use ByteArray.getOrZero method of org.spekframework.spek2.runtime.util.Base64 class

Best Spek code snippet using org.spekframework.spek2.runtime.util.Base64.ByteArray.getOrZero

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

ByteArray.getOrZero

Using AI Code Generation

copy

Full Screen

1fun ByteArray.getOrZero(index: Int): Byte = if (index < size) this[index] else 02fun ByteArray.getOrZero(index: Int): Byte = if (index < size) this[index] else 03fun ByteArray.getOrZero(index: Int): Byte = if (index < size) this[index] else 04fun ByteArray.getOrZero(index: Int): Byte = if (index < size) this[index] else 05fun ByteArray.getOrZero(index: Int): Byte = if (index < size) this[index] else 06fun ByteArray.getOrZero(index: Int): Byte = if (index < size) this[index] else 07fun ByteArray.getOrZero(index: Int): Byte = if (index < size) this[index] else 08fun ByteArray.getOrZero(index: Int): Byte = if (index < size) this[index] else 09fun ByteArray.getOrZero(index: Int): Byte = if (index < size) this[index] else 010fun ByteArray.getOrZero(index: Int): Byte = if (index < size) this[index] else 011fun ByteArray.getOrZero(index: Int): Byte = if (index < size) this[index] else 0

Full Screen

Full Screen

ByteArray.getOrZero

Using AI Code Generation

copy

Full Screen

1 val base64 = Base64()2 val byteArray = base64.decode("aGVsbG8gd29ybGQ=")3 expectThat(byteArray.getOrZero(0)).isEqualTo('h'.toByte())4 expectThat(byteArray.getOrZero(1)).isEqualTo('e'.toByte())5 expectThat(byteArray.getOrZero(2)).isEqualTo('l'.toByte())6 expectThat(byteArray.getOrZero(3)).isEqualTo('l'.toByte())7 expectThat(byteArray.getOrZero(4)).isEqualTo('o'.toByte())8 expectThat(byteArray.getOrZero(5)).isEqualTo(' '.toByte())9 expectThat(byteArray.getOrZero(6)).isEqualTo('w'.toByte())10 expectThat(byteArray.getOrZero(7)).isEqualTo('o'.toByte())11 expectThat(byteArray.getOrZero(8)).isEqualTo('r'.toByte())12 expectThat(byteArray.getOrZero(9)).isEqualTo('l'.toByte())13 expectThat(byteArray.getOrZero(10)).isEqualTo('d'.toByte())14 expectThat(byteArray.getOrZero(11)).isEqualTo('!'.toByte())15 expectThat(byteArray.getOrZero(12)).isEqualTo(0.toByte())16 expectThat(byteArray.getOrZero(13)).isEqualTo(0.toByte())17 expectThat(byteArray.getOrZero(14)).isEqualTo(0.toByte())18 expectThat(byteArray.getOrZero(15)).isEqualTo(0.toByte())19 expectThat(byteArray.getOrZero(16)).isEqualTo(0.toByte())20 expectThat(byteArray.getOrZero(17)).isEqualTo(0.toByte())21 expectThat(byteArray.getOrZero(18)).isEqualTo(0.toByte())22 expectThat(byteArray.getOrZero(19)).isEqualTo(0.toByte())23 expectThat(byteArray.getOrZero(20)).isEqualTo(0.toByte())24 expectThat(byteArray.getOrZero(21)).isEqualTo(0.toByte())25 expectThat(byteArray.getOrZero(22)).isEqualTo(0.toByte())26 expectThat(byteArray.getOrZero(23)).isEqualTo(0.toByte())27 expectThat(byteArray.getOrZero(24)).isEqualTo(0.toByte())28 expectThat(byteArray.getOrZero(25)).isEqualTo(0.toByte())29 expectThat(byteArray.getOrZero(26)).isEqualTo(0.toByte())

Full Screen

Full Screen

ByteArray.getOrZero

Using AI Code Generation

copy

Full Screen

1 val charIndex = Base64.getOrZero(char)2 result += charIndex shl 6 * (3 - index)3 }4}5fun decode(str: String): String {6 val bytes = str.toByteArray()7 var char = 0.toChar()

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