
我有这个代码用于加密视频文件.
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 }}但我使用的文件非常大,使用这种方法需要花费太多时间.
我怎样才能加快速度呢?
解决方法:
这开始看起来很慢:
while((read = cis.read())!=-1){ fos.write(read); fos.flush();}您正在一次读取和写入一个字节并刷新流.一次做一个缓冲区:
byte[] buffer = new byte[8192]; // Or whateverint 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块中关闭所有这些.
总结以上是内存溢出为你收集整理的java – 加速加密?全部内容,希望文章能够帮你解决java – 加速加密?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)