package at.jku.pervasive.ecg;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.bluetooth.RemoteDevice;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;

import org.apache.commons.io.IOUtils;

public class FileHeartManMock extends HeartManMock {

  private final File dataFile;
  private boolean isRunning;

  public FileHeartManMock(File dataFile) {
    super();
    this.dataFile = dataFile;
  }

  @Override
  public void run() {
    InputStream in = null;
    OutputStream dos = null;
    try {
      StreamConnectionNotifier service = null;

      String url = "btspp://localhost:"
          + HeartManDiscovery.HEARTMAN_SERVICE_UUID
          + ";authenticate=false;encrypt=false";

      service = (StreamConnectionNotifier) Connector.open(url);
      System.out.println("Waiting for incoming connection...");
      StreamConnection connection = service.acceptAndOpen();

      RemoteDevice client = RemoteDevice.getRemoteDevice(connection);
      String clientFriendlyName = client.getFriendlyName(true);
      System.out.println("connected to " + clientFriendlyName);

      dos = connection.openOutputStream();

      in = new BufferedInputStream(new FileInputStream(dataFile));

      byte[] buffer = new byte[2];
      int len = -1;
      while ((len = in.read(buffer)) != -1) {
        dos.write(buffer, 0, len);
        dos.flush();
      }

      dos.close();
      connection.close();
    } catch (IOException io) {
      // silent
      System.err.println(io.getMessage());
    } catch (Exception e) {
      System.out.println("uweeh");
      e.printStackTrace();
    } finally {
      IOUtils.closeQuietly(in);
      IOUtils.closeQuietly(dos);
    }
  }

  @Override
  public void stop() {
    this.isRunning = false;
  }

}

@@ -506,37 +506,8 @@
 ile;
-%0A  private boolean isRunning;
 %0A%0A  
@@ -1873,74 +1873,6 @@
  %7D%0A%0A
-  @Override%0A  public void stop() %7B%0A    this.isRunning = false;%0A  %7D%0A%0A
 %7D%0A

remove unused stuff