Skip to content

Instantly share code, notes, and snippets.

@antonis
Created March 21, 2019 11:19
Show Gist options
  • Select an option

  • Save antonis/be0ab00df739e97b8184e6b7cbbcb46c to your computer and use it in GitHub Desktop.

Select an option

Save antonis/be0ab00df739e97b8184e6b7cbbcb46c to your computer and use it in GitHub Desktop.
fun getBytesFromStringFileSize(fileSizeString: String): Long? {
fun parseBytes(string: String, unit: String, scale: Double): Long {
val numberString = string.toUpperCase().replace(unit, "")
val number = numberString.toDouble()
return (number * Math.pow(1024.0, scale)).toLong()
}
try {
if (fileSizeString.endsWith("GB", ignoreCase = true)) {
return parseBytes(fileSizeString, "GB", 3.0)
}
if (fileSizeString.endsWith("MB", ignoreCase = true)) {
return parseBytes(fileSizeString, "MB", 2.0)
}
if (fileSizeString.endsWith("KB", ignoreCase = true)) {
return parseBytes(fileSizeString, "KB", 1.0)
}
return parseBytes(fileSizeString, "B", 0.0)
} catch (e: Exception) { }
return null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment