summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeith Packard <keithp@keithp.com>2017-05-28 14:00:15 -0700
committerKeith Packard <keithp@keithp.com>2017-05-28 14:04:21 -0700
commitda914cd72411af8c36af05b13c11b9093c8a378c (patch)
tree9443f90fbf21315b609631c300bfb4acb3ac23da
parent9e1295ff74d03f940fc68e6795bf30687162a440 (diff)
altoslib: Create data file open helper in AltosLib
Use InputStream everywhere, instead of Reader. Create private string input stream as java one is deprecated. Signed-off-by: Keith Packard <keithp@keithp.com>
-rw-r--r--altoslib/AltosEepromFile.java2
-rw-r--r--altoslib/AltosEepromNew.java67
-rw-r--r--altoslib/AltosEepromRecordSet.java2
-rw-r--r--altoslib/AltosJson.java10
-rw-r--r--altoslib/AltosLib.java24
-rw-r--r--altoslib/AltosStringInputStream.java61
-rw-r--r--altoslib/AltosTelemetryFile.java2
-rw-r--r--altoslib/AltosTelemetryIterable.java30
-rw-r--r--altoslib/Makefile.am1
-rw-r--r--altosui/AltosLanded.java4
-rw-r--r--altosui/AltosUI.java52
-rw-r--r--altosuilib/AltosDataChooser.java16
-rw-r--r--telegps/TeleGPS.java19
13 files changed, 169 insertions, 121 deletions
diff --git a/altoslib/AltosEepromFile.java b/altoslib/AltosEepromFile.java
index df19877b..463948b1 100644
--- a/altoslib/AltosEepromFile.java
+++ b/altoslib/AltosEepromFile.java
@@ -33,7 +33,7 @@ public class AltosEepromFile implements AltosRecordSet {
out.printf("%s\n", set.eeprom.toString());
}
- public AltosEepromFile(Reader input) throws IOException {
+ public AltosEepromFile(InputStream input) throws IOException {
set = new AltosEepromRecordSet(input);
}
diff --git a/altoslib/AltosEepromNew.java b/altoslib/AltosEepromNew.java
index 0da3df71..c373bff3 100644
--- a/altoslib/AltosEepromNew.java
+++ b/altoslib/AltosEepromNew.java
@@ -93,19 +93,34 @@ public class AltosEepromNew {
w.append('\n');
}
- private boolean read_config(Reader r) throws IOException {
- config = AltosJson.fromReader(r);
+ private boolean read_config(InputStream stream) throws IOException {
+ config = AltosJson.fromInputStream(stream);
if (config == null)
return false;
return true;
}
- private boolean read_data(Reader r) throws IOException {
- BufferedReader br = new BufferedReader(r);
- String s;
+ private String read_line(InputStream stream) throws IOException {
+ StringBuffer buffer = null;
+ int c;
+
+ for (;;) {
+ c = stream.read();
+ if (c == -1 && buffer == null)
+ return null;
+ if (buffer == null)
+ buffer = new StringBuffer();
+ if (c == -1 || c == '\n')
+ return buffer.toString();
+ buffer.append((char) c);
+ }
+ }
+
+ private boolean read_data(InputStream stream) throws IOException {
+ String s;
data = new ArrayList<Byte>();
- while ((s = br.readLine()) != null) {
+ while ((s = read_line(stream)) != null) {
String[] tokens = s.split("\\s+");
@@ -122,24 +137,24 @@ public class AltosEepromNew {
return true;
}
- private boolean read_old_config(BufferedReader r) throws IOException {
+ private boolean read_old_config(InputStream stream) throws IOException {
AltosConfigData cfg = new AltosConfigData();
for (;;) {
boolean done = false;
/* The data starts with an upper case F character followed by a space */
- r.mark(2);
- int first = r.read();
+ stream.mark(2);
+ int first = stream.read();
if (first == 'F') {
- int second = r.read();
+ int second = stream.read();
if (second == ' ')
done = true;
}
- r.reset();
+ stream.reset();
if (done)
break;
- String line = r.readLine();
+ String line = read_line(stream);
if (line == null)
return false;
cfg.parse_line(line);
@@ -148,11 +163,11 @@ public class AltosEepromNew {
return true;
}
- private boolean read_old_data(BufferedReader r) throws IOException {
+ private boolean read_old_data(InputStream stream) throws IOException {
String line;
data = new ArrayList<Byte>();
- while ((line = r.readLine()) != null) {
+ while ((line = read_line(stream)) != null) {
String[] tokens = line.split("\\s+");
/* Make sure there's at least a type and time */
@@ -207,22 +222,22 @@ public class AltosEepromNew {
return true;
}
- private void read(Reader r) throws IOException {
- BufferedReader br = new BufferedReader(r);
+ private void read(InputStream stream) throws IOException {
+ BufferedInputStream bis = new BufferedInputStream(stream);
- br.mark(1);
- int c = br.read();
- br.reset();
+ bis.mark(1);
+ int c = bis.read();
+ bis.reset();
if (c == '{') {
- if (!read_config(br))
+ if (!read_config(bis))
throw new IOException("failed to read config");
- if (!read_data(br))
+ if (!read_data(bis))
throw new IOException("failed to read data");
} else {
- if (!read_old_config(br))
+ if (!read_old_config(bis))
throw new IOException("failed to read old config");
- if (!read_old_data(br))
+ if (!read_old_data(bis))
throw new IOException("failed to read old data");
}
}
@@ -253,12 +268,12 @@ public class AltosEepromNew {
/*
* Constructors
*/
- public AltosEepromNew(Reader r) throws IOException {
- read(r);
+ public AltosEepromNew(InputStream stream) throws IOException {
+ read(stream);
}
public AltosEepromNew(String s) throws IOException {
- read(new StringReader(s));
+ read(new AltosStringInputStream(s));
}
public AltosEepromNew(AltosJson config, ArrayList<Byte> data) {
diff --git a/altoslib/AltosEepromRecordSet.java b/altoslib/AltosEepromRecordSet.java
index 183cb9ae..0c60c1a5 100644
--- a/altoslib/AltosEepromRecordSet.java
+++ b/altoslib/AltosEepromRecordSet.java
@@ -110,7 +110,7 @@ public class AltosEepromRecordSet implements AltosRecordSet {
}
}
- public AltosEepromRecordSet(Reader input) throws IOException {
+ public AltosEepromRecordSet(InputStream input) throws IOException {
this(new AltosEepromNew(input));
}
}
diff --git a/altoslib/AltosJson.java b/altoslib/AltosJson.java
index 9191be68..ce50b872 100644
--- a/altoslib/AltosJson.java
+++ b/altoslib/AltosJson.java
@@ -255,7 +255,7 @@ class JsonToken {
* Lexer for json
*/
class JsonLexer extends JsonUtil {
- Reader f;
+ InputStream f;
int line;
int ungot = -2;
StringBuffer pending_token;
@@ -445,12 +445,12 @@ class JsonLexer extends JsonUtil {
}
JsonLexer(String s) {
- f = new StringReader(s);
+ f = new AltosStringInputStream(s);
line = 1;
token = null;
}
- JsonLexer(Reader f) {
+ JsonLexer(InputStream f) {
this.f = f;
line = 1;
token = null;
@@ -570,7 +570,7 @@ class JsonParse {
lexer = new JsonLexer(s);
}
- JsonParse(Reader f) {
+ JsonParse(InputStream f) {
lexer = new JsonLexer(f);
}
}
@@ -670,7 +670,7 @@ public class AltosJson extends JsonUtil {
}
}
- public static AltosJson fromReader(Reader f) {
+ public static AltosJson fromInputStream(InputStream f) {
JsonParse parse = new JsonParse(f);
try {
return parse.parse();
diff --git a/altoslib/AltosLib.java b/altoslib/AltosLib.java
index 355c7a27..6c1729df 100644
--- a/altoslib/AltosLib.java
+++ b/altoslib/AltosLib.java
@@ -588,4 +588,28 @@ public class AltosLib {
public static String igniter_name(int i) {
return String.format("Ignitor %c", 'A' + i);
}
+
+ public static AltosRecordSet record_set(File file) throws FileNotFoundException, IOException {
+ FileInputStream in;
+ in = new FileInputStream(file);
+ if (file.getName().endsWith("telem")) {
+ return new AltosTelemetryFile(in);
+ } else if (file.getName().endsWith("eeprom")) {
+ return new AltosEepromFile(in);
+ } else {
+ String name = file.getName();
+ int dot = name.lastIndexOf('.');
+ String extension;
+
+ if (dot == -1)
+ throw new IOException(String.format("%s (Missing extension)", file.toString()));
+ else {
+ extension = name.substring(dot);
+ throw new IOException(String.format("%s (Invalid extension '%s')",
+ file.toString(),
+ extension));
+ }
+ }
+ }
+
}
diff --git a/altoslib/AltosStringInputStream.java b/altoslib/AltosStringInputStream.java
new file mode 100644
index 00000000..d574db24
--- /dev/null
+++ b/altoslib/AltosStringInputStream.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright © 2017 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ */
+
+package org.altusmetrum.altoslib_11;
+
+import java.util.*;
+import java.io.*;
+
+public class AltosStringInputStream extends InputStream {
+
+ String s;
+ int at;
+ int mark;
+
+ public int available() {
+ return s.length() - at;
+ }
+
+ public void mark(int read_limit) {
+ mark = at;
+ }
+
+ public boolean markSupported() {
+ return true;
+ }
+
+ public int read() {
+ if (at == s.length())
+ return -1;
+ return (int) s.charAt(at++);
+ }
+
+ public void reset() {
+ at = mark;
+ }
+
+ public long skip(long n) throws IOException {
+ if (n < 0) n = 0;
+
+ if (at + n > s.length())
+ n = s.length() - at;
+ at += n;
+ return n;
+ }
+
+ public AltosStringInputStream(String s) {
+ this.s = s;
+ this.at = 0;
+ }
+}
diff --git a/altoslib/AltosTelemetryFile.java b/altoslib/AltosTelemetryFile.java
index 40b9c9bf..077ef9c6 100644
--- a/altoslib/AltosTelemetryFile.java
+++ b/altoslib/AltosTelemetryFile.java
@@ -129,7 +129,7 @@ public class AltosTelemetryFile implements AltosRecordSet {
listener.finish();
}
- public AltosTelemetryFile(FileInputStream input) {
+ public AltosTelemetryFile(FileInputStream input) throws IOException {
telems = new AltosTelemetryIterable(input);
}
}
diff --git a/altoslib/AltosTelemetryIterable.java b/altoslib/AltosTelemetryIterable.java
index 402bbf4f..a752e24e 100644
--- a/altoslib/AltosTelemetryIterable.java
+++ b/altoslib/AltosTelemetryIterable.java
@@ -80,29 +80,25 @@ public class AltosTelemetryIterable implements Iterable<AltosTelemetry> {
return new AltosTelemetryOrderedIterator(telems);
}
- public AltosTelemetryIterable (FileInputStream input) {
+ public AltosTelemetryIterable (FileInputStream input) throws IOException {
telems = new TreeSet<AltosTelemetryOrdered> ();
tick = 0;
index = 0;
- try {
- for (;;) {
- String line = AltosLib.gets(input);
- if (line == null) {
+ for (;;) {
+ String line = AltosLib.gets(input);
+ if (line == null) {
+ break;
+ }
+ try {
+ AltosTelemetry telem = AltosTelemetry.parse(line);
+ if (telem == null)
break;
- }
- try {
- AltosTelemetry telem = AltosTelemetry.parse(line);
- if (telem == null)
- break;
- add(telem);
- } catch (ParseException pe) {
- System.out.printf("parse exception %s\n", pe.getMessage());
- } catch (AltosCRCException ce) {
- }
+ add(telem);
+ } catch (ParseException pe) {
+ System.out.printf("parse exception %s\n", pe.getMessage());
+ } catch (AltosCRCException ce) {
}
- } catch (IOException io) {
- System.out.printf("io exception\n");
}
}
}
diff --git a/altoslib/Makefile.am b/altoslib/Makefile.am
index ffa92783..1e26b724 100644
--- a/altoslib/Makefile.am
+++ b/altoslib/Makefile.am
@@ -107,6 +107,7 @@ altoslib_JAVA = \
AltosSensorTGPS.java \
AltosState.java \
AltosStateName.java \
+ AltosStringInputStream.java \
AltosTelemetry.java \
AltosTelemetryConfiguration.java \
AltosTelemetryCompanion.java \
diff --git a/altosui/AltosLanded.java b/altosui/AltosLanded.java
index a75d5a9f..de0d7425 100644
--- a/altosui/AltosLanded.java
+++ b/altosui/AltosLanded.java
@@ -124,10 +124,10 @@ public class AltosLanded extends AltosUIFlightTab implements ActionListener {
String filename = file.getName();
try {
AltosRecordSet record_set = null;
+ FileInputStream in = new FileInputStream(file);
if (filename.endsWith("eeprom")) {
- record_set = new AltosEepromRecordSet(new FileReader(file));
+ record_set = new AltosEepromRecordSet(in);
} else if (filename.endsWith("telem")) {
- FileInputStream in = new FileInputStream(file);
record_set = new AltosTelemetryFile(in);
} else {
throw new FileNotFoundException(filename);
diff --git a/altosui/AltosUI.java b/altosui/AltosUI.java
index b302b670..ac121512 100644
--- a/altosui/AltosUI.java
+++ b/altosui/AltosUI.java
@@ -372,21 +372,6 @@ public class AltosUI extends AltosUIFrame {
}
}
- static AltosRecordSet open_logfile(File file) {
- try {
- if (file.getName().endsWith("telem"))
- return new AltosTelemetryFile(new FileInputStream(file));
- else
- return new AltosEepromFile(new FileReader(file));
- } catch (FileNotFoundException fe) {
- System.out.printf("%s\n", fe.getMessage());
- return null;
- } catch (IOException ie) {
- System.out.printf("%s\n", ie.getMessage());
- return null;
- }
- }
-
static AltosWriter open_csv(File file) {
try {
return new AltosCSV(file);
@@ -405,6 +390,18 @@ public class AltosUI extends AltosUIFrame {
}
}
+ static AltosRecordSet record_set(File input) {
+ try {
+ return AltosLib.record_set(input);
+ } catch (IOException ie) {
+ String message = ie.getMessage();
+ if (message == null)
+ message = String.format("%s (I/O error)", input.toString());
+ System.err.printf("%s\n", message);
+ }
+ return null;
+ }
+
static final int process_none = 0;
static final int process_csv = 1;
static final int process_kml = 2;
@@ -413,7 +410,7 @@ public class AltosUI extends AltosUIFrame {
static final int process_summary = 5;
static boolean process_csv(File input) {
- AltosRecordSet set = open_logfile(input);
+ AltosRecordSet set = record_set(input);
if (set == null)
return false;
@@ -434,7 +431,7 @@ public class AltosUI extends AltosUIFrame {
}
static boolean process_kml(File input) {
- AltosRecordSet set = open_logfile(input);
+ AltosRecordSet set = record_set(input);
if (set == null)
return false;
@@ -455,27 +452,6 @@ public class AltosUI extends AltosUIFrame {
}
}
- static AltosRecordSet record_set(File file) {
- FileInputStream in;
- if (file.getName().endsWith("telem")) {
- try {
- in = new FileInputStream(file);
- return new AltosTelemetryFile(in);
- } catch (Exception e) {
- System.out.printf("Failed to open file '%s'\n", file);
- }
- } else {
-
- try {
- AltosEepromFile f = new AltosEepromFile(new FileReader(file));
- return f;
- } catch (Exception e) {
- System.out.printf("Failed to open file '%s'\n", file);
- }
- }
- return null;
- }
-
static AltosReplayReader replay_file(File file) {
AltosRecordSet set = record_set(file);
if (set == null)
diff --git a/altosuilib/AltosDataChooser.java b/altosuilib/AltosDataChooser.java
index c6d53a31..c26d3673 100644
--- a/altosuilib/AltosDataChooser.java
+++ b/altosuilib/AltosDataChooser.java
@@ -44,22 +44,8 @@ public class AltosDataChooser extends JFileChooser {
file = getSelectedFile();
if (file == null)
return null;
- filename = file.getName();
try {
- if (filename.endsWith("eeprom")) {
- FileReader in = new FileReader(file);
- return new AltosEepromFile(in);
- } else if (filename.endsWith("telem")) {
- FileInputStream in = new FileInputStream(file);
- return new AltosTelemetryFile(in);
- } else {
- throw new FileNotFoundException();
- }
- } catch (FileNotFoundException fe) {
- JOptionPane.showMessageDialog(frame,
- fe.getMessage(),
- "Cannot open file",
- JOptionPane.ERROR_MESSAGE);
+ return AltosLib.record_set(file);
} catch (IOException ie) {
JOptionPane.showMessageDialog(frame,
ie.getMessage(),
diff --git a/telegps/TeleGPS.java b/telegps/TeleGPS.java
index 5e500e02..e032726a 100644
--- a/telegps/TeleGPS.java
+++ b/telegps/TeleGPS.java
@@ -620,21 +620,10 @@ public class TeleGPS
}
static AltosRecordSet record_set(File file) {
- FileInputStream in;
- if (file.getName().endsWith("telem")) {
- try {
- in = new FileInputStream(file);
- return new AltosTelemetryFile(in);
- } catch (Exception e) {
- System.out.printf("Failed to open file '%s'\n", file);
- }
- } else {
-
- try {
- return new AltosEepromFile(new FileReader(file));
- } catch (Exception e) {
- System.out.printf("Failed to open file '%s'\n", file);
- }
+ try {
+ return AltosLib.record_set(file);
+ } catch (IOException ie) {
+ System.out.printf("%s\n", ie.getMessage());
}
return null;
}