diff options
| author | Bananeweizen <bananeweizen@gmx.de> | 2013-05-11 08:56:34 +0200 |
|---|---|---|
| committer | Bananeweizen <bananeweizen@gmx.de> | 2013-05-11 09:34:58 +0200 |
| commit | aea41c86f05d11828120fa71c0bcb5b1756061f1 (patch) | |
| tree | b2bb0ee0c681a838e1c22eb61885e2c1a5ca715e | |
| parent | f3b65c3221d96bd431000b71bc8fb29a81c0eab9 (diff) | |
| download | cgeo-aea41c86f05d11828120fa71c0bcb5b1756061f1.zip cgeo-aea41c86f05d11828120fa71c0bcb5b1756061f1.tar.gz cgeo-aea41c86f05d11828120fa71c0bcb5b1756061f1.tar.bz2 | |
#1348: use buffered io
| -rw-r--r-- | main/src/cgeo/geocaching/connector/oc/OCXMLClient.java | 11 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/export/FieldnoteExport.java | 12 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/export/GpxExport.java | 5 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/files/FileParser.java | 12 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/files/GPXImporter.java | 5 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/files/LocalStorage.java | 19 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/network/HtmlImage.java | 17 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/ui/ImagesList.java | 9 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/utils/Log.java | 7 |
9 files changed, 55 insertions, 42 deletions
diff --git a/main/src/cgeo/geocaching/connector/oc/OCXMLClient.java b/main/src/cgeo/geocaching/connector/oc/OCXMLClient.java index 6767b48..df75682 100644 --- a/main/src/cgeo/geocaching/connector/oc/OCXMLClient.java +++ b/main/src/cgeo/geocaching/connector/oc/OCXMLClient.java @@ -9,12 +9,14 @@ import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.GeopointFormatter; import cgeo.geocaching.network.Network; import cgeo.geocaching.network.Parameters; +import cgeo.geocaching.utils.IOUtils; import cgeo.geocaching.utils.Log; import ch.boye.httpclientandroidlib.HttpResponse; import org.apache.commons.lang3.StringUtils; +import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Collection; @@ -39,10 +41,12 @@ public class OCXMLClient { return null; } - Collection<Geocache> caches = OC11XMLParser.parseCaches(new GZIPInputStream(data)); + final BufferedInputStream stream = new BufferedInputStream(new GZIPInputStream(data)); + Collection<Geocache> caches = OC11XMLParser.parseCaches(stream); if (caches.iterator().hasNext()) { Geocache cache = caches.iterator().next(); cgData.saveCache(cache, LoadFlags.SAVE_ALL); + IOUtils.closeQuietly(stream); return cache; } return null; @@ -64,7 +68,10 @@ public class OCXMLClient { return Collections.emptyList(); } - return OC11XMLParser.parseCachesFiltered(new GZIPInputStream(data)); + final BufferedInputStream stream = new BufferedInputStream(new GZIPInputStream(data)); + final Collection<Geocache> result = OC11XMLParser.parseCachesFiltered(stream); + IOUtils.closeQuietly(stream); + return result; } catch (IOException e) { Log.e("Error parsing nearby search result", e); return Collections.emptyList(); diff --git a/main/src/cgeo/geocaching/export/FieldnoteExport.java b/main/src/cgeo/geocaching/export/FieldnoteExport.java index 2900781..1165a14 100644 --- a/main/src/cgeo/geocaching/export/FieldnoteExport.java +++ b/main/src/cgeo/geocaching/export/FieldnoteExport.java @@ -25,6 +25,7 @@ import android.view.ContextThemeWrapper; import android.view.View; import android.widget.CheckBox; +import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -153,16 +154,19 @@ class FieldnoteExport extends AbstractExport { SimpleDateFormat fileNameDateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.US); exportFile = new File(exportLocation.toString() + '/' + fileNameDateFormat.format(new Date()) + ".txt"); - Writer fw = null; + Writer fileWriter = null; + BufferedOutputStream buffer = null; try { OutputStream os = new FileOutputStream(exportFile); - fw = new OutputStreamWriter(os, CharEncoding.UTF_16); - fw.write(fieldNoteBuffer.toString()); + buffer = new BufferedOutputStream(os); + fileWriter = new OutputStreamWriter(buffer, CharEncoding.UTF_16); + fileWriter.write(fieldNoteBuffer.toString()); } catch (IOException e) { Log.e("FieldnoteExport.ExportTask export", e); return false; } finally { - IOUtils.closeQuietly(fw); + IOUtils.closeQuietly(buffer); + IOUtils.closeQuietly(fileWriter); } if (upload) { diff --git a/main/src/cgeo/geocaching/export/GpxExport.java b/main/src/cgeo/geocaching/export/GpxExport.java index 3e58276..c17448f 100644 --- a/main/src/cgeo/geocaching/export/GpxExport.java +++ b/main/src/cgeo/geocaching/export/GpxExport.java @@ -32,6 +32,7 @@ import android.view.View; import android.widget.CheckBox; import android.widget.TextView; +import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; @@ -142,13 +143,13 @@ class GpxExport extends AbstractExport { final SimpleDateFormat fileNameDateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.US); final File exportFile = new File(Settings.getGpxExportDir() + File.separatorChar + "export_" + fileNameDateFormat.format(new Date()) + ".gpx"); - FileWriter writer = null; + BufferedWriter writer = null; try { final File exportLocation = new File(Settings.getGpxExportDir()); exportLocation.mkdirs(); final XmlSerializer gpx = new KXmlSerializer(); - writer = new FileWriter(exportFile); + writer = new BufferedWriter(new FileWriter(exportFile)); gpx.setOutput(writer); gpx.startDocument("UTF-8", true); diff --git a/main/src/cgeo/geocaching/files/FileParser.java b/main/src/cgeo/geocaching/files/FileParser.java index 50b65a1..f979d74 100644 --- a/main/src/cgeo/geocaching/files/FileParser.java +++ b/main/src/cgeo/geocaching/files/FileParser.java @@ -2,7 +2,9 @@ package cgeo.geocaching.files; import cgeo.geocaching.Geocache; import cgeo.geocaching.utils.CancellableHandler; +import cgeo.geocaching.utils.IOUtils; +import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; @@ -16,7 +18,7 @@ import java.util.concurrent.CancellationException; public abstract class FileParser { /** * Parses caches from input stream. - * + * * @param stream * @param progressHandler * for reporting parsing progress (in bytes read from input stream) @@ -38,11 +40,11 @@ public abstract class FileParser { * @throws ParserException */ public Collection<Geocache> parse(final File file, final CancellableHandler progressHandler) throws IOException, ParserException { - FileInputStream fis = new FileInputStream(file); + BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file)); try { - return parse(fis, progressHandler); + return parse(stream, progressHandler); } finally { - fis.close(); + IOUtils.closeQuietly(stream); } } @@ -59,7 +61,7 @@ public abstract class FileParser { } return buffer; } finally { - input.close(); + IOUtils.closeQuietly(input); } } diff --git a/main/src/cgeo/geocaching/files/GPXImporter.java b/main/src/cgeo/geocaching/files/GPXImporter.java index ff81fe1..69d2dac 100644 --- a/main/src/cgeo/geocaching/files/GPXImporter.java +++ b/main/src/cgeo/geocaching/files/GPXImporter.java @@ -24,6 +24,7 @@ import android.net.Uri; import android.os.Handler; import android.os.Message; +import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -287,7 +288,7 @@ public class GPXImporter { Collection<Geocache> caches = Collections.emptySet(); // can't assume that GPX file comes before waypoint file in zip -> so we need two passes // 1. parse GPX files - ZipInputStream zis = new ZipInputStream(getInputStream()); + ZipInputStream zis = new ZipInputStream(new BufferedInputStream(getInputStream())); try { for (ZipEntry zipEntry = zis.getNextEntry(); zipEntry != null; zipEntry = zis.getNextEntry()) { if (StringUtils.endsWithIgnoreCase(zipEntry.getName(), GPX_FILE_EXTENSION)) { @@ -305,7 +306,7 @@ public class GPXImporter { } // 2. parse waypoint files - zis = new ZipInputStream(getInputStream()); + zis = new ZipInputStream(new BufferedInputStream(getInputStream())); try { for (ZipEntry zipEntry = zis.getNextEntry(); zipEntry != null; zipEntry = zis.getNextEntry()) { if (StringUtils.endsWithIgnoreCase(zipEntry.getName(), WAYPOINTS_FILE_SUFFIX_AND_EXTENSION)) { diff --git a/main/src/cgeo/geocaching/files/LocalStorage.java b/main/src/cgeo/geocaching/files/LocalStorage.java index f59f15c..0f3e0e1 100644 --- a/main/src/cgeo/geocaching/files/LocalStorage.java +++ b/main/src/cgeo/geocaching/files/LocalStorage.java @@ -2,14 +2,18 @@ package cgeo.geocaching.files; import cgeo.geocaching.cgeoapplication; import cgeo.geocaching.utils.CryptUtils; +import cgeo.geocaching.utils.IOUtils; import cgeo.geocaching.utils.Log; import ch.boye.httpclientandroidlib.Header; import ch.boye.httpclientandroidlib.HttpResponse; + import org.apache.commons.lang3.StringUtils; import android.os.Environment; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; @@ -275,19 +279,14 @@ public class LocalStorage { destination.getParentFile().mkdirs(); InputStream input = null; - OutputStream output; + OutputStream output = null; try { - input = new FileInputStream(source); - output = new FileOutputStream(destination); + input = new BufferedInputStream(new FileInputStream(source)); + output = new BufferedOutputStream(new FileOutputStream(destination)); } catch (FileNotFoundException e) { Log.e("LocalStorage.copy: could not open file", e); - if (input != null) { - try { - input.close(); - } catch (IOException e1) { - // ignore - } - } + IOUtils.closeQuietly(input); + IOUtils.closeQuietly(output); return false; } diff --git a/main/src/cgeo/geocaching/network/HtmlImage.java b/main/src/cgeo/geocaching/network/HtmlImage.java index 45cdbb5..15ea235 100644 --- a/main/src/cgeo/geocaching/network/HtmlImage.java +++ b/main/src/cgeo/geocaching/network/HtmlImage.java @@ -6,6 +6,7 @@ import cgeo.geocaching.cgeoapplication; import cgeo.geocaching.compatibility.Compatibility; import cgeo.geocaching.connector.ConnectorFactory; import cgeo.geocaching.files.LocalStorage; +import cgeo.geocaching.utils.IOUtils; import cgeo.geocaching.utils.ImageHelper; import cgeo.geocaching.utils.Log; @@ -21,10 +22,10 @@ import android.graphics.drawable.BitmapDrawable; import android.net.Uri; import android.text.Html; +import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.IOException; import java.util.Date; public class HtmlImage implements Html.ImageGetter { @@ -209,20 +210,14 @@ public class HtmlImage implements Html.ImageGetter { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; - FileInputStream fis = null; + BufferedInputStream stream = null; try { - fis = new FileInputStream(file); - BitmapFactory.decodeStream(fis, null, options); + stream = new BufferedInputStream(new FileInputStream(file)); + BitmapFactory.decodeStream(stream, null, options); } catch (FileNotFoundException e) { Log.e("HtmlImage.setSampleSize", e); } finally { - if (fis != null) { - try { - fis.close(); - } catch (IOException e) { - // ignore - } - } + IOUtils.closeQuietly(stream); } int scale = 1; diff --git a/main/src/cgeo/geocaching/ui/ImagesList.java b/main/src/cgeo/geocaching/ui/ImagesList.java index b2c819c..218e16e 100644 --- a/main/src/cgeo/geocaching/ui/ImagesList.java +++ b/main/src/cgeo/geocaching/ui/ImagesList.java @@ -30,6 +30,7 @@ import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; +import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.util.Collection; @@ -184,15 +185,15 @@ public class ImagesList { private void viewImageInStandardApp(final BitmapDrawable image) { final File file = LocalStorage.getStorageFile(null, "temp.jpg", false, true); - FileOutputStream fos = null; + BufferedOutputStream stream = null; try { - fos = new FileOutputStream(file); - image.getBitmap().compress(CompressFormat.JPEG, 100, fos); + stream = new BufferedOutputStream(new FileOutputStream(file)); + image.getBitmap().compress(CompressFormat.JPEG, 100, stream); } catch (Exception e) { Log.e("ImagesActivity.handleMessage.onClick", e); return; } finally { - IOUtils.closeQuietly(fos); + IOUtils.closeQuietly(stream); } final Intent intent = new Intent(); diff --git a/main/src/cgeo/geocaching/utils/Log.java b/main/src/cgeo/geocaching/utils/Log.java index 6d57b75..f912ddd 100644 --- a/main/src/cgeo/geocaching/utils/Log.java +++ b/main/src/cgeo/geocaching/utils/Log.java @@ -2,6 +2,7 @@ package cgeo.geocaching.utils; import android.os.Environment; +import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; @@ -100,12 +101,14 @@ final public class Log { first = false; file.delete(); } + Writer writer = null; try { - final Writer writer = new FileWriter(file, true); + writer = new BufferedWriter(new FileWriter(file, true)); writer.write(msg); - writer.close(); } catch (final IOException e) { Log.e("logToFile: cannot write to " + file, e); + } finally { + IOUtils.closeQuietly(writer); } } } |
