Skip to content

Instantly share code, notes, and snippets.

@hafs-r
Created October 7, 2019 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hafs-r/de0992b0d3033874cf66fd30d23184be to your computer and use it in GitHub Desktop.
Save hafs-r/de0992b0d3033874cf66fd30d23184be to your computer and use it in GitHub Desktop.
import android.util.Log
import androidx.test.platform.app.InstrumentationRegistry
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
object ResourcesHelper {
fun loadFileAsString(path: String): String? {
var bufferedReader: BufferedReader? = null
try {
val stringBuilder = StringBuilder()
bufferedReader = BufferedReader(
InputStreamReader(InstrumentationRegistry.getInstrumentation()
.context.assets.open(path)))
var isFirst = true
var line: String?
do {
line = bufferedReader?.readLine()
if (line == null)
break
if (isFirst)
isFirst = false
else
stringBuilder.append('\n')
stringBuilder.append(line)
} while (true)
return stringBuilder.toString()
} catch (e: Exception) {
Log.e("FakeInterceptor", e.message.plus("> Error opening asset $path."))
} finally {
if (bufferedReader != null) {
try {
bufferedReader?.close()
} catch (e: IOException) {
Log.e("FakeInterceptor", e.message.plus("> Error closing $path."))
}
}
}
return null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment