Avoid writing zeroes because of Java ByteBuffer's weirdness

This commit is contained in:
Davide Depau 2018-08-14 14:05:30 +02:00
parent 40f01c134f
commit 0584d2545a
Signed by: depau
GPG key ID: C7D999B6A55EFE86

View file

@ -10,6 +10,8 @@ import eu.depau.ddroid.utils.name
import java.nio.ByteBuffer import java.nio.ByteBuffer
class UsbAPIWriteService : UsbWriteService("UsbAPIWriteService") { class UsbAPIWriteService : UsbWriteService("UsbAPIWriteService") {
val DD_BLOCKSIZE = 4096
class Action { class Action {
val WRITE_IMAGE = "eu.depau.ddroid.action.API_WRITE_IMAGE" val WRITE_IMAGE = "eu.depau.ddroid.action.API_WRITE_IMAGE"
val WRITE_CANCEL = "eu.depau.ddroid.action.API_WRITE_CANCEL" val WRITE_CANCEL = "eu.depau.ddroid.action.API_WRITE_CANCEL"
@ -45,7 +47,8 @@ class UsbAPIWriteService : UsbWriteService("UsbAPIWriteService") {
msDev.init() msDev.init()
val blockDev = msDev.blockDevice val blockDev = msDev.blockDevice
val byteBuffer = ByteBuffer.allocate(blockDev.blockSize) var bsFactor = DD_BLOCKSIZE / blockDev.blockSize
val byteBuffer = ByteBuffer.allocate(blockDev.blockSize * bsFactor)
val imageSize = uri.getFileSize(this) val imageSize = uri.getFileSize(this)
val inputStream = contentResolver.openInputStream(uri)!! val inputStream = contentResolver.openInputStream(uri)!!
@ -56,12 +59,12 @@ class UsbAPIWriteService : UsbWriteService("UsbAPIWriteService") {
readBytes = inputStream.read(byteBuffer.array()!!) readBytes = inputStream.read(byteBuffer.array()!!)
if (readBytes < 0) if (readBytes < 0)
break break
byteBuffer.position(0)
byteBuffer.position(readBytes)
blockDev.write(offset, byteBuffer) blockDev.write(offset, byteBuffer)
offset++ offset++
notify(offset * blockDev.blockSize, imageSize) notify(offset * blockDev.blockSize * bsFactor, imageSize)
} }
msDev.close() msDev.close()