Created
March 21, 2019 11:19
-
-
Save antonis/be0ab00df739e97b8184e6b7cbbcb46c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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