Search in sources :

Example 6 with MemoryFile

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();
        }
    }
}
Also used : MemoryFile(android.os.MemoryFile) Bitmap(android.graphics.Bitmap) IOException(java.io.IOException) FileDescriptor(java.io.FileDescriptor)

Example 7 with MemoryFile

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("=", "");
}
Also used : MemoryFile(android.os.MemoryFile) InputStream(java.io.InputStream)

Example 8 with MemoryFile

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();
    }
}
Also used : MemoryFile(android.os.MemoryFile) Cursor(android.database.Cursor)

Example 9 with MemoryFile

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);
    }
}
Also used : MemoryFile(android.os.MemoryFile) PooledByteBufferInputStream(com.facebook.common.memory.PooledByteBufferInputStream) LimitedInputStream(com.facebook.common.streams.LimitedInputStream) OutputStream(java.io.OutputStream)

Example 10 with MemoryFile

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);
}
Also used : MemoryFile(android.os.MemoryFile) Bitmap(android.graphics.Bitmap) Test(org.junit.Test)

Aggregations

MemoryFile (android.os.MemoryFile)12 Bitmap (android.graphics.Bitmap)5 Test (org.junit.Test)5 IOException (java.io.IOException)3 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 AssetFileDescriptor (android.content.res.AssetFileDescriptor)1 Cursor (android.database.Cursor)1 SensorDirectChannel (android.hardware.SensorDirectChannel)1 PooledByteBufferInputStream (com.facebook.common.memory.PooledByteBufferInputStream)1 LimitedInputStream (com.facebook.common.streams.LimitedInputStream)1 FileDescriptor (java.io.FileDescriptor)1 FileNotFoundException (java.io.FileNotFoundException)1 Config (org.robolectric.annotation.Config)1