user1132897
user1132897

Reputation: 1201

Way to improve bulk file download?

I am trying to download a large amount of small png images which will be used in my application. It works right now, but takes a long time. I was wondering if there are any suggestions on how to speed this process up or if there are any glaring inefficiencies in my code. The process right now is to download a JSON file. This JSON file has links in it to the zip files which contain the images. I then unzip the file and store the images in the phone's internal storage. Here's what I've got so far:

    @Override
    protected Object doInBackground(Object... params) {

        IS_UPDATING = true;
        final String input = Utils
                .downloadString("http://assets.json");

        if (input == null) {
            return null;
        }

        try {
            JSONObject data = new JSONObject(input);
            Iterator<String> topIterator = data.keys();
            while (topIterator.hasNext())
            {
                String topName = topIterator.next();
                String prefix = "png_";
                String preferences = null;
                JSONObject part_data = data.getJSONObject(topName);
                int map_version = part_data.getInt("VersionNumber");
                JSONObject url_json = new JSONObject(input).getJSONObject("Tiles").getJSONObject("TileURLs");
                Iterator<String> iterator = url_json.keys();
                while (iterator.hasNext())
                {
                    String temp = iterator.next();
                    //Downloads the zip file with map tiles
                    URL url = new URL(url_json.getString(temp));
                    InputStream is = url.openStream();
                    DataInputStream dis = new DataInputStream(is);
                    byte[] buffer = new byte[1024];
                    int length;

                    //Stores the zip file in app output
                    FileOutputStream fos = mContext
                            .openFileOutput("tiles_" + temp + ".zip",
                                    Context.MODE_PRIVATE);
                    while ((length = dis.read(buffer)) > 0)
                        fos.write(buffer, 0, length);
                    fos.close();
                    dis.close();
                    is.close();

                    //Extracts the images from the zip file
                    FileInputStream fis = mContext
                            .openFileInput("tiles_" + temp + ".zip");
                    ZipInputStream zis = new ZipInputStream(fis);
                    BufferedInputStream in = new BufferedInputStream(zis);
                    ZipEntry ze = null;

                    while ((ze = zis.getNextEntry()) != null)
                    {
                        String name = ze.getName();
                        if (!ze.isDirectory() && !name.contains("MACOSX"))
                        {
                            name = prefix + ze.getName();
                            name = name.replace('/', '_');
                            fos = mContext.openFileOutput(name, Context.MODE_PRIVATE);

                            for (int y = in.read(); y != -1; y = in.read()) {
                                fos.write(y);
                            }

                            zis.closeEntry();
                            fos.close();
                        }
                    }
                    in.close();
                    zis.close();
                    fis.close();
                    mContext.deleteFile("tiles_" + temp + ".zip");
                }

                sEditor.putInt(preferences, map_version);
                sEditor.commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finished = true;
        return null;
    }

This is done in an async task. Are there any ways I can improve the speed of this? Thanks for any input!

Upvotes: 0

Views: 111

Answers (1)

Krylez
Krylez

Reputation: 17800

Yes, download all the files to the temp directory first, then parse them afterward.

Another thing you can do is to not use the stuff in the org.json package. Gson is a great alternative.

Upvotes: 1

Related Questions