use of android.os.MemoryFile in project fresco by facebook.
the class GingerbreadPurgeableDecoder method decodeFileDescriptorAsPurgeable.
private Bitmap decodeFileDescriptorAsPurgeable(CloseableReference<PooledByteBuffer> bytesRef, int inputLength, @Nullable byte[] suffix, BitmapFactory.Options options) {
MemoryFile memoryFile = null;
try {
memoryFile = copyToMemoryFile(bytesRef, inputLength, suffix);
FileDescriptor fd = getMemoryFileDescriptor(memoryFile);
if (mWebpBitmapFactory != null) {
Bitmap bitmap = mWebpBitmapFactory.decodeFileDescriptor(fd, null, options);
return Preconditions.checkNotNull(bitmap, "BitmapFactory returned null");
} else {
throw new IllegalStateException("WebpBitmapFactory is null");
}
} catch (IOException e) {
throw Throwables.propagate(e);
} finally {
if (memoryFile != null) {
memoryFile.close();
}
}
}
use of android.os.MemoryFile in project matrix-android-sdk by matrix-org.
the class AttachmentEncryptionTest method checkDecryption.
private String checkDecryption(String input, EncryptedFileInfo encryptedFileInfo) throws Exception {
byte[] in = Base64.decode(input, Base64.DEFAULT);
MemoryFile memoryFile;
if (0 == in.length) {
memoryFile = new MemoryFile("file" + System.currentTimeMillis(), 0);
} else {
memoryFile = new MemoryFile("file" + System.currentTimeMillis(), in.length);
memoryFile.getOutputStream().write(in);
}
InputStream decryptedStream = MXEncryptedAttachments.decryptAttachment(memoryFile.getInputStream(), encryptedFileInfo);
assertTrue(null != decryptedStream);
byte[] buffer = new byte[100];
int len = decryptedStream.read(buffer);
return Base64.encodeToString(buffer, 0, len, Base64.DEFAULT).replaceAll("\n", "").replaceAll("=", "");
}
use of android.os.MemoryFile in project android-database-sqlcipher by sqlcipher.
the class SQLiteContentHelper method simpleQueryForBlobMemoryFile.
/**
* 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();
}
}
use of android.os.MemoryFile in project fresco by facebook.
the class GingerbreadPurgeableDecoder method copyToMemoryFile.
private static MemoryFile copyToMemoryFile(CloseableReference<PooledByteBuffer> bytesRef, int inputLength, @Nullable byte[] suffix) throws IOException {
int outputLength = inputLength + (suffix == null ? 0 : suffix.length);
MemoryFile memoryFile = new MemoryFile(null, outputLength);
memoryFile.allowPurging(false);
PooledByteBufferInputStream pbbIs = null;
LimitedInputStream is = null;
OutputStream os = null;
try {
pbbIs = new PooledByteBufferInputStream(bytesRef.get());
is = new LimitedInputStream(pbbIs, inputLength);
os = memoryFile.getOutputStream();
ByteStreams.copy(is, os);
if (suffix != null) {
memoryFile.writeBytes(suffix, 0, inputLength, suffix.length);
}
return memoryFile;
} finally {
CloseableReference.closeSafely(bytesRef);
Closeables.closeQuietly(pbbIs);
Closeables.closeQuietly(is);
Closeables.close(os, true);
}
}
use of android.os.MemoryFile in project fresco by facebook.
the class WebpDecodingTest method test_webp_extended_with_alpha_decoding_filedescriptor_bitmap.
@Test
public void test_webp_extended_with_alpha_decoding_filedescriptor_bitmap() throws Throwable {
final MemoryFile memoryFile = getMemoryFile("webp_ea.webp");
final Bitmap bitmap = mWebpBitmapFactory.decodeFileDescriptor(getMemoryFileDescriptor(memoryFile), null, null);
memoryFile.close();
assertBitmap(bitmap, 400, 301);
}
Aggregations