How to use String.decodeBase64ToArray method of com.github.kittinunf.fuel.util.Base64 class

Best Fuel code snippet using com.github.kittinunf.fuel.util.Base64.String.decodeBase64ToArray

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

String.decodeBase64ToArray

Using AI Code Generation

copy

Full Screen

1val byteArray = Base64.decodeBase64ToArray(base64String)2val decodedString = String(byteArray)3println(decodedString)4val base64String = decodedString.encodeToBase64()5println(base64String)6val byteArray = byteArrayOf(0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21)7val base64String = Base64.encodeToString(byteArray)8println(base64String)9val byteArray = Base64.decode(base64String)10val decodedString = String(byteArray)11println(decodedString)12val base64String = Base64.encodeToString(decodedString)13println(base64String)14val byteArray = byteArrayOf(0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21)

Full Screen

Full Screen

String.decodeBase64ToArray

Using AI Code Generation

copy

Full Screen

1 val decodedByteArray = String.decodeBase64ToArray(base64String)2 println(String(decodedByteArray))3 val base64EncodedString = String.encodeBase64("hello world")4 println(base64EncodedString)5 val base64EncodedString2 = "hello world".encodeBase64()6 println(base64EncodedString2)7 val base64EncodedString3 = "hello world".encodeBase64()8 println(base64EncodedString3)9 val base64EncodedString4 = "hello world".encodeBase64()10 println(base64EncodedString4)11 val base64EncodedString5 = "hello world".encodeBase64()12 println(base64EncodedString5)13 val base64EncodedString6 = "hello world".encodeBase64()14 println(base64EncodedString6)

Full Screen

Full Screen

String.decodeBase64ToArray

Using AI Code Generation

copy

Full Screen

1String imageString = Base64.decodeBase64ToArray(imageString);2Bitmap bitmap = BitmapFactory.decodeByteArray(imageString, 0, imageString.length);3image.setImageBitmap(bitmap);4String imageString = Base64.decodeBase64(imageString);5Bitmap bitmap = BitmapFactory.decodeByteArray(imageString.getBytes(), 0, imageString.length());6image.setImageBitmap(bitmap);7String imageString = Base64.decodeBase64ToArray(imageString);8Bitmap bitmap = BitmapFactory.decodeByteArray(imageString, 0, imageString.length);9image.setImageBitmap(bitmap);

Full Screen

Full Screen

String.decodeBase64ToArray

Using AI Code Generation

copy

Full Screen

1fun String.decodeBase64ToArray(): ByteArray = Base64.decodeBase64(this)2fun String.decodeBase64ToByteArray(): ByteArray = Base64.decodeBase64(this)3fun String.decodeBase64ToString(): String = Base64.decodeBase64(this).toString(Charsets.UTF_8)4fun String.decodeBase64ToByteArray(charset: Charset): ByteArray = Base64.decodeBase64(this).toString(charset).toByteArray()5fun String.decodeBase64ToString(charset: Charset): String = Base64.decodeBase64(this).toString(charset)6fun String.decodeBase64ToByteArray(charset: String): ByteArray = Base64.decodeBase64(this).toString(charset(charset)).toByteArray()7fun String.decodeBase64ToString(charset: String): String = Base64.decodeBase64(this).toString(charset(charset))

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