How to use ByteArray.encodeBase64ToArray method of com.github.kittinunf.fuel.util.Base64 class

Best Fuel code snippet using com.github.kittinunf.fuel.util.Base64.ByteArray.encodeBase64ToArray

Base64.kt

Source:Base64.kt Github

copy

Full Screen

1package com.github.kittinunf.fuel.util2/**3 * Inspired From https://github.com/square/okio/blob/master/okio/src/main/kotlin/okio/-Base64.kt4 */5import java.lang.System.arraycopy6fun ByteArray.encodeBase64(): ByteArray = encodeBase64ToArray()7fun ByteArray.encodeBase64Url(): ByteArray = encodeBase64ToArray(map = BASE64_URL_SAFE)8fun String.encodeBase64ToString(): String = String(toByteArray().encodeBase64())9fun String.encodeBase64UrlToString(): String = String(toByteArray().encodeBase64Url())10fun String.decodeBase64(): ByteArray? = decodeBase64ToArray()?.let { it }11fun String.decodeBase64ToString(): String? = decodeBase64ToArray()?.let { String(it) }12private val regular = listOf(('A'..'Z'), ('a'..'z'), ('0'..'9'), listOf('+', '/'))13private val urlSafe = listOf(('A'..'Z'), ('a'..'z'), ('0'..'9'), listOf('-', '_'))14private val BASE64 = regular.flatten().map { it.toByte() }.toByteArray()15private val BASE64_URL_SAFE = urlSafe.flatten().map { it.toByte() }.toByteArray()16private fun ByteArray.encodeBase64ToArray(map: ByteArray = BASE64): ByteArray {17 val length = (size + 2) / 3 * 418 val out = ByteArray(length)19 var index = 020 val end = size - size % 321 var i = 022 while (i < end) {23 val b0 = this[i++].toInt()24 val b1 = this[i++].toInt()25 val b2 = this[i++].toInt()26 out[index++] = map[(b0 and 0xff shr 2)]27 out[index++] = map[(b0 and 0x03 shl 4) or (b1 and 0xff shr 4)]28 out[index++] = map[(b1 and 0x0f shl 2) or (b2 and 0xff shr 6)]29 out[index++] = map[(b2 and 0x3f)]30 }31 when (size - end) {32 1 -> {33 val b0 = this[i].toInt()34 out[index++] = map[b0 and 0xff shr 2]35 out[index++] = map[b0 and 0x03 shl 4]36 out[index++] = '='.toByte()37 out[index] = '='.toByte()38 }39 2 -> {40 val b0 = this[i++].toInt()41 val b1 = this[i].toInt()42 out[index++] = map[(b0 and 0xff shr 2)]43 out[index++] = map[(b0 and 0x03 shl 4) or (b1 and 0xff shr 4)]44 out[index++] = map[(b1 and 0x0f shl 2)]45 out[index] = '='.toByte()46 }47 }48 return out49}50private fun String.decodeBase64ToArray(): ByteArray? {51 // Ignore trailing '=' padding and whitespace from the input.52 var limit = length53 while (limit > 0) {54 val c = this[limit - 1]55 if (c != '=' && c != '\n' && c != '\r' && c != ' ' && c != '\t') {56 break57 }58 limit--59 }60 // If the input includes whitespace, this output array will be longer than necessary.61 val out = ByteArray((limit * 6L / 8L).toInt())62 var outCount = 063 var inCount = 064 var word = 065 for (pos in 0 until limit) {66 val c = this[pos]67 val bits: Int68 if (c in 'A'..'Z') {69 // char ASCII value70 // A 65 071 // Z 90 25 (ASCII - 65)72 bits = c.toInt() - 6573 } else if (c in 'a'..'z') {74 // char ASCII value75 // a 97 2676 // z 122 51 (ASCII - 71)77 bits = c.toInt() - 7178 } else if (c in '0'..'9') {79 // char ASCII value80 // 0 48 5281 // 9 57 61 (ASCII + 4)82 bits = c.toInt() + 483 } else if (c == '+' || c == '-') {84 bits = 6285 } else if (c == '/' || c == '_') {86 bits = 6387 } else if (c == '\n' || c == '\r' || c == ' ' || c == '\t') {88 continue89 } else {90 return null91 }92 // Append this char's 6 bits to the word.93 word = word shl 6 or bits94 // For every 4 chars of input, we accumulate 24 bits of output. Emit 3 bytes.95 inCount++96 if (inCount % 4 == 0) {97 out[outCount++] = (word shr 16).toByte()98 out[outCount++] = (word shr 8).toByte()99 out[outCount++] = word.toByte()100 }101 }102 val lastWordChars = inCount % 4103 when (lastWordChars) {104 1 -> {105 // We read 1 char followed by "===". But 6 bits is a truncated byte! Fail.106 return null107 }108 2 -> {109 // We read 2 chars followed by "==". Emit 1 byte with 8 of those 12 bits.110 word = word shl 12111 out[outCount++] = (word shr 16).toByte()112 }113 3 -> {114 // We read 3 chars, followed by "=". Emit 2 bytes for 16 of those 18 bits.115 word = word shl 6116 out[outCount++] = (word shr 16).toByte()117 out[outCount++] = (word shr 8).toByte()118 }119 }120 // If we sized our out array perfectly, we're done.121 if (outCount == out.size) return out122 // Copy the decoded bytes to a new, right-sized array.123 val prefix = ByteArray(outCount)124 arraycopy(out, 0, prefix, 0, outCount)125 return prefix126}...

Full Screen

Full Screen

ByteArray.encodeBase64ToArray

Using AI Code Generation

copy

Full Screen

1val byteArray = ByteArray(10)2val encodedByteArray = Base64.encodeBase64ToArray(byteArray)3val byteArray = ByteArray(10)4val decodedByteArray = Base64.decodeBase64ToArray(byteArray)5val byteArray = ByteArray(10)6val encodedString = Base64.encodeBase64ToString(byteArray)7val byteArray = ByteArray(10)8val decodedString = Base64.decodeBase64ToString(byteArray)9val encodedByteArray = Base64.encodeBase64ToArray(string)10val decodedByteArray = Base64.decodeBase64ToArray(string)11val encodedString = Base64.encodeBase64ToString(string)12val decodedString = Base64.decodeBase64ToString(string)13val byteArray = ByteArray(10)14val encodedByteArray = byteArray.encodeBase64()15val byteArray = ByteArray(10)16val decodedByteArray = byteArray.decodeBase64()17val encodedString = string.encodeBase64()18val decodedString = string.decodeBase64()

Full Screen

Full Screen

ByteArray.encodeBase64ToArray

Using AI Code Generation

copy

Full Screen

1val bytes = ByteArray(100)2val base64String = Base64.encodeBase64ToString(bytes)3val decodedBytes = Base64.decodeBase64(base64String)4val bytes = ByteArray(100)5val base64String = bytes.encodeBase64()6val decodedBytes = base64String.decodeBase64()7val bytes = ByteArray(100)8val base64String = bytes.encodeBase64ToString()9val decodedBytes = base64String.decodeBase64()10val bytes = ByteArray(100)11val base64String = bytes.encodeBase64ToString()12val decodedBytes = base64String.decodeBase64()13val bytes = ByteArray(100)14val base64String = bytes.encodeBase64ToString()15val decodedBytes = base64String.decodeBase64()16val bytes = ByteArray(100)17val base64String = bytes.encodeBase64ToString()18val decodedBytes = base64String.decodeBase64()19val bytes = ByteArray(100)20val base64String = bytes.encodeBase64ToString()21val decodedBytes = base64String.decodeBase64()22val bytes = ByteArray(100)

Full Screen

Full Screen

ByteArray.encodeBase64ToArray

Using AI Code Generation

copy

Full Screen

1val byteArray : ByteArray = ByteArray(10)2val encodedByteArray = byteArray.encodeBase64ToArray()3val encodedString = String(encodedByteArray)4println(encodedString)5val byteArray : ByteArray = ByteArray(10)6val encodedString = byteArray.encodeBase64ToString()7println(encodedString)8val decodedByteArray = encodedString.decodeBase64()9val decodedString = String(decodedByteArray)10println(decodedString)11val encodedByteArray = "dGVzdA==".toByteArray()12val decodedByteArray = encodedByteArray.decodeBase64()13val decodedString = String(decodedByteArray)14println(decodedString)15val decodedString = encodedString.decodeBase64String()16println(decodedString)17val encodedByteArray = "dGVzdA==".toByteArray()18val decodedString = encodedByteArray.decodeBase64String()19println(decodedString)20val decodedByteArray = encodedString.decodeBase64ToArray()21val decodedString = String(decodedByteArray)22println(decodedString)23val encodedByteArray = "dGVzdA==".toByteArray()24val decodedByteArray = encodedByteArray.decodeBase64ToArray()25val decodedString = String(decodedByteArray)26println(decodedString)27val decodedString = encodedString.decodeBase64ToString()28println(decodedString)

Full Screen

Full Screen

ByteArray.encodeBase64ToArray

Using AI Code Generation

copy

Full Screen

1 val imageBase64 = Base64.encodeBase64ToArray(imageBytes)2 val decodedImageBytes = Base64.decodeBase64(imageBase64)3 at com.github.kittinunf.fuel.util.Base64Kt.encodeBase64ToArray(Base64.kt:13)4 at com.github.kittinunf.fuel.util.Base64Kt.encodeBase64ToArray$default(Base64.kt:11)5 at com.github.kittinunf.fuel.util.Base64Kt.encodeBase64(Base64.kt:19)6 at com.github.kittinunf.fuel.util.Base64Kt.encodeBase64$default(Base64.kt:17)7 at com.github.kittinunf.fuel.util.Base64Kt.encodeBase64(Base64.kt:23)8 at com.github.kittinunf.fuel.util.Base64Kt.encodeBase64$default(Base64.kt:21)9 at com.github.kittinunf.fuel.util.Base64Kt.encodeBase64(Base64.kt:27)10 at com.github.kittinunf.fuel.util.Base64Kt.encodeBase64$default(Base64.kt:25)11 at com.github.kittinunf.fuel.util.Base64Kt.encodeBase64(Base64.kt:31)12 at com.github.kittinunf.fuel.util.Base64Kt.encodeBase64$default(Base64.kt:29)13 at com.github.kittinunf.fuel.util.Base64Kt.encodeBase64(Base64.kt:35)14 at com.github.kittinunf.fuel.util.Base64Kt.encodeBase64$default(Base64.kt:33)15 at com.github.kittinunf.fuel.util.Base64Kt.encodeBase64(Base64.kt:

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful