java - Decrypting files via Runtime.getRuntime().exec without Thread.sleep() -


my code decrypt encrypted files in folder, used thread.sleep(). if dont use thread.sleep(); program can't decrypt files. i'm looking better way how decrypt files without thread.sleep();

i need write conditions in code, code waiting until file decrypted. me?

my code:

public static void decrypt() throws interruptedexception, ioexception {     file folder = new file("c:/users/hajdukri/desktop/src");     file[] files = folder.listfiles();      string pass;     scanner in = new scanner(system.in);     system.out.println("enter password");     pass = in.nextline();      if("pass123456".equals(pass)){         for(file :files){             thread.sleep(500);             runtime.getruntime().exec("cmd /c start c:\\users\\hajdukri\\documents\\netbeansprojects\\javaapplication3\\gpg.bat");              if (a.isfile()) {                 string filename = a.getname();                 // only last index (exeption)                 int co = filename.lastindexof('.')+1;                 stringbuilder sb = new stringbuilder(filename);                  sb.delete(0, co);                  sb.tostring();                  if ("gpg".equals(sb.tostring())){                     int last = filename.lastindexof('.');                     stringbuilder dellast = new stringbuilder(filename);                      dellast.delete(last, 4+last);                      dellast.tostring();                      file file = new file("c:\\users\\hajdukri\\documents\\netbeansprojects\\javaapplication3\\gpg.bat");                     string content = "cd c:\\users\\hajdukri\\desktop\\src \r\ngpg --output "+ dellast +" --batch --passphrase "+"\""+pass+"\""+  " "+" --decrypt "+ filename +"\r\nexit b/";                     filewriter fw = new filewriter(file.getabsolutefile());                     try (bufferedwriter bw = new bufferedwriter(fw)) {                         bw.write(content);                         bw.close();                     }                    }               }             }     }     else{     system.out.println("invalid password entered!");}  } 

i need run runtime.getruntime().exec (gpg.bat) next files when previous file decripted.

thank lot.

the runtime.exec() returns process object.

you must read output , error streams. if don't, small os buffer fill , process (cmd.exe) hang until do.

and since both streams blocking, need @ least 2 threads (typically 1 thread error stream while current thread read process output). solid control, put 2 threads on streams while current thread waits process, timeout, , kills if required.

if don't have outputs, might away without reading err/out streams, won't see issues. yet must still use process.waitfor() unless intend have limited number running concurrently.

also, child process consuming few system descriptors. cannot spawn thousands without concerns...

other consideration: hotspot runtime.exec very bad @ spawning child process when jvm heap high. let's have 10 gb jvm, spawning child exec fork require 10gb virtual memory. so, in such env, doing more 1 exec gets tricky. (i had use tiny exec daemon has no issued forking processes communicate through socket).


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -