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

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

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.encodeBase64Url

Using AI Code Generation

copy

Full Screen

1val base64Url = ByteArray(0).encodeBase64Url()2val base64 = ByteArray(0).encodeBase64()3val byteArray = ByteArray(0).decodeBase64Url()4val byteArray = ByteArray(0).decodeBase64()5val hexString = ByteArray(0).toHex()6val byteArray = String(0).toHex()7val base64 = String(0).toBase64()8val base64Url = String(0).toBase64Url()9val byteArray = String(0).decodeBase64()10val byteArray = String(0).decodeBase64Url()11val byteArray = String(0).decodeBase64()12val byteArray = String(0).decodeBase64Url()13val byteArray = String(0).decodeBase64()14val byteArray = String(0).decodeBase64Url()

Full Screen

Full Screen

ByteArray.encodeBase64Url

Using AI Code Generation

copy

Full Screen

1ByteArray.encodeBase64Url()2ByteArray.decodeBase64Url()3ByteArray.toBase64()4ByteArray.fromBase64()5ByteArray.toBase64Url()6ByteArray.fromBase64Url()7ByteArray.toBase64()8ByteArray.fromBase64()9ByteArray.toBase64Url()10ByteArray.fromBase64Url()11ByteArray.toBase64()12ByteArray.fromBase64()13ByteArray.toBase64Url()14ByteArray.fromBase64Url()15ByteArray.toBase64()16ByteArray.fromBase64()17ByteArray.toBase64Url()

Full Screen

Full Screen

ByteArray.encodeBase64Url

Using AI Code Generation

copy

Full Screen

1import com.github.kittinunf.fuel.util.Base642fun main(args : Array<String>) {3 val byteArray = ByteArray(10)4 val base64String = Base64.encodeBase64Url(byteArray)5 println(base64String)6}7import com.github.kittinunf.fuel.util.Base648fun main(args : Array<String>) {9 val byteArray = ByteArray(10)10 val base64String = Base64.encodeBase64(byteArray)11 println(base64String)12}13import com.github.kittinunf.fuel.util.Base6414fun main(args : Array<String>) {15 val byteArray = ByteArray(10)16 val base64String = Base64.encodeBase64(byteArray)17 println(base64

Full Screen

Full Screen

ByteArray.encodeBase64Url

Using AI Code Generation

copy

Full Screen

1val base64urlEncoded = ByteArray.encodeBase64Url(byteArray)2val base64urlDecoded = ByteArray.decodeBase64Url(base64urlEncoded)3val base64Encoded = ByteArray.encodeBase64(byteArray)4val base64Decoded = ByteArray.decodeBase64(base64Encoded)5val base64Encoded = ByteArray.encodeBase64(byteArray)6val base64Decoded = ByteArray.decodeBase64(base64Encoded)7val base64Encoded = ByteArray.encodeBase64(byteArray)8val base64Decoded = ByteArray.decodeBase64(base64Encoded)9val base64Encoded = ByteArray.encodeBase64(byteArray)10val base64Decoded = ByteArray.decodeBase64(base64Encoded)11val base64Encoded = ByteArray.encodeBase64(byteArray)12val base64Decoded = ByteArray.decodeBase64(base64Encoded)13val base64Encoded = ByteArray.encodeBase64(byteArray)14val base64Decoded = ByteArray.decodeBase64(base64Encoded)

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