Skip to content

Instantly share code, notes, and snippets.

@huttneab
Created September 26, 2012 18:54
Show Gist options
  • Save huttneab/3789827 to your computer and use it in GitHub Desktop.
Save huttneab/3789827 to your computer and use it in GitHub Desktop.
the original openFileMethod of my content provider and the two alternate methods I use instead
// including the imports to for the sake of the io objects
import info.guardianproject.database.sqlcipher.SQLiteDatabase;
import info.guardianproject.database.sqlcipher.SQLiteQueryBuilder;
import info.guardianproject.iocipher.File;
import info.guardianproject.iocipher.FileInputStream;
import info.guardianproject.iocipher.FileOutputStream;
import info.guardianproject.iocipher.VirtualFileSystem;
...
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
ParcelFileDescriptor fd;
int match = sURLMatcher.match(uri);
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.d(TAG, "openFile: uri=" + uri + ", mode=" + mode);
}
switch (match) {
case MMS_SCRAP_SPACE:
fd = getTempStoreFd();
break;
default:
fd = openFileHelper(uri, mode);
}
return fd;
}
public static synchronized InputStream getEncryptedFileInputStream(Context context, Uri uri) throws FileNotFoundException {
if (uri.getAuthority().startsWith("com.gryphn")) {
Cursor c = context.getContentResolver().query(uri, new String[] { "_data" }, null, null, null);
int count = (c != null) ? c.getCount() : 0;
if (count != 1) {
// If there is not exactly one result, throw an appropriate
// exception.
if (c != null) {
c.close();
}
if (count == 0) {
throw new FileNotFoundException("No entry for " + uri);
}
throw new FileNotFoundException("Multiple items at " + uri);
}
c.moveToFirst();
int i = c.getColumnIndex("_data");
String path = (i >= 0 ? c.getString(i) : null);
c.close();
if (path == null) {
throw new FileNotFoundException("Column _data not found.");
}
return new FileInputStream(new File(path));
} else {
return context.getContentResolver().openInputStream(uri);
}
}
public static OutputStream getEncryptedFileOutputStream(Context context, Uri uri) throws FileNotFoundException {
if (uri.getAuthority().startsWith("com.gryphn")) {
Cursor c = context.getContentResolver().query(uri, new String[] { "_data" }, null, null, null);
int count = (c != null) ? c.getCount() : 0;
if (count != 1) {
// If there is not exactly one result, throw an appropriate
// exception.
if (c != null) {
c.close();
}
if (count == 0) {
throw new FileNotFoundException("No entry for " + uri);
}
throw new FileNotFoundException("Multiple items at " + uri);
}
c.moveToFirst();
int i = c.getColumnIndex("_data");
String path = (i >= 0 ? c.getString(i) : null);
c.close();
if (path == null) {
throw new FileNotFoundException("Column _data not found.");
}
return new FileOutputStream(path);
} else {
return context.getContentResolver().openOutputStream(uri);
}
}
private void persistData(PduPart part, Uri uri, String contentType) throws MmsException {
OutputStream os = null;
InputStream is = null;
IOCipherFileChannel destinationFileChannel = null;
IOCipherFileChannel sourceCipherFileChannel = null;
FileChannel sourceNativeFileChannel = null;
try {
byte[] data = part.getData();
if ("text/plain".equals(contentType) || "application/smil".equals(contentType)) {
ContentValues cv = new ContentValues();
cv.put(SecureCom.Mms.Part.TEXT, new EncodedStringValue(data).getString());
if (mContentResolver.update(uri, cv, null, null) != 1) {
throw new MmsException("unable to update " + uri.toString());
}
} else {
// os = mContentResolver.openOutputStream(uri);
os = (FileOutputStream) SecureComMmsProvider.getEncryptedFileOutputStream(mContext, uri);
if (data == null) {
Uri dataUri = part.getDataUri();
if ((dataUri == null) || (dataUri == uri)) {
Log.w(TAG, "Can't find data for this part.");
return;
}
// is = mContentResolver.openInputStream(dataUri);
is = SecureComMmsProvider.getEncryptedFileInputStream(mContext, dataUri);
if (LOCAL_LOGV) {
Log.v(TAG, "Saving data to: " + uri);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream(is.available());
byte[] buffer = new byte[256];
for (int len = 0; (len = is.read(buffer)) != -1;) {
baos.write(buffer,0,len);
}
os.write(baos.toByteArray());
baos.close();
// destinationFileChannel = ((FileOutputStream)os).getChannel();
//
// if(is instanceof info.guardianproject.iocipher.FileInputStream){
// if (LOCAL_LOGV) {
// Log.v(TAG, "transfer from Cipher to Cipher : " + uri);
// }
//
// sourceCipherFileChannel = ((FileInputStream) is).getChannel();
// sourceCipherFileChannel.transferTo(0, sourceCipherFileChannel.size(), destinationFileChannel);
// sourceCipherFileChannel.close();
// } else {
// if (LOCAL_LOGV) {
// Log.v(TAG, "transfer from Native to Cipher : " + uri);
// }
//
// sourceNativeFileChannel = ((java.io.FileInputStream)is).getChannel();
// sourceNativeFileChannel.transferTo(0, sourceNativeFileChannel.size(), destinationFileChannel);
// sourceNativeFileChannel.close();
// }
//
// destinationFileChannel.close();
} else {
if (LOCAL_LOGV) {
Log.v(TAG, "Saving data to: " + uri);
}
os.write(data);
}
}
} catch (FileNotFoundException e) {
Log.e(TAG, "Failed to open Input/Output stream.", e);
throw new MmsException(e);
} catch (IOException e) {
Log.e(TAG, "Failed to read/write data.", e);
throw new MmsException(e);
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
Log.e(TAG, "IOException while closing: " + os, e);
} // Ignore
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
Log.e(TAG, "IOException while closing: " + is, e);
} // Ignore
}
if(sourceCipherFileChannel !=null){
try{
sourceCipherFileChannel.close();
} catch(IOException e){
Log.e(TAG, "IOException while closing: " + sourceCipherFileChannel, e);
}
}
if(sourceNativeFileChannel !=null){
try{
sourceNativeFileChannel.close();
} catch(IOException e){
Log.e(TAG, "IOException while closing: " + sourceNativeFileChannel, e);
}
}
if(destinationFileChannel !=null){
try{
destinationFileChannel.close();
} catch(IOException e){
Log.e(TAG, "IOException while closing: " + destinationFileChannel, e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment