diff options
author | Bjorn Bringert <bringert@android.com> | 2009-06-01 21:50:09 +0100 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2009-06-04 09:57:18 -0700 |
commit | 7fb952ac237bbb99d5ccc1455225a7ea07992312 (patch) | |
tree | 4a677fd030946679a29277fb3f361763d4fbc71c | |
parent | f6b72523e28555b8ddd5bc799a5e4b1d1259dcae (diff) | |
download | frameworks_base-7fb952ac237bbb99d5ccc1455225a7ea07992312.zip frameworks_base-7fb952ac237bbb99d5ccc1455225a7ea07992312.tar.gz frameworks_base-7fb952ac237bbb99d5ccc1455225a7ea07992312.tar.bz2 |
Allow making AssetFileDescriptors from SQLite blobs.
This change adds a new class SQLiteContentHelper, which contains
a static method for creating an AssetFileDescriptor from
an SQLite query that returns a blob.
Internally, this uses a file descriptor for a MemoryFile.
The implementation is temporary. Ideally, the data should be copied
directly from SQLite to the MemoryFile ashmem region, using
sqlite3_blob_read().
This is part of the implementation of http://b/issue?id=1871731
-rw-r--r-- | core/java/android/database/sqlite/SQLiteContentHelper.java | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/core/java/android/database/sqlite/SQLiteContentHelper.java b/core/java/android/database/sqlite/SQLiteContentHelper.java new file mode 100644 index 0000000..2800d86 --- /dev/null +++ b/core/java/android/database/sqlite/SQLiteContentHelper.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.database.sqlite; + +import android.content.res.AssetFileDescriptor; +import android.database.Cursor; +import android.os.MemoryFile; + +import java.io.FileNotFoundException; +import java.io.IOException; + +/** + * Some helper functions for using SQLite database to implement content providers. + * + * @hide + */ +public class SQLiteContentHelper { + + /** + * Runs an SQLite query and returns an AssetFileDescriptor for the + * blob in column 0 of the first row. If the first column does + * not contain a blob, an unspecified exception is thrown. + * + * @param db Handle to a readable database. + * @param sql SQL query, possibly with query arguments. + * @param selectionArgs Query argument values, or {@code null} for no argument. + * @return If no exception is thrown, a non-null AssetFileDescriptor is returned. + * @throws FileNotFoundException If the query returns no results or the + * value of column 0 is NULL, or if there is an error creating the + * asset file descriptor. + */ + public static AssetFileDescriptor getBlobColumnAsAssetFile(SQLiteDatabase db, String sql, + String[] selectionArgs) throws FileNotFoundException { + try { + MemoryFile file = simpleQueryForBlobMemoryFile(db, sql, selectionArgs); + if (file == null) { + throw new FileNotFoundException("No results."); + } + return AssetFileDescriptor.fromMemoryFile(file); + } catch (IOException ex) { + throw new FileNotFoundException(ex.toString()); + } + } + + /** + * Runs an SQLite query and returns a MemoryFile for the + * blob in column 0 of the first row. If the first column does + * not contain a blob, an unspecified exception is thrown. + * + * @return A memory file, or {@code null} if the query returns no results + * or the value column 0 is NULL. + * @throws IOException If there is an error creating the memory file. + */ + // TODO: make this native and use the SQLite blob API to reduce copying + private static MemoryFile simpleQueryForBlobMemoryFile(SQLiteDatabase db, String sql, + String[] selectionArgs) throws IOException { + Cursor cursor = db.rawQuery(sql, selectionArgs); + if (cursor == null) { + return null; + } + try { + if (!cursor.moveToFirst()) { + return null; + } + byte[] bytes = cursor.getBlob(0); + if (bytes == null) { + return null; + } + MemoryFile file = new MemoryFile(null, bytes.length); + file.writeBytes(bytes, 0, 0, bytes.length); + file.deactivate(); + return file; + } finally { + cursor.close(); + } + } + +} |