Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Very low copy speed #401

Open
vasiledoe opened this issue Oct 12, 2023 · 3 comments
Open

Very low copy speed #401

vasiledoe opened this issue Oct 12, 2023 · 3 comments

Comments

@vasiledoe
Copy link

Problem

I need to copy files from USB to device storage, I do it like so:

@Throws(IOException::class)
fun UsbFile.copyTo(outFile: File) {
    var inStream: InputStream? = null
    try {
        inStream = BufferedInputStream(UsbFileInputStream(this))
        val outStream: OutputStream = FileOutputStream(outFile)
        outStream.use {
            inStream.copyTo(it)
            it.close()
        }
    } catch (e: Exception) {
        e.printStackTrace()
    } finally {
        inStream?.close()
    }
}

this kotlin copyTo() method uses default buffer soze:
public const val DEFAULT_BUFFER_SIZE: Int = 8 * 1024 (8KB)

This operation takes a lot, especially for big video files. If we increase buffer size like DEFAULT_BUFFER_SIZE * 8 (64KB) then speed will be increased too but this doesn't guarantee that file will be copied successfully. This will work only if Allocation unit size is set >= with this buffer size. There is no way to determine this value from the lib API. Actually we have chunkSize defined in me.jahnen.libaums.core.fs.FileSystem but it always retutns 512

This is where we can change USB Allocation unit size
image

Expected behavior

Maybe there is a better/faster way to copy files from USB to internal memory.
Maybe there is a way to determine the right value for USB Allocation unit size so we can use it in order to copy faster.

** @magnusja your feedback is appreciated

@magnusja
Copy link
Owner

Not sure if I understand. You say there should be a faster way to copy but at the same suggest that using larger buffer sizes increases the speed. If the file is corrupt with larger buffer sizes that is a bug in the file system library (maybe?).

The allocation unit size should be the cluster size. So this is also probably a bug if you expect something else with 64 KB.

/**
     * Returns the amount in bytes in one cluster.
     *
     * @return Amount of bytes.
     */
    val bytesPerCluster: Int
        get() = sectorsPerCluster * bytesPerSector

@magnusja
Copy link
Owner

see also #406

@junxiaojun
Copy link

@magnusja
Thank you. I am very happy to see your reply. The problem has been solved. Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants