java - Ensuring right data is read from an InputStream -


i have root application supposed capture screen @ point during execution. in order accomplish this, interact android shell using following code:

private static process su = runtime.getruntime().exec("su"); private static dataoutputstream outputstream = new dataoutputstream(su.getoutputstream()); private static datainputstream inputstream = new datainputstream(su.getinputstream());  private void capturescreen() {     outputstream.writebytes("/system/bin/screencap -p\n");     outputstream.flush();     bitmap bitmap = bitmapfactory.decodestream(inputstream);     //outputstream.writebytes("echo test\n");     //outputstream.flush(); } 

it works fine, when call multiple times, moment issue dummy command produces shell output between capturescreen calls, bitmapfactory.decodestream fails. considering this, have few questions:

  • i assume happens because data within inputstream no longer purely related image data. since runtime single instance (as referred here), again assume other processes can introduce outputs within inputstream on live system. how can make sure data need inputstream?
  • why capturescreen work correctly when called multiple times? how bitmapfactory.decodestream manage last image inputstream? "consume" relevant data when succeeds? search last image data inputstream? if so, why fail when there irrelevant data before image data within inputstream?

i know can around problem writing image file read there, avoid i/o operations in favor of performance.

after toying around while, found answer own questions:

  • although runtime single instance, executing code on starts new process on own. input , output streams related process can written/read process (unless process redirects streams process, extremely unlikely in case).
  • contents of input stream indeed "consumed" upon reading, calling function multiple times writes/reads image data wholly. "polluting" input stream of process data not related image breaks decodestream's functionality.

also keep in mind "su" isn't command ends. not terminate until called so. here's revised class use in code:

public class bitmapscreencap {     public final static bitmapscreencap = new bitmapscreencap();     private bitmapscreencap() { }     public bitmap screen() {         try {             process process = runtime.getruntime().exec("su");             outputstreamwriter outputstream = new outputstreamwriter(process.getoutputstream());             outputstream.write("/system/bin/screencap -p\n");             outputstream.flush();             bitmap screen = bitmapfactory.decodestream(process.getinputstream());             outputstream.write("exit\n");             outputstream.flush();             outputstream.close();             return screen;         } catch (ioexception e) {             e.printstacktrace();         }         return null;     } } 

and can called anywhere within project as:

bitmapscreencap.get.screen(); 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -