title: how to send a command at runtime using "su" and get all lines back from DataInputStream
body: <p>I am working on a program that needs to be able to determine what is on the android device at Xlocation. I am using "su ls Xlocation"</p>
  
  <p>I want to get back and array list of files but only manage to get back the first Item. Am I missing a command that gets the next line? Or is there something else I need to do.</p>
  
  <p>Below is my command I am sending</p>
  
  <pre><code>String[] commands = new String[]{"ls /system/app/"};
  return doCommand(commands);
  </code></pre>
  
  <p>Below is my current method for doCommand</p>
  
  <pre><code>    private boolean doCommand(String[] commands)
      {
      ArrayList&lt;String&gt; output = new ArrayList&lt;String&gt;();
  
      boolean ran = false;
      try
      {
          Process process = Runtime.getRuntime().exec("su");
          DataOutputStream os = new DataOutputStream(process.getOutputStream());
          DataInputStream ins = new DataInputStream(process.getInputStream());
  
          // This part works I sends the command right!
          for (String single : commands) 
          {
              os.writeBytes(single + "\n");
              os.flush();
              os.writeBytes("exit\n");
              os.flush();
              process.waitFor();
              ran = true;
          }
  
          int av = -1;
          while (av != 0)
          {
  
  //////////////////////////// WORKING ON THIS TO GET ALL INFO /////////////////
              av = ins.available();
              if (av != 0) 
              {
                  byte[] b = new byte[av];
                  ins.read(b);
                  output.add(new String(b));
                  System.out.println(new String(b) + "Recieved form modem");
              }
          }
      }
      catch(Exception ex){}
      return ran;
  }
  </code></pre>
  
  <p>As seen currently it only returns true or false. However run in debug I only get the first item in output. (output = "[first.apk")</p>
  
  <p><strong>Edited Newer Version;</strong></p>
  
  <pre><code>    public ArrayList&lt;String&gt; doCommand(String[] commands)
  {
  
      ArrayList&lt;String&gt; output = new ArrayList&lt;String&gt;();
  
      try
      {
  
          Process process = Runtime.getRuntime().exec("su");
          DataOutputStream os = new DataOutputStream(process.getOutputStream());  
          DataInputStream ins = new DataInputStream(process.getInputStream());
          for (String single : commands) 
          {
              os.writeBytes(single + "\n");
  
              //os.flush();
              output.add("true");
          }
          os.writeBytes("exit\n");
  
  
          byte[] bc = new byte[10000];
          String st = "";
          while(ins.read(bc) != -1)
          {
          st += new String(bc);
          os.flush();
  
          }
          output.add(st);
  
  
          os.flush();
          os.close();
          ins.close();
          process.waitFor();          
      }
      catch(Exception ex)
      {}
  
      return output;
  }
  </code></pre>
  
  <p>Now getting a decent amount of output but still not all where the directory has large items inside size limit of byte[10000] I have checked.</p>
  
  <p>If anyone wants to improve on this and get an exact answer that works do so I still check this post.</p>
  
  <p>Thanks</p>