Java 类android.os.MemoryFile 实例源码

项目:GitHub    文件:GingerbreadPurgeableDecoder.java   
protected Bitmap decodeFileDescriptorAsPurgeable(
    CloseableReference<PooledByteBuffer> bytesRef,
    int inputLength,
    byte[] suffix,
    BitmapFactory.Options options) {
  MemoryFile memoryFile = null;
  try {
    memoryFile = copyToMemoryFile(bytesRef, inputLength, suffix);
    FileDescriptor fd = getMemoryFileDescriptor(memoryFile);
    Bitmap bitmap = sWebpBitmapFactory.decodeFileDescriptor(fd, null, options);
    return Preconditions.checkNotNull(bitmap, "BitmapFactory returned null");
  } catch (IOException e) {
    throw Throwables.propagate(e);
  } finally {
    if (memoryFile != null) {
      memoryFile.close();
    }
  }
}
项目:FontProvider    文件:FontManager.java   
public static void init(Context context) {
    if (sFonts != null) {
        return;
    }

    sCache = new LruCache<String, MemoryFile>(FontProviderSettings.getMaxCache()) {
        @Override
        protected void entryRemoved(boolean evicted, String key, MemoryFile oldValue, MemoryFile newValue) {
            if (evicted) {
                oldValue.close();
            }
        }

        @Override
        protected int sizeOf(String key, MemoryFile value) {
            return value.length();
        }
    };

    sFonts = new ArrayList<>();

    for (int res : FONTS_RES) {
        FontInfo font = new Gson().fromJson(new InputStreamReader(context.getResources().openRawResource(res)), FontInfo.class);
        sFonts.add(font);
    }
}
项目:ebook    文件:WordParseUtils.java   
public static MemoryFile openMemoryFile( String filename, int length )
{
    try 
    {
        MemoryFile memoryFile = new MemoryFile(filename, length);
        if( memoryFile != null )
        {
            memoryFile.allowPurging(false);
        }

        return  memoryFile;
    } 
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return  null;
}
项目:fresco    文件:GingerbreadPurgeableDecoder.java   
protected Bitmap decodeFileDescriptorAsPurgeable(
    CloseableReference<PooledByteBuffer> bytesRef,
    int inputLength,
    byte[] suffix,
    BitmapFactory.Options options) {
  MemoryFile memoryFile = null;
  try {
    memoryFile = copyToMemoryFile(bytesRef, inputLength, suffix);
    FileDescriptor fd = getMemoryFileDescriptor(memoryFile);
    Bitmap bitmap = sWebpBitmapFactory.decodeFileDescriptor(fd, null, options);
    return Preconditions.checkNotNull(bitmap, "BitmapFactory returned null");
  } catch (IOException e) {
    throw Throwables.propagate(e);
  } finally {
    if (memoryFile != null) {
      memoryFile.close();
    }
  }
}
项目:silent-contacts-android    文件:DbQueryUtils.java   
/**
 * 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 {

    android.os.ParcelFileDescriptor fd = null;

    try {
        MemoryFile file = simpleQueryForBlobMemoryFile(db, sql, selectionArgs);
        if (file == null) {
            throw new FileNotFoundException("No results.");
        }
        Class<?> c = file.getClass();
        try {
            java.lang.reflect.Method m = c.getDeclaredMethod("getParcelFileDescriptor");
            m.setAccessible(true);
            fd = (android.os.ParcelFileDescriptor)m.invoke(file);
        } catch (Exception e) {
            android.util.Log.i("SQLiteContentHelper", "SQLiteCursor.java: " + e);
        }       
        AssetFileDescriptor afd = new AssetFileDescriptor(fd, 0, file.length());
        return afd;
    } catch (IOException ex) {
        throw new FileNotFoundException(ex.toString());
    }
}
项目:silent-contacts-android    文件:DbQueryUtils.java   
/**
     * 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();
        }
    }
项目:greendao-cipher    文件:SQLiteContentHelper.java   
/**
    * 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 {
    android.os.ParcelFileDescriptor fd = null;

    try {
        MemoryFile file = simpleQueryForBlobMemoryFile(db, sql, selectionArgs);
        if (file == null) {
            throw new FileNotFoundException("No results.");
        }
        Class c = file.getClass();
        try {
            java.lang.reflect.Method m = c.getDeclaredMethod("getParcelFileDescriptor");
            m.setAccessible(true);
            fd = (android.os.ParcelFileDescriptor)m.invoke(file);
        } catch (Exception e) {
            android.util.Log.i("SQLiteContentHelper", "SQLiteCursor.java: " + e);
        }       
        AssetFileDescriptor afd = new AssetFileDescriptor(fd, 0, file.length());
        return afd;
    } catch (IOException ex) {
        throw new FileNotFoundException(ex.toString());
    }
}
项目:greendao-cipher    文件:SQLiteContentHelper.java   
/**
 * 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();
    }
}
项目:GitHub    文件:WebpDecodingTest.java   
private MemoryFile getMemoryFile(String path) {
  try {
    byte[] data = ByteStreams.toByteArray(getTestImageInputStream(path));
    MemoryFile memoryFile = new MemoryFile(null, data.length);
    memoryFile.allowPurging(false);
    memoryFile.writeBytes(data, 0, 0, data.length);
    return memoryFile;
  } catch (IOException e) {
    throw Throwables.propagate(e);
  }
}
项目:GitHub    文件:WebpDecodingTest.java   
private synchronized Method getFileDescriptorMethod() {
  if (sGetFileDescriptorMethod == null) {
    try {
      sGetFileDescriptorMethod = MemoryFile.class.getDeclaredMethod("getFileDescriptor");
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }
  return sGetFileDescriptorMethod;
}
项目:GitHub    文件:WebpDecodingTest.java   
private FileDescriptor getMemoryFileDescriptor(MemoryFile memoryFile) {
  try {
    Object rawFD = getFileDescriptorMethod().invoke(memoryFile);
    return (FileDescriptor) rawFD;
  } catch (Exception e) {
    throw Throwables.propagate(e);
  }
}
项目:GitHub    文件:WebpDecodingTest.java   
@Test
public void test_webp_extended_decoding_filedescriptor_bitmap() throws Throwable {
  final MemoryFile memoryFile =  getMemoryFile("webp_e.webp");
  final Bitmap bitmap = mWebpBitmapFactory.decodeFileDescriptor(
      getMemoryFileDescriptor(memoryFile),
      null,
      null);
  memoryFile.close();
  assertBitmap(bitmap, 480, 320);
}
项目:GitHub    文件:WebpDecodingTest.java   
@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);
}
项目:GitHub    文件:WebpDecodingTest.java   
@Test
public void test_webp_lossless_decoding_filedescriptor_bitmap() throws Throwable {
  final MemoryFile memoryFile =  getMemoryFile("webp_ll.webp");
  final Bitmap bitmap = mWebpBitmapFactory.decodeFileDescriptor(
      getMemoryFileDescriptor(memoryFile),
      null,
      null);
  memoryFile.close();
  assertBitmap(bitmap, 400, 301);
}
项目:GitHub    文件:WebpDecodingTest.java   
@Test
public void test_webp_plain_decoding_filedescriptor_bitmap() throws Throwable {
  final MemoryFile memoryFile =  getMemoryFile("webp_plain.webp");
  final Bitmap bitmap = mWebpBitmapFactory.decodeFileDescriptor(
      getMemoryFileDescriptor(memoryFile),
      null,
      null);
  memoryFile.close();
  assertBitmap(bitmap, 320, 214);
}
项目:GitHub    文件:GingerbreadPurgeableDecoder.java   
private synchronized Method getFileDescriptorMethod() {
  if (sGetFileDescriptorMethod == null) {
    try {
      sGetFileDescriptorMethod = MemoryFile.class.getDeclaredMethod("getFileDescriptor");
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }
  return sGetFileDescriptorMethod;
}
项目:GitHub    文件:GingerbreadPurgeableDecoder.java   
private FileDescriptor getMemoryFileDescriptor(MemoryFile memoryFile) {
  try {
    Object rawFD = getFileDescriptorMethod().invoke(memoryFile);
    return (FileDescriptor) rawFD;
  } catch (Exception e) {
    throw Throwables.propagate(e);
  }
}
项目:FontProvider    文件:MemoryFileUtils.java   
public static FileDescriptor getFileDescriptor(MemoryFile mf) {
    if (getFileDescriptorMethod != null) {
        try {
            return  (FileDescriptor) getFileDescriptorMethod.invoke(mf);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    return null;
}
项目:Cable-Android    文件:PartProvider.java   
private ParcelFileDescriptor getParcelStreamForAttachment(MasterSecret masterSecret, AttachmentId attachmentId) throws IOException {
  long       plaintextLength = Util.getStreamLength(DatabaseFactory.getAttachmentDatabase(getContext()).getAttachmentStream(masterSecret, attachmentId));
  MemoryFile memoryFile      = new MemoryFile(attachmentId.toString(), Util.toIntExact(plaintextLength));

  InputStream  in  = DatabaseFactory.getAttachmentDatabase(getContext()).getAttachmentStream(masterSecret, attachmentId);
  OutputStream out = memoryFile.getOutputStream();

  Util.copy(in, out);
  Util.close(out);
  Util.close(in);

  return MemoryFileUtil.getParcelFileDescriptor(memoryFile);
}
项目:fresco    文件:WebpDecodingTest.java   
private MemoryFile getMemoryFile(String path) {
  try {
    byte[] data = ByteStreams.toByteArray(getTestImageInputStream(path));
    MemoryFile memoryFile = new MemoryFile(null, data.length);
    memoryFile.allowPurging(false);
    memoryFile.writeBytes(data, 0, 0, data.length);
    return memoryFile;
  } catch (IOException e) {
    throw Throwables.propagate(e);
  }
}
项目:fresco    文件:WebpDecodingTest.java   
private synchronized Method getFileDescriptorMethod() {
  if (sGetFileDescriptorMethod == null) {
    try {
      sGetFileDescriptorMethod = MemoryFile.class.getDeclaredMethod("getFileDescriptor");
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }
  return sGetFileDescriptorMethod;
}
项目:fresco    文件:WebpDecodingTest.java   
private FileDescriptor getMemoryFileDescriptor(MemoryFile memoryFile) {
  try {
    Object rawFD = getFileDescriptorMethod().invoke(memoryFile);
    return (FileDescriptor) rawFD;
  } catch (Exception e) {
    throw Throwables.propagate(e);
  }
}
项目:fresco    文件:WebpDecodingTest.java   
@Test
public void test_webp_extended_decoding_filedescriptor_bitmap() throws Throwable {
  final MemoryFile memoryFile =  getMemoryFile("webp_e.webp");
  final Bitmap bitmap = mWebpBitmapFactory.decodeFileDescriptor(
      getMemoryFileDescriptor(memoryFile),
      null,
      null);
  memoryFile.close();
  assertBitmap(bitmap, 480, 320);
}
项目:fresco    文件:WebpDecodingTest.java   
@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);
}
项目:fresco    文件:WebpDecodingTest.java   
@Test
public void test_webp_lossless_decoding_filedescriptor_bitmap() throws Throwable {
  final MemoryFile memoryFile =  getMemoryFile("webp_ll.webp");
  final Bitmap bitmap = mWebpBitmapFactory.decodeFileDescriptor(
      getMemoryFileDescriptor(memoryFile),
      null,
      null);
  memoryFile.close();
  assertBitmap(bitmap, 400, 301);
}
项目:fresco    文件:WebpDecodingTest.java   
@Test
public void test_webp_plain_decoding_filedescriptor_bitmap() throws Throwable {
  final MemoryFile memoryFile =  getMemoryFile("webp_plain.webp");
  final Bitmap bitmap = mWebpBitmapFactory.decodeFileDescriptor(
      getMemoryFileDescriptor(memoryFile),
      null,
      null);
  memoryFile.close();
  assertBitmap(bitmap, 320, 214);
}
项目:fresco    文件:GingerbreadPurgeableDecoder.java   
private synchronized Method getFileDescriptorMethod() {
  if (sGetFileDescriptorMethod == null) {
    try {
      sGetFileDescriptorMethod = MemoryFile.class.getDeclaredMethod("getFileDescriptor");
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }
  return sGetFileDescriptorMethod;
}
项目:fresco    文件:GingerbreadPurgeableDecoder.java   
private FileDescriptor getMemoryFileDescriptor(MemoryFile memoryFile) {
  try {
    Object rawFD = getFileDescriptorMethod().invoke(memoryFile);
    return (FileDescriptor) rawFD;
  } catch (Exception e) {
    throw Throwables.propagate(e);
  }
}
项目:FontProvider    文件:FontManager.java   
public static FileDescriptor getFileDescriptor(Context context, String filename) {
    MemoryFile mf = sCache.get(filename);

    if (mf != null) {
        Log.i(TAG, "MemoryFile " + filename + " is in the cache");

        FileDescriptor fd = MemoryFileUtils.getFileDescriptor(mf);
        if (fd != null && fd.valid()) {
            return fd;
        } else {
            Log.i(TAG, "MemoryFile " + filename + " is not valid?");
        }
    }

    long time = System.currentTimeMillis();

    Log.i(TAG, "loading file " + filename);

    // built in font? read from asset
    if (BUILT_IN_FONTS_SIZE.containsKey(filename)) {
        mf = MemoryFileUtils.fromAsset(context.getAssets(), filename, FILE_SIZE.get(filename));
    }

    // downloadable font? read from file
    if (mf == null) {
        File file = ContextUtils.getExternalFile(context, filename);
        if (file.exists()) {
            mf = MemoryFileUtils.fromFile(file);
            if (mf != null) {
                FILE_SIZE.put(filename, mf.length());
            }
        }
    }

    // file not exist?
    if (mf == null) {
        Log.w(TAG, "loading " + filename + " failed");
        return null;
    }

    Log.i(TAG, "loading finished in " + (System.currentTimeMillis() - time) + "ms");
    sCache.put(filename, mf);

    return MemoryFileUtils.getFileDescriptor(mf);
}
项目:ebook    文件:WordParseUtils.java   
public static MemoryFile getMemoryFile()
{
    return  mMemoryFile;
}
项目:LiveMultimedia    文件:SharedVideoMemory.java   
/**
 * Allocates a new ashmem region. The region is initially not purgable.
 *
 * @param name      optional name for the file (can be null).
 * @param frameSize how many frame to store
 * @param length    of the memory file in bytes.
 * @throws IOException if the memory file could not be created.
 */
public SharedVideoMemory(String name, int frameSize, int length) throws IOException {
    mSharedMemFile = new MemoryFile(name, length);
    mFrameSize = frameSize;
}