Codecleanup: for importer, make sure inputstreams are closed properly

This commit is contained in:
Jimmy Shih
2013-09-04 16:26:27 -07:00
parent 396845a694
commit e0375050d2
5 changed files with 90 additions and 72 deletions
@@ -30,6 +30,7 @@ import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@@ -132,6 +133,9 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
publishProgress(i + 1, totalCount);
}
return true;
} catch (IOException e) {
Log.e(TAG, "IOException", e);
return false;
} finally {
if (wakeLock != null && wakeLock.isHeld()) {
wakeLock.release();
@@ -159,7 +163,8 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
*
* @param file the file
*/
private boolean importFile(final File file) {
private boolean importFile(final File file) throws IOException {
FileInputStream fileInputStream = null;
try {
TrackImporter trackImporter;
if (trackFileFormat == TrackFileFormat.KML) {
@@ -177,8 +182,9 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
}
} else {
trackImporter = new GpxFileTrackImporter(context);
}
long trackIds[] = trackImporter.importFile(new FileInputStream(file));
}
fileInputStream = new FileInputStream(file);
long trackIds[] = trackImporter.importFile(fileInputStream);
int length = trackIds.length;
if (length > 0) {
trackId = trackIds[length - 1];
@@ -187,6 +193,10 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
} catch (Exception e) {
Log.d(TAG, "file: " + file.getAbsolutePath(), e);
return false;
} finally {
if (fileInputStream != null) {
fileInputStream.close();
}
}
}
@@ -40,6 +40,8 @@ import org.xml.sax.SAXException;
*/
public class KmzTrackImporter implements TrackImporter {
private static final int BUFFER_SIZE = 4096;
private final Context context;
private final String photoPath;
@@ -51,18 +53,17 @@ public class KmzTrackImporter implements TrackImporter {
@Override
public long[] importFile(InputStream inputStream)
throws IOException, ParserConfigurationException, SAXException {
long[] result = null;
ZipInputStream zipInputStream = null;
try {
zipInputStream = new ZipInputStream(inputStream);
long[] result = null;
ZipEntry zipEntry;
zipInputStream = new ZipInputStream(inputStream);
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
String fileName = zipEntry.getName();
if (fileName.equals(KmzTrackExporter.KML_FILE_NAME)) {
KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(
context, -1L, photoPath);
byte[] buffer = readKml(zipInputStream);
result = kmlFileTrackImporter.importFile(new ByteArrayInputStream(buffer));
if (fileName.equals(KmzTrackExporter.KMZ_KML_FILE)) {
result = parseKml(zipInputStream);
} else {
String prefix = KmzTrackExporter.KMZ_IMAGES_DIR + File.separatorChar;
if (fileName.startsWith(prefix)) {
@@ -71,19 +72,33 @@ public class KmzTrackImporter implements TrackImporter {
}
zipInputStream.closeEntry();
}
return result == null ? new long[0] : result;
} finally {
if (zipInputStream != null) {
zipInputStream.close();
}
}
return result == null ? new long[0] : result;
}
private byte[] readKml(ZipInputStream zipInputStream) throws IOException {
private long[] parseKml(ZipInputStream zipInputStream)
throws IOException, ParserConfigurationException, SAXException {
ByteArrayInputStream byteArrayInputStream = null;
try {
KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(context, -1L, photoPath);
byteArrayInputStream = new ByteArrayInputStream(getKml(zipInputStream));
return kmlFileTrackImporter.importFile(byteArrayInputStream);
} finally {
if (byteArrayInputStream != null) {
byteArrayInputStream.close();
}
}
}
private byte[] getKml(ZipInputStream zipInputStream) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = null;
try {
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[KmzTrackExporter.BUFFER_SIZE];
byte[] buffer = new byte[BUFFER_SIZE];
int count;
while ((count = zipInputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, count);
@@ -100,7 +115,7 @@ public class KmzTrackImporter implements TrackImporter {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(photoPath + File.separatorChar + fileName);
byte[] buffer = new byte[KmzTrackExporter.BUFFER_SIZE];
byte[] buffer = new byte[BUFFER_SIZE];
int count;
while ((count = zipInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, count);
@@ -37,10 +37,10 @@ import java.util.zip.ZipOutputStream;
*/
public class KmzTrackExporter extends AbstractTrackExporter {
public static final String KMZ_IMAGES_DIR = "images";
public static final String KMZ_EXTENSION = "kmz";
public static final int BUFFER_SIZE = 4096;
public static final String KML_FILE_NAME = "doc.kml";
public static final String KMZ_IMAGES_DIR = "images";
public static final String KMZ_KML_FILE = "doc.kml";
private static final int BUFFER_SIZE = 4096;
private final MyTracksProviderUtils myTracksProviderUtils;
private final FileTrackExporter fileTrackExporter;
@@ -72,7 +72,7 @@ public class KmzTrackExporter extends AbstractTrackExporter {
zipOutputStream = new ZipOutputStream(outputStream);
// Add kml file
ZipEntry zipEntry = new ZipEntry(KML_FILE_NAME);
ZipEntry zipEntry = new ZipEntry(KMZ_KML_FILE);
zipOutputStream.putNextEntry(zipEntry);
fileTrackExporter.writeTrack(zipOutputStream);
@@ -345,33 +345,40 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
*/
private void insertNewDriveFiles(Collection<File> driveFiles) throws IOException {
for (File driveFile : driveFiles) {
if (driveFile != null) {
InputStream inputStream = downloadDriveFile(driveFile, true);
if (inputStream != null) {
KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(context, -1L, null);
try {
long[] trackIds = kmlFileTrackImporter.importFile(inputStream);
if (trackIds.length == 1) {
Track track = myTracksProviderUtils.getTrack(trackIds[0]);
if (track == null) {
Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId());
} else {
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, driveFile);
Log.d(TAG, "Add from Google Drive " + track.getName());
}
} else {
// Clean up if imported more than one track
for (int i = 0; i < trackIds.length; i++) {
myTracksProviderUtils.deleteTrack(trackIds[i]);
}
}
} catch (SAXException e) {
Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId(), e);
} catch (IOException e) {
Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId(), e);
} catch (ParserConfigurationException e) {
Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId(), e);
InputStream inputStream = null;
try {
if (driveFile == null) {
continue;
}
inputStream = downloadDriveFile(driveFile, true);
if (inputStream == null) {
continue;
}
KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(context, -1L, null);
long[] trackIds = kmlFileTrackImporter.importFile(inputStream);
if (trackIds.length == 1) {
Track track = myTracksProviderUtils.getTrack(trackIds[0]);
if (track == null) {
Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId());
continue;
}
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, driveFile);
Log.d(TAG, "Add from Google Drive " + track.getName());
} else {
// Clean up if imported more than one track
for (int i = 0; i < trackIds.length; i++) {
myTracksProviderUtils.deleteTrack(trackIds[i]);
}
}
} catch (SAXException e) {
Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId(), e);
} catch (IOException e) {
Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId(), e);
} catch (ParserConfigurationException e) {
Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId(), e);
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
@@ -489,14 +496,15 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
file.delete();
}
}
InputStream inputStream = downloadDriveFile(driveFile, true);
if (inputStream == null) {
Log.e(TAG, "Unable to update track. Input stream is null for track " + track.getName());
return false;
}
KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(
context, track.getId(), null);
InputStream inputStream = null;
try {
inputStream = downloadDriveFile(driveFile, true);
if (inputStream == null) {
Log.e(TAG, "Unable to update track. Input stream is null for track " + track.getName());
return false;
}
KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(
context, track.getId(), null);
long[] trackIds = kmlFileTrackImporter.importFile(inputStream);
if (trackIds.length == 1) {
Track newTrack = myTracksProviderUtils.getTrack(trackIds[0]);
@@ -515,6 +523,10 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
Log.e(TAG, "Unable to merge", e);
} catch (IOException e) {
Log.e(TAG, "Unable to merge", e);
} finally {
if (inputStream != null) {
inputStream.close();
}
}
return false;
}
@@ -111,10 +111,6 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
/**
* Configures the calorie preference.
*
* @param reference to configure
* @param key of the preference
* @param defaultValue default value of this preference
*/
private void configCaloriePreference() {
caloriePreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@@ -160,11 +156,6 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
/**
* Configures the weight preference.
*
* @param preference to configure
* @param key of the preference
* @param defaultValue default value of this preference
* @param isEnable true means enable the weight preference
*/
private void configWeightPreference() {
weightPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@@ -188,10 +179,6 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
/**
* Updates the weight summary.
*
* @param preference the preference
* @param keyId the key id
* @param defaultValue the default value
*/
private void updateWeightSummary() {
boolean metricUnits = PreferencesUtils.isMetricUnits(this);
@@ -201,10 +188,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
/**
* Updates the weight summary.
*
* @param preference the preference
* @param keyId the key id
* @param defaultValue the default value
* @param metricUnits the status of metric units
* @param metricUnits true if metric units
*/
private void updateWeightSummary(boolean metricUnits) {
int displayValue = getWeightDisplayValue(metricUnits);
@@ -216,8 +200,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
* Gets the weight display value in metric or imperial depending on the
* preferred units.
*
* @param keyId the key id
* @param defaultValue the default value
* @param metricUnits true if metric units
*/
private int getWeightDisplayValue(boolean metricUnits) {
int value = PreferencesUtils.getInt(this, R.string.stats_weight_key,
@@ -231,8 +214,6 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
/**
* Stores the weight value, always in metric units.
*
* @param keyId the key id
* @param defaultValue the default value
* @param displayValue the display value
*/
private void storeWeightValue(String displayValue) {