Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14792 manas 1
/**
2
 * Copyright 2010-present Facebook.
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
 
17
package com.facebook.internal;
18
 
19
import android.content.Context;
20
import android.util.Log;
21
import com.facebook.LoggingBehavior;
22
 
23
import java.io.BufferedInputStream;
24
import java.io.IOException;
25
import java.io.InputStream;
26
import java.net.HttpURLConnection;
27
import java.net.URI;
28
import java.net.URISyntaxException;
29
import java.net.URL;
30
 
31
class ImageResponseCache {
32
    static final String TAG = ImageResponseCache.class.getSimpleName();
33
 
34
    private volatile static FileLruCache imageCache;
35
 
36
    synchronized static FileLruCache getCache(Context context) throws IOException{
37
        if (imageCache == null) {
38
            imageCache = new FileLruCache(context.getApplicationContext(), TAG, new FileLruCache.Limits());
39
        }
40
        return imageCache;
41
    }
42
 
43
    // Get stream from cache, or return null if the image is not cached.
44
    // Does not throw if there was an error.
45
    static InputStream getCachedImageStream(URI url, Context context) {
46
        InputStream imageStream = null;
47
        if (url != null) {
48
            if (isCDNURL(url)) {
49
                try {
50
                    FileLruCache cache = getCache(context);
51
                    imageStream = cache.get(url.toString());
52
                } catch (IOException e) {
53
                    Logger.log(LoggingBehavior.CACHE, Log.WARN, TAG, e.toString());
54
                }
55
            }
56
        }
57
 
58
        return imageStream;
59
    }
60
 
61
    static InputStream interceptAndCacheImageStream(Context context, HttpURLConnection connection) throws IOException {
62
        InputStream stream = null;
63
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
64
            URL url = connection.getURL();
65
            stream = connection.getInputStream(); // Default stream in case caching fails
66
            try {
67
                if (isCDNURL(url.toURI())) {
68
                    FileLruCache cache = getCache(context);
69
 
70
                    // Wrap stream with a caching stream
71
                    stream = cache.interceptAndPut(
72
                            url.toString(),
73
                            new BufferedHttpInputStream(stream, connection));
74
                }
75
            } catch (IOException e) {
76
                // Caching is best effort
77
            } catch (URISyntaxException e) {
78
            // Caching is best effort
79
            }
80
        }
81
        return stream;
82
    }
83
 
84
   private static boolean isCDNURL(URI url) {
85
        if (url != null) {
86
            String uriHost = url.getHost();
87
 
88
            if (uriHost.endsWith("fbcdn.net")) {
89
                return true;
90
            }
91
 
92
            if (uriHost.startsWith("fbcdn") && uriHost.endsWith("akamaihd.net")) {
93
                return true;
94
            }
95
        }
96
 
97
        return false;
98
    }
99
 
100
    static void clearCache(Context context) {
101
        try {
102
            getCache(context).clearCache();
103
        } catch (IOException e) {
104
            Logger.log(LoggingBehavior.CACHE, Log.WARN, TAG, "clearCache failed " + e.getMessage());
105
        }
106
    }
107
 
108
    private static class BufferedHttpInputStream extends BufferedInputStream {
109
        HttpURLConnection connection;
110
        BufferedHttpInputStream(InputStream stream, HttpURLConnection connection) {
111
            super(stream, Utility.DEFAULT_STREAM_BUFFER_SIZE);
112
            this.connection = connection;
113
        }
114
 
115
        @Override
116
        public void close() throws IOException {
117
            super.close();
118
            Utility.disconnectQuietly(connection);
119
        }
120
    }
121
}
122