package com.openxc.remote.sources;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;

import java.net.MalformedURLException;
import java.net.URI;

import android.util.Log;

public class TraceVehicleDataSource extends JsonVehicleDataSource {
    private static final String TAG = "TraceVehicleDataSource";

    private boolean mRunning;
    private URI mFilename;
    private InputStream mStream;

    public TraceVehicleDataSource(
            VehicleDataSourceCallbackInterface callback) {
        super(callback);
        mRunning = false;
    }

    public TraceVehicleDataSource(
            VehicleDataSourceCallbackInterface callback,
            URI filename) throws VehicleDataSourceException {
        this(callback);
        mFilename = filename;

        if(mFilename == null) {
            throw new VehicleDataSourceException(
                    "No filename specified for the trace source");
        }
    }

    public TraceVehicleDataSource(
            VehicleDataSourceCallbackInterface callback,
            InputStream stream) throws VehicleDataSourceException {
        this(callback);
        mStream = stream;

        if(mStream == null) {
            throw new VehicleDataSourceException(
                    "No input stream specified for the trace source");
        }
    }

    public void trigger(String name, double value) {
        handleMessage(name, value);
    }

    public void trigger(String name, String value) {
        handleMessage(name, value);
    }

    public void stop() {
        mRunning = false;
    }

    public void run() {
        if(mCallback != null) {
            mRunning = true;
        }

        InputStream stream = null;
        if(mFilename != null) {
            try {
                stream = new FileInputStream(mFilename.toURL().getFile());
            } catch(FileNotFoundException e) {
                Log.w(TAG, "Couldn't open the trace file " + mFilename, e);
                return;
            } catch(MalformedURLException e) {
                Log.w(TAG, "Couldn't open the trace file " + mFilename, e);
                return;
            }
        } else {
            stream = mStream;
        }

        while(mRunning) {
            BufferedReader reader = openFile(stream);

            String line;
            try {
                while((line = reader.readLine()) != null) {
                    parseJson(line);
                }
            } catch(IOException e) {
                Log.w(TAG, "An exception occured when reading the trace file " +
                        mFilename, e);
                try {
                    stream.close();
                } catch(IOException ee) {
                    Log.w(TAG, "Couldn't even close the trace file", ee);
                }
                return;
            }
        }

        try {
            if(stream != null) {
                stream.close();
            }
        } catch(IOException e) {
            Log.w(TAG, "Couldn't even close the trace file", e);
        }
    }

    private static BufferedReader openFile(InputStream stream) {
        DataInputStream dataStream = new DataInputStream(stream);
        return new BufferedReader(new InputStreamReader(dataStream));
    }
}

@@ -1740,24 +1740,56 @@
 oid run() %7B%0A
+        BufferedReader reader;%0A%0A
         if(m
@@ -1860,35 +1860,30 @@
     
-InputStream stream = null;%0A
+while(mRunning) %7B%0A    
     
@@ -1914,32 +1914,36 @@
 ) %7B%0A            
+    
 try %7B%0A          
@@ -1952,67 +1952,46 @@
     
-stream = new FileInputStream(mFilename.toURL().getFile());%0A
+    reader = openFile(mFilename);%0A    
     
@@ -2041,32 +2041,36 @@
                 
+  
+  
 Log.w(TAG, %22Coul
@@ -2125,32 +2125,36 @@
             
+    
 return;%0A
             
@@ -2133,32 +2133,36 @@
         return;%0A
+    
             %7D ca
@@ -2200,32 +2200,36 @@
                 
+ 
+   
 Log.w(TAG, %22Coul
@@ -2280,32 +2280,36 @@
                 
+    
 return;%0A        
@@ -2315,27 +2315,14 @@
     
- %7D%0A
      
-   %7D else %7B
+%7D
 %0A   
@@ -2334,89 +2334,36 @@
     
-stream = mStream;%0A        %7D%0A%0A        while(mRunning) %7B%0A            BufferedReader
+%7D else %7B%0A                   
  rea
@@ -2377,24 +2377,39 @@
 penFile(
-s
+mS
 tream);
+%0A            %7D
 %0A%0A      
@@ -2729,35 +2729,32 @@
             
-try %7B%0A     
+return;%0A
             
@@ -2753,34 +2753,17 @@
         
-   stream.close();
+%7D
 %0A       
@@ -2767,383 +2767,254 @@
     
+%7D%0A
     
+%7D%0A%0A
     
-%7D catch(IOException ee) %7B%0A                    Log.w(TAG, %22Couldn't even close the trace file%22, ee);%0A                %7D%0A                return;%0A            %7D%0A        %7D%0A%0A        try %7B%0A            if(
+private static BufferedReader openFile(URI filename)%0A            throws FileNotFoundException, MalformedURLException %7B%0A        FileInputStream 
 stream 
-!
 = n
-ull) %7B%0A                stream.close();%0A            %7D%0A        %7D catch(IOException e) %7B%0A            Log.w(TAG, %22Couldn't even close the trace file%22, e);%0A        %7D
+ew FileInputStream(filename.toURL().getFile());%0A        return openFile(stream);
 %0A   

Revert "Close trace file pointer."