Created
October 7, 2019 13:38
-
-
Save hafs-r/de0992b0d3033874cf66fd30d23184be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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