• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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 
17 package android.database.sqlite;
18 
19 import android.content.res.AssetFileDescriptor;
20 import android.database.Cursor;
21 import android.os.MemoryFile;
22 
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 
26 /**
27  * Some helper functions for using SQLite database to implement content providers.
28  *
29  * @hide
30  */
31 public class SQLiteContentHelper {
32 
33     /**
34      * Runs an SQLite query and returns an AssetFileDescriptor for the
35      * blob in column 0 of the first row. If the first column does
36      * not contain a blob, an unspecified exception is thrown.
37      *
38      * @param db Handle to a readable database.
39      * @param sql SQL query, possibly with query arguments.
40      * @param selectionArgs Query argument values, or {@code null} for no argument.
41      * @return If no exception is thrown, a non-null AssetFileDescriptor is returned.
42      * @throws FileNotFoundException If the query returns no results or the
43      *         value of column 0 is NULL, or if there is an error creating the
44      *         asset file descriptor.
45      */
getBlobColumnAsAssetFile(SQLiteDatabase db, String sql, String[] selectionArgs)46     public static AssetFileDescriptor getBlobColumnAsAssetFile(SQLiteDatabase db, String sql,
47             String[] selectionArgs) throws FileNotFoundException {
48         try {
49             MemoryFile file = simpleQueryForBlobMemoryFile(db, sql, selectionArgs);
50             if (file == null) {
51                 throw new FileNotFoundException("No results.");
52             }
53             return AssetFileDescriptor.fromMemoryFile(file);
54         } catch (IOException ex) {
55             throw new FileNotFoundException(ex.toString());
56         }
57     }
58 
59     /**
60      * Runs an SQLite query and returns a MemoryFile for the
61      * blob in column 0 of the first row. If the first column does
62      * not contain a blob, an unspecified exception is thrown.
63      *
64      * @return A memory file, or {@code null} if the query returns no results
65      *         or the value column 0 is NULL.
66      * @throws IOException If there is an error creating the memory file.
67      */
68     // TODO: make this native and use the SQLite blob API to reduce copying
simpleQueryForBlobMemoryFile(SQLiteDatabase db, String sql, String[] selectionArgs)69     private static MemoryFile simpleQueryForBlobMemoryFile(SQLiteDatabase db, String sql,
70             String[] selectionArgs) throws IOException {
71         Cursor cursor = db.rawQuery(sql, selectionArgs);
72         if (cursor == null) {
73             return null;
74         }
75         try {
76             if (!cursor.moveToFirst()) {
77                 return null;
78             }
79             byte[] bytes = cursor.getBlob(0);
80             if (bytes == null) {
81                 return null;
82             }
83             MemoryFile file = new MemoryFile(null, bytes.length);
84             file.writeBytes(bytes, 0, 0, bytes.length);
85             file.deactivate();
86             return file;
87         } finally {
88             cursor.close();
89         }
90     }
91 
92 }
93