forked from upstream-mirrors/OpenTracks
Code cleanup: move closing a file output stream at the same level as opening it
This commit is contained in:
@@ -77,11 +77,6 @@ public class CsvTrackWriter implements TrackWriter {
|
||||
pointIndex = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
printWriter.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeHeader(Track track) {
|
||||
writeCommaSeparatedLine(context.getString(R.string.generic_name),
|
||||
|
||||
@@ -80,7 +80,6 @@ public class FileTrackExporter extends AbstractTrackExporter {
|
||||
writeLocations(tracks[i], offset);
|
||||
}
|
||||
trackWriter.writeFooter();
|
||||
trackWriter.close();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -68,14 +68,6 @@ public class GpxTrackWriter implements TrackWriter {
|
||||
this.printWriter = new PrintWriter(outputStream);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (printWriter != null) {
|
||||
printWriter.close();
|
||||
printWriter = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeHeader(Track track) {
|
||||
if (printWriter != null) {
|
||||
|
||||
@@ -103,14 +103,6 @@ public class KmlTrackWriter implements TrackWriter {
|
||||
this.printWriter = new PrintWriter(outputStream);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (printWriter != null) {
|
||||
printWriter.close();
|
||||
printWriter = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeHeader(Track track) {
|
||||
if (printWriter != null) {
|
||||
|
||||
@@ -23,7 +23,6 @@ import com.google.android.apps.mytracks.content.Waypoint;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
@@ -76,13 +75,9 @@ public class KmzTrackExporter extends AbstractTrackExporter {
|
||||
ZipEntry zipEntry = new ZipEntry(KML_FILE_NAME);
|
||||
zipOutputStream.putNextEntry(zipEntry);
|
||||
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
fileTrackExporter.writeTrack(byteArrayOutputStream);
|
||||
if (fileTrackExporter.isSuccess()) {
|
||||
zipOutputStream.write(byteArrayOutputStream.toByteArray());
|
||||
zipOutputStream.closeEntry();
|
||||
} else {
|
||||
zipOutputStream.closeEntry();
|
||||
fileTrackExporter.writeTrack(zipOutputStream);
|
||||
zipOutputStream.closeEntry();
|
||||
if (!fileTrackExporter.isSuccess()) {
|
||||
throw new IOException();
|
||||
}
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ import android.util.Log;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Async Task to save tracks to the external storage.
|
||||
@@ -210,23 +211,31 @@ public class SaveAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
||||
: fileTrackExporter;
|
||||
|
||||
File file = new File(directory, fileName);
|
||||
FileOutputStream fileOutputStream;
|
||||
FileOutputStream fileOutputStream = null;
|
||||
try {
|
||||
fileOutputStream = new FileOutputStream(file);
|
||||
trackExporter.writeTrack(fileOutputStream);
|
||||
|
||||
if (trackExporter.isSuccess()) {
|
||||
savedPath = file.getAbsolutePath();
|
||||
return true;
|
||||
} else {
|
||||
if (!file.delete()) {
|
||||
Log.w(TAG, "Failed to delete file " + file.getAbsolutePath());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
Log.e(TAG, "Unable to open file " + file.getName(), e);
|
||||
return false;
|
||||
}
|
||||
trackExporter.writeTrack(fileOutputStream);
|
||||
|
||||
if (trackExporter.isSuccess()) {
|
||||
savedPath = file.getAbsolutePath();
|
||||
return true;
|
||||
} else {
|
||||
if (!file.delete()) {
|
||||
Log.w(TAG, "Failed to delete file " + file.getAbsolutePath());
|
||||
} finally {
|
||||
if (fileOutputStream != null) {
|
||||
try {
|
||||
fileOutputStream.close();
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Unable to close file output stream", e);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -96,14 +96,6 @@ public class TcxTrackWriter implements TrackWriter {
|
||||
this.printWriter = new PrintWriter(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (printWriter != null) {
|
||||
printWriter.close();
|
||||
printWriter = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExtension() {
|
||||
return TrackFileFormat.TCX.getExtension();
|
||||
|
||||
@@ -64,11 +64,6 @@ public interface TrackWriter {
|
||||
*/
|
||||
public void prepare(OutputStream outputStream);
|
||||
|
||||
/**
|
||||
* Closes the underlying file handler.
|
||||
*/
|
||||
public void close();
|
||||
|
||||
/**
|
||||
* Writes the header for a file
|
||||
*
|
||||
|
||||
@@ -444,16 +444,28 @@ public class SyncUtils {
|
||||
FileTrackExporter fileTrackExporter = new FileTrackExporter(
|
||||
myTracksProviderUtils, new Track[] { track }, trackFileFormat, context, false, null);
|
||||
|
||||
fileTrackExporter.writeTrack(new FileOutputStream(file));
|
||||
if (fileTrackExporter.isSuccess()) {
|
||||
return file;
|
||||
} else {
|
||||
if (!file.delete()) {
|
||||
Log.d(TAG, "Unable to delete file for track " + track.getName());
|
||||
FileOutputStream fileOutputStream = null;
|
||||
try {
|
||||
fileOutputStream = new FileOutputStream(file);
|
||||
fileTrackExporter.writeTrack(fileOutputStream);
|
||||
if (fileTrackExporter.isSuccess()) {
|
||||
return file;
|
||||
} else {
|
||||
if (!file.delete()) {
|
||||
Log.d(TAG, "Unable to delete file for track " + track.getName());
|
||||
}
|
||||
Log.d(TAG, "Unable to get file for track " + track.getName());
|
||||
return null;
|
||||
}
|
||||
Log.d(TAG, "Unable to get file for track " + track.getName());
|
||||
return null;
|
||||
}
|
||||
} finally {
|
||||
if (fileOutputStream != null) {
|
||||
try {
|
||||
fileOutputStream.close();
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Unable to close file output stream", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+1
-4
@@ -96,8 +96,7 @@ public class FileTrackExporterTest extends AndroidTestCase {
|
||||
trackWriter.writeBeginTrack(track, null);
|
||||
trackWriter.writeEndTrack(track, null);
|
||||
trackWriter.writeFooter();
|
||||
trackWriter.close();
|
||||
|
||||
|
||||
mocksControl.replay();
|
||||
fileTrackExporter.writeTrack(outputStream);
|
||||
|
||||
@@ -128,7 +127,6 @@ public class FileTrackExporterTest extends AndroidTestCase {
|
||||
trackWriter.writeBeginTrack(track, null);
|
||||
trackWriter.writeEndTrack(track, null);
|
||||
trackWriter.writeFooter();
|
||||
trackWriter.close();
|
||||
|
||||
mocksControl.replay();
|
||||
fileTrackExporter.writeTrack(outputStream);
|
||||
@@ -193,7 +191,6 @@ public class FileTrackExporterTest extends AndroidTestCase {
|
||||
trackWriter.writeEndTrack(trackEq(track), locationEq(locations[5]));
|
||||
|
||||
trackWriter.writeFooter();
|
||||
trackWriter.close();
|
||||
|
||||
mocksControl.replay();
|
||||
fileTrackExporter.writeTrack(outputStream);
|
||||
|
||||
@@ -132,7 +132,6 @@ public abstract class TrackWriterTest extends AndroidTestCase {
|
||||
trackWriter.writeCloseSegment();
|
||||
trackWriter.writeEndTrack(track, location4);
|
||||
trackWriter.writeFooter();
|
||||
trackWriter.close();
|
||||
return output.toString();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user