|
1 /* |
|
2 BEEM is a videoconference application on the Android Platform. |
|
3 |
|
4 Copyright (C) 2009 by Frederic-Charles Barthelery, |
|
5 Jean-Manuel Da Silva, |
|
6 Nikita Kozlov, |
|
7 Philippe Lago, |
|
8 Jean Baptiste Vergely, |
|
9 Vincent Veronis. |
|
10 |
|
11 This file is part of BEEM. |
|
12 |
|
13 BEEM is free software: you can redistribute it and/or modify |
|
14 it under the terms of the GNU General Public License as published by |
|
15 the Free Software Foundation, either version 3 of the License, or |
|
16 (at your option) any later version. |
|
17 |
|
18 BEEM is distributed in the hope that it will be useful, |
|
19 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 GNU General Public License for more details. |
|
22 |
|
23 You should have received a copy of the GNU General Public License |
|
24 along with BEEM. If not, see <http://www.gnu.org/licenses/>. |
|
25 |
|
26 Please send bug reports with examples or suggestions to |
|
27 contact@beem-project.com or http://dev.beem-project.com/ |
|
28 |
|
29 Epitech, hereby disclaims all copyright interest in the program "Beem" |
|
30 written by Frederic-Charles Barthelery, |
|
31 Jean-Manuel Da Silva, |
|
32 Nikita Kozlov, |
|
33 Philippe Lago, |
|
34 Jean Baptiste Vergely, |
|
35 Vincent Veronis. |
|
36 |
|
37 Nicolas Sadirac, November 26, 2009 |
|
38 President of Epitech. |
|
39 |
|
40 Flavien Astraud, November 26, 2009 |
|
41 Head of the EIP Laboratory. |
|
42 |
|
43 */ |
|
44 package com.beem.project.beem.service; |
|
45 |
|
46 import android.content.ContentResolver; |
|
47 import android.content.Context; |
|
48 |
|
49 import android.database.Cursor; |
|
50 |
|
51 import android.net.Uri; |
|
52 |
|
53 import com.beem.project.beem.providers.AvatarProvider; |
|
54 import com.beem.project.beem.smack.avatar.AvatarCache; |
|
55 |
|
56 import java.io.BufferedInputStream; |
|
57 import java.io.BufferedOutputStream; |
|
58 import java.io.ByteArrayOutputStream; |
|
59 import java.io.InputStream; |
|
60 import java.io.IOException; |
|
61 import java.io.OutputStream; |
|
62 |
|
63 /** |
|
64 * An implementation of an AvatarCache which store the data of the filesystem. |
|
65 */ |
|
66 public class BeemAvatarCache implements AvatarCache { |
|
67 |
|
68 private static final String TAG = BeemAvatarCache.class.getSimpleName(); |
|
69 |
|
70 private Context mContext; |
|
71 private ContentResolver mContentResolver; |
|
72 |
|
73 /** |
|
74 * Create a BeemAvatarCache. |
|
75 * |
|
76 * @param ctx The android context of the cache. |
|
77 */ |
|
78 public BeemAvatarCache(final Context ctx) { |
|
79 mContext = ctx; |
|
80 mContentResolver = mContext.getContentResolver(); |
|
81 } |
|
82 |
|
83 |
|
84 @Override |
|
85 public void put(String key, byte[] data) throws IOException { |
|
86 Uri uri = AvatarProvider.CONTENT_URI.buildUpon().appendPath(key).build(); |
|
87 OutputStream os = new BufferedOutputStream(mContentResolver.openOutputStream(uri)); |
|
88 try { |
|
89 os.write(data); |
|
90 } finally { |
|
91 os.close(); |
|
92 } |
|
93 } |
|
94 |
|
95 @Override |
|
96 public void put(String key, InputStream in) throws IOException { |
|
97 Uri uri = AvatarProvider.CONTENT_URI.buildUpon().appendPath(key).build(); |
|
98 OutputStream os = new BufferedOutputStream(mContentResolver.openOutputStream(uri)); |
|
99 try { |
|
100 byte[] data = new byte[1024]; |
|
101 int nbread; |
|
102 while ((nbread = in.read(data)) != -1) |
|
103 os.write(data, 0, nbread); |
|
104 } finally { |
|
105 in.close(); |
|
106 os.close(); |
|
107 } |
|
108 } |
|
109 |
|
110 @Override |
|
111 public byte[] get(String key) throws IOException { |
|
112 Uri uri = AvatarProvider.CONTENT_URI.buildUpon().appendPath(key).build(); |
|
113 InputStream is = new BufferedInputStream(mContentResolver.openInputStream(uri)); |
|
114 ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
|
115 try { |
|
116 byte[] data = new byte[1024]; |
|
117 is.read(data); |
|
118 bos.write(data); |
|
119 } finally { |
|
120 is.close(); |
|
121 } |
|
122 return bos.toByteArray(); |
|
123 } |
|
124 |
|
125 @Override |
|
126 public boolean contains(String key) { |
|
127 Uri uri = AvatarProvider.CONTENT_URI.buildUpon().appendPath(key).build(); |
|
128 uri = Uri.parse("content://com.beem.project.beem.providers.avatarprovider"); |
|
129 Cursor c = mContentResolver.query(uri, null, null, null, null); |
|
130 boolean res = c.getCount() > 0; |
|
131 c.close(); |
|
132 return res; |
|
133 } |
|
134 } |