package magnus4j.engine;

import magnus4j.engine.uci.AbstractUCIEngine;
import magnus4j.engine.uci.UCIEngineException;
import magnus4j.utils.OSUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Stockfish internal engine.
 */
public class StockfishEngine extends AbstractUCIEngine {

    private String _engineCommand;

    @Override
    public String getEngineName() {
        return "Stockfish 7";
    }

    @Override
    public String getEngineCommand() {

        if (_engineCommand != null)
            return _engineCommand;

        String stockfishResource;

        if (OSUtils.isWindows()) {
            stockfishResource = "/engines/windows/stockfish_7_x64.exe";
        } else if (OSUtils.isLinux()) {
            stockfishResource = "/engines/linux/stockfish_7_x64";
        } else if (OSUtils.isMac()) {
            stockfishResource = "/engines/mac/stockfish-7-64";
        } else {
            throw new RuntimeException("Unsupported operating system.");
        }

        try {
            InputStream in = getClass().getResourceAsStream(stockfishResource);
            File tmpStockfish = File.createTempFile("stockfish-6-", ".exe");
            OutputStream out = new FileOutputStream(tmpStockfish);

            byte[] buffer = new byte[4096];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            out.flush();
            out.close();

            tmpStockfish.setExecutable(true, false);

            _engineCommand = tmpStockfish.getAbsolutePath();
            return _engineCommand;

        } catch (IOException e) {
            throw new UCIEngineException(e);
        }
    }
}

@@ -621,32 +621,672 @@
 engineCommand;%0A%0A
+        try %7B%0A            InputStream in = getStockfishResource();%0A            File localStockfish = getTempStockfish();%0A%0A            try (OutputStream out = new FileOutputStream(localStockfish)) %7B%0A                byte%5B%5D buf = new byte%5B8192%5D;%0A                int len;%0A                while ((len = in.read(buf)) %3E 0) %7B%0A                    out.write(buf, 0, len);%0A                %7D%0A            %7D%0A%0A            _engineCommand = localStockfish.getAbsolutePath();%0A            return _engineCommand;%0A%0A        %7D catch (IOException e) %7B%0A            throw new UCIEngineException(e);%0A        %7D%0A    %7D%0A%0A    private InputStream getStockfishResource() %7B%0A
         String s
@@ -1718,25 +1718,24 @@
       %7D%0A
-%0A
         
 try %7B%0A  
@@ -1730,42 +1730,14 @@
     
-try %7B%0A            InputStream in =
+return
  get
@@ -1792,22 +1792,33 @@
     
+%7D%0A%0A
     
-   
+private
  File 
-t
+getTe
 mpSt
@@ -1828,356 +1828,103 @@
 fish
- = File.createTempFile(%22stockfish-6-%22, %22.exe%22);%0A            OutputStream out = new FileOutputStream(tmpStockfish);%0A%0A            byte%5B%5D buffer = new byte%5B4096%5D;%0A            int read;%0A            while ((read = in.read(buffer)) != -1) %7B%0A                out.write(buffer, 0, read);%0A            %7D%0A            out.flush();%0A            out.close();%0A%0A    
+() throws IOException %7B%0A        File tmpStockfish = File.createTempFile(%22stockfish-%22, %22.exe%22);%0A
     
@@ -1968,26 +1968,24 @@
 false);%0A
-%0A 
         
    _engi
@@ -1980,56 +1980,33 @@
     
-   _engineCommand = tmpStockfish.getAbsolutePath
+tmpStockfish.deleteOnExit
 ();%0A
@@ -2017,124 +2017,28 @@
     
-    
 return 
-_engineCommand;%0A%0A        %7D catch (IOException e) %7B%0A            throw new UCIEngineException(e);%0A        %7D
+tmpStockfish;
 %0A   

Delete on exit temp stockfish engine