Remove partial download of different versions of the file

Julien LepillerSun May 31 17:04:20+0200 2020

9580110

Remove partial download of different versions of the file

CHANGELOG.md

1616
1717
* When a file is already downloaded, updating it will append to it instead
1818
  of overriding it.
19+
* When a file is partially downloaded but updated on server, remove the old
20+
  partial file as continuing download would cause a hash mismatch.
1921
2022
Changes In 0.2.1
2123
-----------------

app/src/main/java/eu/lepiller/nani/DictionaryDownloadActivity.java

195195
                        input = connection.getInputStream();
196196
                        file = new File(e.getValue() + ".sha256");
197197
                        file.getParentFile().mkdirs();
198+
                        File old_file = new File(file + ".old");
199+
                        if(old_file.exists()) {
200+
                            old_file.delete();
201+
                        }
202+
                        if(file.exists()) {
203+
                            file.renameTo(old_file);
204+
                        }
198205
                        output = new FileOutputStream(file);
199206
                        while((count = input.read(data)) != -1) {
200207
                            if (isCancelled()) {

203210
                            }
204211
                            output.write(data, 0, count);
205212
                        }
213+
214+
                        if(old_file.exists()) {
215+
                            // Check that we continue to download the same file
216+
                            String old_hash = Dictionary.readSha256FromFile(old_file);
217+
                            String new_hash = Dictionary.readSha256FromFile(file);
218+
219+
                            if(old_hash.compareTo(new_hash) != 0) {
220+
                                // Remove file that is now different
221+
                                d.remove();
222+
                            }
223+
224+
                            old_file.delete();
225+
                        }
206226
                    }
207227
208228
                    // .sha256 file now downloaded

app/src/main/java/eu/lepiller/nani/dictionary/Dictionary.java

33
import android.content.Context;
44
import android.graphics.drawable.Drawable;
55
import android.os.Build;
6+
import android.util.Log;
7+
68
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;
79
810
import java.io.File;
11+
import java.io.FileNotFoundException;
12+
import java.io.FileReader;
13+
import java.io.IOException;
914
import java.net.URL;
1015
import java.util.Map;
1116

7580
    abstract int getDrawableId();
7681
7782
    abstract public void remove();
83+
84+
    public static String readSha256FromFile(File file) throws IOException {
85+
        StringBuilder sb = new StringBuilder();
86+
        char data[] = new char[4096];
87+
        FileReader fr = new FileReader(file);
88+
        int count;
89+
        while((count = fr.read(data)) != -1) {
90+
            sb.append(data, 0, count);
91+
        }
92+
93+
        return sb.toString().trim();
94+
    }
7895
}

app/src/main/java/eu/lepiller/nani/dictionary/FileDictionary.java

7373
                byte[] result = digest.digest();
7474
                String hash = String.format("%0" + (result.length*2) + "X", new BigInteger(1, result)).toLowerCase();
7575
76-
                StringBuilder sb = new StringBuilder();
7776
                try {
78-
                    char data[] = new char[4096];
79-
                    FileReader fr = new FileReader(sha256);
80-
                    int count;
81-
                    while((count = fr.read(data)) != -1) {
82-
                        sb.append(data, 0, count);
83-
                    }
77+
                    String expected = readSha256FromFile(sha256);
8478
8579
                    // If the file has the expected hash, we have it
86-
                    Log.d(TAG, "expected: " + sb.toString().trim() + "; actual: " + hash);
87-
                    return sb.toString().trim().compareTo(hash) == 0;
80+
                    Log.d(TAG, "expected: " + expected + "; actual: " + hash);
81+
                    return expected.compareTo(hash) == 0;
8882
                } catch (FileNotFoundException e) {
8983
                    e.printStackTrace();
9084
                } catch (IOException e) {