question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exception when storing long Strings

See original GitHub issue

Hi there!

First of all, awesome job on the library, it really keeps the encrypting neat and simple. Good work!

Nevertheless, I’m facing the next issue: When a long string is gonna be stored, the library throws an Exception (see below for more info). It seems to happen when the string length is over 245 characters.

String string246Chars = "AciQ7fLfNGx1l5EivLMGIDtK33Q7P2PfXrqjbKJTdPzkdplrCSlf9LZdwwE2tKNgsvW3tLTnAlr9A5mF2CdNdZjFqJBtZDiAKph4SRsmhpJ94ZM7aXRYBb8KARzyYqYnTfbBkIG3UmRSCKEYrTAB34owTNKK1t7VYrsFrUIbbG4p6OI32Yby5ORVXhLmUAy32qwjMVw0UdLur6NXCcx5Sg0kxZvM6z47IKeSdsWitlsVtHIH8TKOGP";

SecurePreferences.setValue("ALIAS", string246Chars);

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
	Caused by: java.lang.reflect.InvocationTargetException
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Caused by: de.adorsys.android.securestoragelibrary.SecureStorageException: Problem during Encryption

As long as I’ve not been able to see anything related to this in the documentation, Is it supposed to work like that?

Furthermore, if any user wants to store a token from backend or something similar, what can he do as an alternative?

Thanks in advice, and greetings,

Rodri

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
gotevcommented, Feb 11, 2019

Came across the same problem and done a workaround.
Until the new release is ready, you can use this:

object SecurePreferencesHelper {
    private const val chunkSize = 240

    private fun getNumberOfChunksKey(key: String) = "${key}_numberOfChunks"

    fun setLongStringValue(key: String, value: String) {
        val chunks = value.chunked(chunkSize)

        SecurePreferences.setValue(getNumberOfChunksKey(key), chunks.size)

        chunks.forEachIndexed { index, chunk ->
            SecurePreferences.setValue("$key$index", chunk)
        }
    }

    fun getLongStringValue(key: String): String? {
        val numberOfChunks = SecurePreferences.getIntValue(getNumberOfChunksKey(key), 0)

        if (numberOfChunks == 0) {
            return null
        }

        return (0 until numberOfChunks)
                .map { index ->
                    val string = SecurePreferences.getStringValue("$key$index", null) ?: run {
                        return null
                    }

                    string
                }.reduce { accumulator, chunk -> accumulator + chunk }
    }

    fun removeLongStringValue(key: String) {
        val numberOfChunks = SecurePreferences.getIntValue(getNumberOfChunksKey(key), 0)

        (0 until numberOfChunks).map { SecurePreferences.removeValue("$key$it") }
        SecurePreferences.removeValue(getNumberOfChunksKey(key))
    }

    fun containsLongStringValue(key: String): Boolean {
        return SecurePreferences.contains(getNumberOfChunksKey(key))
    }
}
2reactions
ben-p-commitscommented, Oct 4, 2018

Was about to abandon the library due to this limit- happy to see a workaround + plans for having this in the next release. Clever!

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Why does storing a long string cause an OOM error but ...
When line 3332 char[] copy = new char[newLength]; is reached inside Arrays.copyOf , the exception is thrown because there is not enough memory ......
Read more >
Fixing “constant string too long” Build Error - Baeldung
Learn to fix the "constant string too long" build error in Java. ... The best way is to store our string in a...
Read more >
Out of Memory Exception for Java while storing large oject in ...
while in process i am trying to store this CLOB object into a String variable. When the size of CLOB, that usually is...
Read more >
SQLiteConnectionProvider - OverflowException when storing ...
SQLiteConnectionProvider - OverflowException when storing a long string that was saved correctly prior to v20.1.
Read more >
THROW (Transact-SQL) - SQL Server - Microsoft Learn
Raises an exception and transfers execution to a CATCH block of a TRY. ... Is a string or variable that describes the exception....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found