我有这个加密视频文件的代码。
public static void encryptVideos(File fil,File outfile)
{
try{
FileInputStream fis = new FileInputStream(fil);
//File outfile = new File(fil2);
int read;
if(!outfile.exists())
outfile.createNewFile();
FileOutputStream fos = new FileOutputStream(outfile);
FileInputStream encfis = new FileInputStream(outfile);
Cipher encipher = Cipher.getInstance("AES");
KeyGenerator kgen = KeyGenerator.getInstance("AES");
//byte key[] = {0x00,0x32,0x22,0x11,0x00,0x00,0x00,0x00,0x00,0x23,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
SecretKey skey = kgen.generateKey();
//Lgo
encipher.init(Cipher.ENCRYPT_MODE, skey);
CipherInputStream cis = new CipherInputStream(fis, encipher);
while((read = cis.read())!=-1)
{
fos.write(read);
fos.flush();
}
fos.close();
}catch (Exception e) {
// TODO: handle exception
}
}但是我使用的文件非常大,使用这种方法需要太多时间。我怎么才能加快速度呢?
发布于 2012-02-29 19:08:42
嗯,这看起来非常慢:
while((read = cis.read())!=-1)
{
fos.write(read);
fos.flush();
}您一次只读写一个字节,然后刷新流。一次做一个缓冲区:
byte[] buffer = new byte[8192]; // Or whatever
int bytesRead;
while ((bytesRead = cis.read(buffer)) != -1)
{
fos.write(buffer, 0, bytesRead);
}
fos.flush(); // Not strictly necessary, but can avoid close() masking issues还要注意,您只关闭了fos (而不是cis或fis),并且应该在finally块中关闭所有它们。
发布于 2012-02-29 19:15:13
您可以使用android NDK来编写应用程序的这一部分与C++,以获得显著的性能提升。这看起来是一种可以从中受益的情况。而且NDK可能已经有类似的东西了。
发布于 2015-07-12 17:55:21
你应该试试Facebook的隐藏功能。它的速度令人难以置信!
https://stackoverflow.com/questions/9497876
复制相似问题