Skip to content

Instantly share code, notes, and snippets.

@tdcolvin
Last active February 3, 2023 18:56
Show Gist options
  • Save tdcolvin/9532270acffd5a5f1aa4a06803fae0b4 to your computer and use it in GitHub Desktop.
Save tdcolvin/9532270acffd5a5f1aa4a06803fae0b4 to your computer and use it in GitHub Desktop.
fun readFromEncryptedFile(): String {
//We will read up to the first 32KB from this file. If your file may be larger, then you
//can increase this value, or read it in chunks.
val fileContent = ByteArray(32000)
//The number of bytes actually read from the file.
val numBytesRead: Int
//Open the file for reading, and read all the contents.
//Note how Kotlin's 'use' function correctly closes the resource after we've finished,
//regardless of whether or not an exception was thrown.
encryptedFile.openFileInput().use {
numBytesRead = it.read(fileContent)
}
return String(fileContent, 0, numBytesRead)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment