Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

Base 64 Kotlin extensions #406

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions api/current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

}

49 changes: 49 additions & 0 deletions src/androidTest/java/androidx/util/Base64Test.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
64 changes: 64 additions & 0 deletions src/main/java/androidx/util/Base64.kt
Original file line number Diff line number Diff line change
@@ -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)