package jitb.net;

import flash.events.Event;
import flash.events.EventDispatcher;
import flash.utils.ByteArray;
import jitb.events.EventSystem;
import jitb.util.PathUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author Joa Ebert
 */
public final class ContinuousFileLoader extends EventDispatcher {
	private final String _file;
	private boolean _running = false;
	private ByteArray _data;
	
	public ContinuousFileLoader(final String file) {
		_file = PathUtil.createPath(file);
	}

	public ByteArray data() {
		return _data;
	}
	
	public void start() {
		final ContinuousFileLoader continuousFileLoader = this;
		_running = true;
		EventSystem.execute(new Runnable() {
			@Override
			public void run() {
				final File file = new File(continuousFileLoader._file);
				long lastModified = 0L;

				try {
					while(continuousFileLoader._running && !Thread.interrupted()) {
						if(file.lastModified() > lastModified) {
							lastModified = file.lastModified();

							FileInputStream fis = null;
							FileChannel fc = null;

							try {
								fis = new FileInputStream(file);
								fc = fis.getChannel();
								final int fs = (int)fc.size();
								final ByteBuffer byteBuffer = ByteBuffer.allocateDirect(fs);
								MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fs);
								bb.load();
								byteBuffer.put(bb);
								bb = null;
								byteBuffer.flip();
								fc.close();
								continuousFileLoader._data = ByteArray.JITB$fromBuffer(byteBuffer);
								EventSystem.delayedDispatch(continuousFileLoader, new Event(Event.COMPLETE));
							} catch(FileNotFoundException e) {
								/* ignored */
							} catch(IOException e) {
								/* ignored */
							} finally {
								if(null != fc) {
									try { fc.close(); } catch(Throwable t) { /*nada*/ } 
								}
								if(null != fis) {
									try { fis.close(); } catch(Throwable t) { /*nada*/ }
								}
							}
						}

						Thread.sleep(500L);
					}
				} catch(InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
	}

	public void stop() {
		_running = false;
	}
}

@@ -891,22 +891,16 @@
 ) %7B%0A%09%09%09%09
-final 
 File fil
@@ -908,43 +908,11 @@
  = n
-ew File(continuousFileLoader._file)
+ull
 ;%0A%09%09
@@ -1013,24 +1013,76 @@
 rupted()) %7B%0A
+%09%09%09%09%09%09file = new File(continuousFileLoader._file);%0A%0A
 %09%09%09%09%09%09if(fil
@@ -1903,24 +1903,54 @@
 %7D finally %7B%0A
+%09%09%09%09%09%09%09%09file = null;%0A%09%09%09%09%09%09%09%09%0A
 %09%09%09%09%09%09%09%09if(n

test for david l.