mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-12-04 15:49:33 +01:00
Revert "fix: Base64 encoder as external library"
This reverts commit 13f2003fed.
This commit is contained in:
|
Before Width: | Height: | Size: 2.7 MiB After Width: | Height: | Size: 2.7 MiB |
1
database/src/androidTest/java/com/kunzisoft/keepass/tests/.gitignore
vendored
Normal file
1
database/src/androidTest/java/com/kunzisoft/keepass/tests/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
R.java
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.kunzisoft.keepass.tests.stream
|
||||
|
||||
import android.content.Context
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import com.kunzisoft.keepass.database.element.binary.BinaryCache
|
||||
import com.kunzisoft.keepass.database.element.binary.BinaryFile
|
||||
import com.kunzisoft.keepass.utils.readAllBytes
|
||||
@@ -12,22 +14,19 @@ import kotlin.random.Random
|
||||
|
||||
class BinaryDataTest {
|
||||
|
||||
private val fileA = File.createTempFile(TEST_FILE_CACHE_A, null)
|
||||
private val fileB = File.createTempFile(TEST_FILE_CACHE_B, null)
|
||||
private val fileC = File.createTempFile(TEST_FILE_CACHE_C, null)
|
||||
private val context: Context by lazy {
|
||||
InstrumentationRegistry.getInstrumentation().context
|
||||
}
|
||||
|
||||
private val cacheDirectory = context.filesDir
|
||||
private val fileA = File(cacheDirectory, TEST_FILE_CACHE_A)
|
||||
private val fileB = File(cacheDirectory, TEST_FILE_CACHE_B)
|
||||
private val fileC = File(cacheDirectory, TEST_FILE_CACHE_C)
|
||||
|
||||
private val binaryCache = BinaryCache()
|
||||
|
||||
private fun buildFile(stringFile: String): File {
|
||||
return File(javaClass.getResource("/$stringFile")!!.path)
|
||||
}
|
||||
|
||||
private fun buildFileStream(stringFile: String): InputStream {
|
||||
return javaClass.getResourceAsStream("/$stringFile")!!
|
||||
}
|
||||
|
||||
private fun saveBinary(asset: String, binaryData: BinaryFile) {
|
||||
buildFileStream(asset).use { assetInputStream ->
|
||||
context.assets.open(asset).use { assetInputStream ->
|
||||
binaryData.getOutputDataStream(binaryCache).use { binaryOutputStream ->
|
||||
assetInputStream.readAllBytes(DEFAULT_BUFFER_SIZE) { buffer ->
|
||||
binaryOutputStream.write(buffer)
|
||||
@@ -130,7 +129,7 @@ class BinaryDataTest {
|
||||
fun testReadText() {
|
||||
val binaryA = BinaryFile(fileA)
|
||||
saveBinary(TEST_TEXT_ASSET, binaryA)
|
||||
assert(streamAreEquals(buildFileStream(TEST_TEXT_ASSET),
|
||||
assert(streamAreEquals(context.assets.open(TEST_TEXT_ASSET),
|
||||
binaryA.getInputDataStream(binaryCache)))
|
||||
}
|
||||
|
||||
@@ -138,7 +137,7 @@ class BinaryDataTest {
|
||||
fun testReadImage() {
|
||||
val binaryA = BinaryFile(fileA)
|
||||
saveBinary(TEST_IMAGE_ASSET, binaryA)
|
||||
assert(streamAreEquals(buildFileStream(TEST_IMAGE_ASSET),
|
||||
assert(streamAreEquals(context.assets.open(TEST_IMAGE_ASSET),
|
||||
binaryA.getInputDataStream(binaryCache)))
|
||||
}
|
||||
|
||||
@@ -21,10 +21,11 @@ package com.kunzisoft.keepass.database.element.binary
|
||||
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
import com.kunzisoft.keepass.database.element.binary.BinaryCache.Companion.UNKNOWN
|
||||
import android.util.Base64
|
||||
import android.util.Base64InputStream
|
||||
import android.util.Base64OutputStream
|
||||
import com.kunzisoft.keepass.utils.readAllBytes
|
||||
import org.apache.commons.codec.binary.Base64InputStream
|
||||
import org.apache.commons.codec.binary.Base64OutputStream
|
||||
import com.kunzisoft.keepass.database.element.binary.BinaryCache.Companion.UNKNOWN
|
||||
import java.io.*
|
||||
import java.util.zip.GZIPOutputStream
|
||||
|
||||
@@ -59,12 +60,12 @@ class BinaryByte : BinaryData {
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun getInputDataStream(binaryCache: BinaryCache): InputStream {
|
||||
return Base64InputStream(ByteArrayInputStream(getByteArray(binaryCache)), false)
|
||||
return Base64InputStream(ByteArrayInputStream(getByteArray(binaryCache)), Base64.NO_WRAP)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun getOutputDataStream(binaryCache: BinaryCache): OutputStream {
|
||||
return BinaryCountingOutputStream(Base64OutputStream(ByteOutputStream(binaryCache), true))
|
||||
return BinaryCountingOutputStream(Base64OutputStream(ByteOutputStream(binaryCache), Base64.NO_WRAP))
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
*/
|
||||
package com.kunzisoft.keepass.database.element.binary
|
||||
|
||||
import android.app.ActivityManager
|
||||
import android.content.Context
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
import org.apache.commons.io.output.CountingOutputStream
|
||||
@@ -178,6 +180,17 @@ abstract class BinaryData : Parcelable {
|
||||
|
||||
companion object {
|
||||
private val TAG = BinaryData::class.java.name
|
||||
private const val MAX_BINARY_BYTE = 10485760 // 10 MB
|
||||
|
||||
fun canMemoryBeAllocatedInRAM(context: Context, memoryWanted: Long): Boolean {
|
||||
if (memoryWanted > MAX_BINARY_BYTE)
|
||||
return false
|
||||
val memoryInfo = ActivityManager.MemoryInfo()
|
||||
(context.getSystemService(Context.ACTIVITY_SERVICE)
|
||||
as? ActivityManager?)?.getMemoryInfo(memoryInfo)
|
||||
val availableMemory = memoryInfo.availMem
|
||||
return availableMemory > (memoryWanted * 5)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,9 +21,10 @@ package com.kunzisoft.keepass.database.element.binary
|
||||
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
import android.util.Base64
|
||||
import android.util.Base64InputStream
|
||||
import android.util.Base64OutputStream
|
||||
import com.kunzisoft.keepass.utils.readAllBytes
|
||||
import org.apache.commons.codec.binary.Base64InputStream
|
||||
import org.apache.commons.codec.binary.Base64OutputStream
|
||||
import java.io.*
|
||||
import java.util.zip.GZIPOutputStream
|
||||
import javax.crypto.Cipher
|
||||
@@ -74,7 +75,7 @@ class BinaryFile : BinaryData {
|
||||
return when {
|
||||
file != null && file.length() > 0 -> {
|
||||
cipherDecryption.init(Cipher.DECRYPT_MODE, cipherKey.key, IvParameterSpec(cipherKey.iv))
|
||||
Base64InputStream(CipherInputStream(FileInputStream(file), cipherDecryption), false)
|
||||
Base64InputStream(CipherInputStream(FileInputStream(file), cipherDecryption), Base64.NO_WRAP)
|
||||
}
|
||||
else -> ByteArrayInputStream(ByteArray(0))
|
||||
}
|
||||
@@ -86,7 +87,7 @@ class BinaryFile : BinaryData {
|
||||
return when {
|
||||
file != null -> {
|
||||
cipherEncryption.init(Cipher.ENCRYPT_MODE, cipherKey.key, IvParameterSpec(cipherKey.iv))
|
||||
BinaryCountingOutputStream(Base64OutputStream(CipherOutputStream(FileOutputStream(file), cipherEncryption), true))
|
||||
BinaryCountingOutputStream(Base64OutputStream(CipherOutputStream(FileOutputStream(file), cipherEncryption), Base64.NO_WRAP))
|
||||
}
|
||||
else -> throw IOException("Unable to write in an unknown file")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user