Search code examples
androidimage-uploadingsmbjcifs

Android upload to network drive(samba share) performance issue


i have a problem when I upload a picture of 100kb to samba share with JCIFS from my tablet, it takes about 10-20 minutes (before I changed my buffer from 1024 to 20971520 it took almost 6 hours) but it does not give any effect anymore to increase it

it is not the connection issue as i had tested it with ES File where it Uploaded my picture immediately

private class MyCopy extends AsyncTask<String, String, String> {

    String z = "";
    String username = "", password = "", servername = "", filestocopy = "";

    @Override
    protected void onPreExecute() {   
            username = edtusername.getText().toString();
            password = edtpassword.getText().toString();
            servername = "smb://" + edtservername.getText().toString();
            filestocopy = editdir.getText().toString();
        }

       protected String doInBackground(String... params) {
   //         String buffer;
  //          buffer = setingPreferences.getString("buffer", "");
            File file = new File(filestocopy);
            String filename = file.getName();

            NtlmPasswordAuthentication auth1 = new NtlmPasswordAuthentication(
                    servername, username, password);

            try {

                SmbFile sfile = new SmbFile(servername + "/" + filename, auth1);
                if (!sfile.exists())
                    sfile.createNewFile();
                sfile.connect();

                InputStream in = new FileInputStream(file);

                SmbFileOutputStream sfos = new SmbFileOutputStream(sfile);

                byte[] buf = new byte[20971520]; //(parseInt(buffer))
                int len;
                while ((len = in.read(buf)) > 0){
                    sfos.write(buf, 0, len);

                }
                in.close();
                sfos.close();

                z = "File copied successfully";
            } catch (Exception ex) {

                z = z + " " + ex.getMessage().toString();
            }

            return z;
        }
    }

Solution

  • The buffer size shouldn't make a noticeable difference, but it definitely shouldn't be 20M. Use something like 4k instead.

    Are you sure it's the actual file transfer that is taking so long? There's no reason 100k should take more than a few seconds at the most. Have you tried putting log statements between each step, including before and after the authentication calls, createNewFile(), and connect() to check if those are the bottleneck?

    Also, I believe you should be copying bytes while the read length is >= 0 instead of strictly > 0, since -1 signals the end of the stream, not 0.