I am trying to execute commands using Java. So when I try to convert the process Inputstream to string using BufferedReader, the code works if I call process.waitFor() after processing the inputstream to string. But when I try to convert the process input stream using ByteArrayOutputStream to string, the results are not returned if I write process.waitFor() after processing the inputstream to string. It works only process.waitFor is written before inputstream.isavailable(). I don't understand why this is behaving like this? Also I want to know how to determing the buffer array size incase of using ByteArrayStream. I am trying to use isavailable() to know to number of bytes.
``ProcessBuilder pb = new ProcessBuilder();
    String cmd = "ls -l /Users/uma/data";
    pb.command("bash", "-c",cmd);
    
    try {
        Process process = pb.start();
        StringBuilder output = new StringBuilder();
        
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            output.append(line + "\n");
        }
        intexitVal = process.waitFor();
        if (exitVal == 0) {
            System.out.println("Success!");
            System.out.println(output);
            System.exit(0);
        } else {
              try (final BufferedReader b = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
                    String errorline;
                    if ((errorline = b.readLine()) != null)
                        System.out.println(errorline);
                } catch (final IOException e) {
                    e.printStackTrace();
                }   
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    `
``ProcessBuilder pb = new ProcessBuilder();
        
        String cmd = "ls -l /Users/uma/data";
        pb.command("bash", "-c",cmd);
        
        try {
            Process process = pb.start();
            int exitVal = process.waitFor();
            InputStream is = process.getInputStream();
             ByteArrayOutputStream result = new ByteArrayOutputStream();
                byte[] buffer = newbyte[is.available()];
                int length;
                while ((length = is.read(buffer)) != -1) {
                    result.write(buffer, 0, length);
                }
                String output = result.toString();
            if (exitVal == 0) {
                System.out.println("Success!");
                System.out.println(output);
                System.exit(0);
            } else {
                  try (final BufferedReader b = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
                        String errorline;
                        if ((errorline = b.readLine()) != null)
                            System.out.println(errorline);
                    } catch (final IOException e) {
                        e.printStackTrace();
                    }   
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }`
How to determine the buffer size? Also when should I call waitFor()?
 
    