aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/files/FileParser.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/files/FileParser.java')
-rw-r--r--main/src/cgeo/geocaching/files/FileParser.java61
1 files changed, 45 insertions, 16 deletions
diff --git a/main/src/cgeo/geocaching/files/FileParser.java b/main/src/cgeo/geocaching/files/FileParser.java
index f697f07..9c9ba8d 100644
--- a/main/src/cgeo/geocaching/files/FileParser.java
+++ b/main/src/cgeo/geocaching/files/FileParser.java
@@ -6,36 +6,66 @@ import android.os.Handler;
import java.io.BufferedReader;
import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
+import java.io.FileInputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Collection;
import java.util.Date;
public abstract class FileParser {
- protected static StringBuilder readFile(File file)
- throws FileNotFoundException, IOException {
+ /**
+ * Parses caches from input stream.
+ *
+ * @param stream
+ * @param progressHandler
+ * for reporting parsing progress (in bytes read from input stream)
+ * @return collection of parsed caches
+ * @throws IOException
+ * if the input stream can't be read
+ * @throws ParserException
+ * if the input stream contains data not matching the file format of the parser
+ */
+ public abstract Collection<cgCache> parse(final InputStream stream, final Handler progressHandler) throws IOException, ParserException;
+
+ /**
+ * Convenience method for parsing a file.
+ *
+ * @param file
+ * @param progressHandler
+ * @return
+ * @throws IOException
+ * @throws ParserException
+ */
+ public Collection<cgCache> parse(final File file, final Handler progressHandler) throws IOException, ParserException {
+ FileInputStream fis = new FileInputStream(file);
+ try {
+ return parse(fis, progressHandler);
+ } finally {
+ fis.close();
+ }
+ }
+
+ protected static StringBuilder readStream(InputStream is, Handler progressHandler) throws IOException {
final StringBuilder buffer = new StringBuilder();
- final BufferedReader input = new BufferedReader(new FileReader(file));
+ ProgressInputStream progressInputStream = new ProgressInputStream(is);
+ final BufferedReader input = new BufferedReader(new InputStreamReader(progressInputStream));
+
try {
String line = null;
while ((line = input.readLine()) != null) {
buffer.append(line);
+ showProgressMessage(progressHandler, progressInputStream.getProgress());
}
+ return buffer;
} finally {
input.close();
}
- return buffer;
}
- protected static void showCountMessage(final Handler handler, final int msgId, final int count) {
- if (handler != null && (count <= 1 || count % 10 == 0)) {
- handler.sendMessage(handler.obtainMessage(0, msgId, count));
- }
- }
-
- protected static void showCountMessage(final Handler handler, final int msgId, final int count, final int bytesRead) {
- if (handler != null && (count <= 1 || count % 10 == 0)) {
- handler.sendMessage(handler.obtainMessage(0, msgId, count, bytesRead));
+ protected static void showProgressMessage(final Handler handler, final int bytesRead) {
+ if (handler != null) {
+ handler.sendMessage(handler.obtainMessage(0, bytesRead, 0));
}
}
@@ -49,5 +79,4 @@ public abstract class FileParser {
cache.updated = time;
cache.detailedUpdate = time;
}
-
}