package com.github.to2mbn.jmccc.exec;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Arrays;
import java.util.Collection;
import com.github.to2mbn.jmccc.util.OsTypes;

public class LoggingMonitor extends ProcessMonitor {

    private class LogMonitor implements Runnable {

        /**
         * False for stdout, true for stderr
         */
        private boolean isErr;
        private InputStream in;

        public LogMonitor(boolean isErr, InputStream in) {
            this.isErr = isErr;
            this.in = in;
        }

        @Override
        public void run() {
            char[] eol = OsTypes.getLineSpearator().toCharArray();
            try {
                // no need for close this
                // because we don't need to close the base stream
                Reader reader = new InputStreamReader(in);

                StringBuilder buffer = new StringBuilder();
                int ch;
                while ((ch = reader.read()) != -1) {
                    buffer.append((char) ch);

                    // check eol
                    boolean isEOL = true;
                    for (int i = 0; i < eol.length; i++) {
                        if (eol[i] != buffer.charAt(buffer.length() - 1 - i)) {
                            isEOL = false;
                            break;
                        }
                    }

                    if (isEOL) {
                        buffer.delete(buffer.length() - eol.length, buffer.length());
                        String log = buffer.toString();
                        buffer.delete(0, buffer.length());
                        if (isErr) {
                            listener.onErrorLog(log);
                        } else {
                            listener.onLog(log);
                        }
                    }

                    if (Thread.interrupted()) {
                        return;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    private class ExitMonitor implements Runnable {

        @Override
        public void run() {
            try {
                process.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
                return;
            }

            int exitCode = process.exitValue();
            listener.onExit(exitCode);
        }

    }

    private GameProcessListener listener;

    public LoggingMonitor(Process process, GameProcessListener listener) {
        super(process);
        this.listener = listener;
    }

    public GameProcessListener getListener() {
        return listener;
    }

    @Override
    protected Collection<? extends Runnable> createMonitors() {
        return Arrays.asList(new LogMonitor(false, process.getInputStream()), new LogMonitor(true, process.getErrorStream()), new ExitMonitor());
    }

}

@@ -1091,24 +1091,85 @@
 char) ch);%0A%0A
+                    if (buffer.length() %3E= eol.length) %7B%0A    
             
@@ -1209,16 +1209,20 @@
         
+ 
+   
 boolean 
@@ -1235,16 +1235,20 @@
 = true;%0A
+    
         
@@ -1318,24 +1318,28 @@
             
+   
+ 
 if (eol%5Bi%5D !
@@ -1376,18 +1376,31 @@
 ) - 
-1 -
+eol.length +
  i)) %7B%0A
+    
     
@@ -1470,15 +1470,23 @@
     
+   
+ 
 break;%0A
+    
     
@@ -1519,32 +1519,36 @@
                 
+    
 %7D%0A%0A             
@@ -1550,24 +1550,28 @@
             
+ 
+   
 if (isEOL) %7B
@@ -1567,24 +1567,28 @@
 f (isEOL) %7B%0A
+    
             
@@ -1681,24 +1681,28 @@
             
+   
+ 
 String log =
@@ -1717,24 +1717,28 @@
 toString();%0A
+    
             
@@ -1804,24 +1804,28 @@
             
+    
 if (isErr) %7B
@@ -1845,32 +1845,36 @@
                 
+ 
+   
 listener.onError
@@ -1907,16 +1907,20 @@
         
+    
 %7D else %7B
@@ -1920,16 +1920,20 @@
  else %7B%0A
+    
         
@@ -1965,32 +1965,62 @@
 ner.onLog(log);%0A
+                            %7D%0A
                 

fix line spliting invalid on windows