Make write exceptions more descriptive

This commit is contained in:
Davide Depau 2018-09-13 19:28:01 +02:00
parent 6b437b647d
commit 8e8f39bf33
Signed by: depau
GPG key ID: C7D999B6A55EFE86
2 changed files with 14 additions and 1 deletions

View file

@ -0,0 +1,8 @@
package eu.depau.etchdroid.exceptions
import eu.depau.etchdroid.kotlin_exts.toHRSize
import java.io.IOException
class UsbWriteException(offset: Long, writtenBytes: Long, exc: Exception) : IOException(
"Write failed at block $offset, ${writtenBytes.toHRSize()} written. Error: $exc", exc
)

View file

@ -5,6 +5,7 @@ import android.hardware.usb.UsbDevice
import android.net.Uri import android.net.Uri
import android.util.Log import android.util.Log
import com.github.mjdev.libaums.UsbMassStorageDevice import com.github.mjdev.libaums.UsbMassStorageDevice
import eu.depau.etchdroid.exceptions.UsbWriteException
import eu.depau.etchdroid.kotlin_exts.getFileName import eu.depau.etchdroid.kotlin_exts.getFileName
import eu.depau.etchdroid.kotlin_exts.name import eu.depau.etchdroid.kotlin_exts.name
import java.io.BufferedInputStream import java.io.BufferedInputStream
@ -69,7 +70,11 @@ abstract class UsbApiWriteService(name: String) : UsbWriteService(name) {
byteBuffer.limit(readBlocksBytes) byteBuffer.limit(readBlocksBytes)
// Write the buffer to the device // Write the buffer to the device
blockDev.write(offset, byteBuffer) try {
blockDev.write(offset, byteBuffer)
} catch (e: Exception) {
throw UsbWriteException(offset, writtenBytes, e)
}
offset += (readBlocksBytes) / blockDev.blockSize offset += (readBlocksBytes) / blockDev.blockSize
writtenBytes += readBlocksBytes writtenBytes += readBlocksBytes