Commit f887711c authored by Bartek Fabiszewski's avatar Bartek Fabiszewski
Browse files

Fix potential resource leaks

parent 9616ec4e
Loading
Loading
Loading
Loading
+31 −14
Original line number Diff line number Diff line
@@ -94,6 +94,7 @@ public class GpxExportService extends IntentService {
            return;
        }

        FileOutputStream fileOutputStream = null;
        try {
            String trackName = db.getTrackName();
            if (trackName == null) {
@@ -111,7 +112,7 @@ public class GpxExportService extends IntentService {
                file = getFile(dir, trackName + "_" + (++i));
            }

            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream = new FileOutputStream(file);

            XmlSerializer serializer = Xml.newSerializer();
            StringWriter writer = new StringWriter();
@@ -146,14 +147,25 @@ public class GpxExportService extends IntentService {
            serializer.endTag("", "gpx");
            serializer.endDocument();
            serializer.flush();

            String dataWrite = writer.toString();
            fileOutputStream.write(dataWrite.getBytes());
            fileOutputStream.close();
            fileOutputStream.flush();

            if (Logger.DEBUG) { Log.d(TAG, "[export gpx file written to " + file.getPath()); }
            sendBroadcast(BROADCAST_EXPORT_DONE, null);
        } catch (IOException|IllegalArgumentException|IllegalStateException e) {
            if (Logger.DEBUG) { Log.d(TAG, "[export gpx exception: " + e + "]"); }
            sendBroadcast(BROADCAST_EXPORT_FAILED, e.getMessage());
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    if (Logger.DEBUG) { Log.d(TAG, "[export gpx exception: " + e + "]"); }
                    sendBroadcast(BROADCAST_EXPORT_FAILED, e.getMessage());
                }
            }
        }

    }
@@ -172,6 +184,9 @@ public class GpxExportService extends IntentService {
        Cursor cursor = db.getPositions();
        serializer.startTag(null, "trkseg");

        // suppress as it requires target api 19
        //noinspection TryFinallyCanBeTryWithResources
        try {
            while (cursor.moveToNext()) {
                serializer.startTag(null, "trkpt");
                serializer.attribute(null, "lat", cursor.getString(cursor.getColumnIndex(DbContract.Positions.COLUMN_LATITUDE)));
@@ -185,7 +200,9 @@ public class GpxExportService extends IntentService {
                writeTag(serializer, "name", cursor.getString(cursor.getColumnIndex(DbContract.Positions._ID)));
                serializer.endTag(null, "trkpt");
            }
        } finally {
            cursor.close();
        }

        serializer.endTag(null, "trkseg");
    }