Download .sha256 file if it exists

Julien LepillerMon May 25 20:02:19+0200 2020

e140e79

Download .sha256 file if it exists

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

149149
150150
            Log.d(TAG, "Start downloading");
151151
152-
            for (Map.Entry<URL, File> e : d.getDownloads().entrySet()) {
152+
            for (Map.Entry<String, File> e : d.getDownloads().entrySet()) {
153153
                try {
154-
                    URL url = e.getKey();
155-
                    Log.d(TAG, "URL: " + url.toString());
154+
                    String uri = e.getKey();
155+
                    Log.d(TAG, "URL: " + uri);
156+
157+
                    // Download a .sha256 file if it exists
158+
                    URL url = new URL(uri + ".sha256");
159+
                    byte data[] = new byte[4096];
160+
                    int count;
161+
                    File file;
162+
163+
                    connection = (HttpURLConnection) url.openConnection();
164+
                    connection.connect();
165+
                    if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
166+
                        input = connection.getInputStream();
167+
                        file = new File(e.getValue() + ".sha256");
168+
                        file.getParentFile().mkdirs();
169+
                        output = new FileOutputStream(file);
170+
                        while((count = input.read(data)) != -1) {
171+
                            if (isCancelled()) {
172+
                                input.close();
173+
                                return null;
174+
                            }
175+
                            output.write(data, 0, count);
176+
                        }
177+
                    }
178+
179+
                    // .sha256 file now downloaded
180+
                    url = new URL(uri);
156181
                    connection = (HttpURLConnection) url.openConnection();
157182
                    connection.connect();
158183

171196
172197
                    // download the file
173198
                    input = connection.getInputStream();
174-
                    File file = e.getValue();
175-
                    file.getParentFile().mkdirs();
199+
                    file = e.getValue();
176200
                    output = new FileOutputStream(file);
177201
178-
                    byte data[] = new byte[4096];
179202
                    long total = 0;
180-
                    int count;
181203
                    while ((count = input.read(data)) != -1) {
182204
                        // allow canceling with back button
183205
                        if (isCancelled()) {

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

4242
4343
    abstract public int size();
4444
45-
    // Used for downloads: tells what siwe we currently have downloaded
45+
    // Used for downloads: tells what size we currently have downloaded
4646
    abstract int currentSize();
4747
48-
    public abstract Map<URL, File> getDownloads();
48+
    public abstract Map<String, File> getDownloads();
4949
5050
    public Drawable getDrawable(Context context) {
5151
        Drawable drawable;

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

5454
    }
5555
5656
    @Override
57-
    public Map<URL, File> getDownloads() {
58-
        HashMap<URL, File> result = new HashMap<>();
59-
        try {
60-
            result.put(new URL(mUrl), getFile());
61-
        } catch (MalformedURLException e) {
62-
            e.printStackTrace();
63-
        }
57+
    public Map<String, File> getDownloads() {
58+
        HashMap<String, File> result = new HashMap<>();
59+
        result.put(mUrl, getFile());
6460
        return result;
6561
    }
6662