Code cleanup: move closing a file output stream at the same level as opening it

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