0

I have following piece of code :

    File file = new File("//path/to/BatchFile");
    long fileLength = 0 ;
    if(file.exists()){
      fileLength = file.length();
    }

    Process process = Runtime.getRuntime().exec("cmd /c " + "//path/to/batchfile");
    InputStream inputStream = process.getInputStream();
    BufferedReader  br = new BufferedReader(new InputStreamReader(inputStream));

    String s;
    long bytesRead=0;

    while (( s = br.readLine()) != null) {
            bytesRead = bytesRead + s.getBytes().length + 2; // forget about +2
            int progress = (int) (( bytesRead/  fileLenth)*100 );
            System.out.println("Progress : "+ progress);

    }

What I am trying to do is running a batch file and converting it to BufferedReader to calculate the percentage for progress bar. But my percentage goes beyond 100.

Help me correcting what i am doing wrong or let me know how to convert the 'process' part into percentage.

1 Answers1

1

How to get the size of an InputStream?

An InputStream inherently doesn't have a size. It could conceivably keep delivering bytes forever. Or the producing end could end the stream without warning.

If you must find out the length, then you have to read to the end, counting the bytes, and report the length when you finish.

I assume that the batch file returns a result and you do not want to have the size of the batch file.

Because the batch returns a result as stream you do not know the stream size. If the batch writes the result into another file, you can start reading the result-file by using the file length.


UPDATE
You pointed the the batch logic. The Batch checks out SVN projects and you want to have the number of checked out elements. This is a different request!

Its more like another SVN request you have to use. You can first ask for the SVN children of a specific folder. Afterwards you have to read the checkout-stream result and analyze the content.

You have to search for key-words defining, that a checkout command finsihed(successful and not successful). This will increase the progress counter and not the stream bytes.

Community
  • 1
  • 1
Markus Lausberg
  • 12,177
  • 6
  • 40
  • 66