1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.gallery3d.data;
18
19import com.android.gallery3d.common.Utils;
20import com.android.gallery3d.util.ThreadPool.CancelListener;
21import com.android.gallery3d.util.ThreadPool.JobContext;
22
23import java.io.ByteArrayOutputStream;
24import java.io.File;
25import java.io.FileOutputStream;
26import java.io.IOException;
27import java.io.InputStream;
28import java.io.InterruptedIOException;
29import java.io.OutputStream;
30import java.net.URL;
31
32public class DownloadUtils {
33    private static final String TAG = "DownloadService";
34
35    public static boolean requestDownload(JobContext jc, URL url, File file) {
36        FileOutputStream fos = null;
37        try {
38            fos = new FileOutputStream(file);
39            return download(jc, url, fos);
40        } catch (Throwable t) {
41            return false;
42        } finally {
43            Utils.closeSilently(fos);
44        }
45    }
46
47    public static byte[] requestDownload(JobContext jc, URL url) {
48        ByteArrayOutputStream baos = null;
49        try {
50            baos = new ByteArrayOutputStream();
51            if (!download(jc, url, baos)) {
52                return null;
53            }
54            return baos.toByteArray();
55        } catch (Throwable t) {
56            Log.w(TAG, t);
57            return null;
58        } finally {
59            Utils.closeSilently(baos);
60        }
61    }
62
63    public static void dump(JobContext jc, InputStream is, OutputStream os)
64            throws IOException {
65        byte buffer[] = new byte[4096];
66        int rc = is.read(buffer, 0, buffer.length);
67        final Thread thread = Thread.currentThread();
68        jc.setCancelListener(new CancelListener() {
69            public void onCancel() {
70                thread.interrupt();
71            }
72        });
73        while (rc > 0) {
74            if (jc.isCancelled()) throw new InterruptedIOException();
75            os.write(buffer, 0, rc);
76            rc = is.read(buffer, 0, buffer.length);
77        }
78        jc.setCancelListener(null);
79        Thread.interrupted(); // consume the interrupt signal
80    }
81
82    public static boolean download(JobContext jc, URL url, OutputStream output) {
83        InputStream input = null;
84        try {
85            input = url.openStream();
86            dump(jc, input, output);
87            return true;
88        } catch (Throwable t) {
89            Log.w(TAG, "fail to download", t);
90            return false;
91        } finally {
92            Utils.closeSilently(input);
93        }
94    }
95}