diff --git a/api/current.txt b/api/current.txt index 4c56ac98..6f7f7ad8 100644 --- a/api/current.txt +++ b/api/current.txt @@ -656,3 +656,15 @@ package androidx.core.widget { } +package androidx.util { + + public final class Base64Kt { + ctor public Base64Kt(); + method public static String decodeBase64(kotlin.jvm.internal.StringCompanionObject, String input, int offset = "0", int len = "input.length", int flags = "Base64.DEFAULT", java.nio.charset.Charset byteArrayCharset = "Charsets.US_ASCII", java.nio.charset.Charset stringCharset = "Charsets.UTF_8"); + method public static byte[] decodeBase64(byte[], int offset = "0", int len = "this.size", int flags = "Base64.DEFAULT"); + method public static String encodeBase64(String, int offset = "0", int len = "this.length", int flags = "Base64.DEFAULT", java.nio.charset.Charset byteArrayCharset = "Charsets.US_ASCII", java.nio.charset.Charset stringCharset = "Charsets.UTF_8"); + method public static byte[] encodeBase64(byte[], int offset = "0", int len = "this.size", int flags = "Base64.DEFAULT"); + } + +} + diff --git a/src/androidTest/java/androidx/util/Base64Test.kt b/src/androidTest/java/androidx/util/Base64Test.kt new file mode 100644 index 00000000..7323ddfc --- /dev/null +++ b/src/androidTest/java/androidx/util/Base64Test.kt @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package androidx.util + +import android.util.Base64 +import org.junit.Assert.assertEquals +import org.junit.Test + +class Base64Test { + @Test fun toBase64String() { + val expected = "SGksIEphcmVk" + val actual = "Hi, Jared".encodeBase64(flags = Base64.NO_WRAP) + assertEquals(expected, actual) + } + + @Test fun toBase64ByteArray() { + val expected = "AQIDBA==" + val actual = byteArrayOf(1, 2, 3, 4).encodeBase64(flags = Base64.NO_WRAP) + .toString(Charsets.UTF_8) + assertEquals(expected, actual) + } + + @Test fun fromBase64String() { + val expected = "Hi, Jared" + val actual = String.decodeBase64("SGksIEphcmVk", flags = Base64.NO_WRAP) + assertEquals(expected, actual) + } + + @Test fun fromBase64ByteArray() { + val expected = byteArrayOf(1, 2, 3, 4).toString(Charsets.UTF_8) + val actual = "AQIDBA==".toByteArray().decodeBase64(flags = Base64.NO_WRAP) + .toString(Charsets.UTF_8) + assertEquals(expected, actual) + } +} diff --git a/src/main/java/androidx/util/Base64.kt b/src/main/java/androidx/util/Base64.kt new file mode 100644 index 00000000..02fa8c54 --- /dev/null +++ b/src/main/java/androidx/util/Base64.kt @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@file:Suppress("NOTHING_TO_INLINE") + +package androidx.util + +import android.util.Base64 +import java.nio.charset.Charset + +/** + * Convert [String] to base-64 encoded [String]. + */ +inline fun String.encodeBase64( + offset: Int = 0, + len: Int = this.length, + flags: Int = Base64.DEFAULT, + byteArrayCharset: Charset = Charsets.US_ASCII, + stringCharset: Charset = Charsets.UTF_8 +): String = Base64.encode(toByteArray(byteArrayCharset), offset, len, flags).toString(stringCharset) + +/** + * Convert [ByteArray] to base-64 encoded [ByteArray]. + */ +inline fun ByteArray.encodeBase64( + offset: Int = 0, + len: Int = this.size, + flags: Int = Base64.DEFAULT +): ByteArray = Base64.encode(this, offset, len, flags) + +/** + * Convert base-64 encoded [String] to decoded [String]. + */ +inline fun String.Companion.decodeBase64( + input: String, + offset: Int = 0, + len: Int = input.length, + flags: Int = Base64.DEFAULT, + byteArrayCharset: Charset = Charsets.US_ASCII, + stringCharset: Charset = Charsets.UTF_8 +): String = + Base64.decode(input.toByteArray(byteArrayCharset), offset, len, flags).toString(stringCharset) + +/** + * Convert base-64 encoded [ByteArray] to decoded [ByteArray]. + */ +inline fun ByteArray.decodeBase64( + offset: Int = 0, + len: Int = this.size, + flags: Int = Base64.DEFAULT +): ByteArray = Base64.decode(this, offset, len, flags)