Change FileReadWrite to more idiomatic code
This commit is contained in:
committed by
Ilya Gorbunov
parent
f4015b48f7
commit
7fe7b6d6e1
@@ -50,7 +50,7 @@ public inline fun File.printWriter(charset: Charset = Charsets.UTF_8): PrintWrit
|
|||||||
*
|
*
|
||||||
* @return the entire content of this file as a byte array.
|
* @return the entire content of this file as a byte array.
|
||||||
*/
|
*/
|
||||||
public fun File.readBytes(): ByteArray = FileInputStream(this).use { input ->
|
public fun File.readBytes(): ByteArray = inputStream().use { input ->
|
||||||
var offset = 0
|
var offset = 0
|
||||||
var remaining = this.length().let {
|
var remaining = this.length().let {
|
||||||
if (it > Int.MAX_VALUE) throw OutOfMemoryError("File $this is too big ($it bytes) to fit in memory.") else it
|
if (it > Int.MAX_VALUE) throw OutOfMemoryError("File $this is too big ($it bytes) to fit in memory.") else it
|
||||||
@@ -129,19 +129,16 @@ public fun File.forEachBlock(action: (buffer: ByteArray, bytesRead: Int) -> Unit
|
|||||||
*/
|
*/
|
||||||
public fun File.forEachBlock(blockSize: Int, action: (buffer: ByteArray, bytesRead: Int) -> Unit): Unit {
|
public fun File.forEachBlock(blockSize: Int, action: (buffer: ByteArray, bytesRead: Int) -> Unit): Unit {
|
||||||
val arr = ByteArray(blockSize.coerceAtLeast(MINIMUM_BLOCK_SIZE))
|
val arr = ByteArray(blockSize.coerceAtLeast(MINIMUM_BLOCK_SIZE))
|
||||||
val fis = FileInputStream(this)
|
|
||||||
|
|
||||||
try {
|
inputStream().use { input ->
|
||||||
do {
|
do {
|
||||||
val size = fis.read(arr)
|
val size = input.read(arr)
|
||||||
if (size <= 0) {
|
if (size <= 0) {
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
action(arr, size)
|
action(arr, size)
|
||||||
}
|
}
|
||||||
} while (true)
|
} while (true)
|
||||||
} finally {
|
|
||||||
fis.close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user