Use InputStream everywhere, instead of Reader.
Create private string input stream as java one is deprecated.
Signed-off-by: Keith Packard <keithp@keithp.com>
out.printf("%s\n", set.eeprom.toString());
}
- public AltosEepromFile(Reader input) throws IOException {
+ public AltosEepromFile(InputStream input) throws IOException {
set = new AltosEepromRecordSet(input);
}
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+");
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);
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 */
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");
}
}
/*
* 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) {
}
}
- public AltosEepromRecordSet(Reader input) throws IOException {
+ public AltosEepromRecordSet(InputStream input) throws IOException {
this(new AltosEepromNew(input));
}
}
* Lexer for json
*/
class JsonLexer extends JsonUtil {
- Reader f;
+ InputStream f;
int line;
int ungot = -2;
StringBuffer pending_token;
}
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;
lexer = new JsonLexer(s);
}
- JsonParse(Reader f) {
+ JsonParse(InputStream f) {
lexer = new JsonLexer(f);
}
}
}
}
- public static AltosJson fromReader(Reader f) {
+ public static AltosJson fromInputStream(InputStream f) {
JsonParse parse = new JsonParse(f);
try {
return parse.parse();
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));
+ }
+ }
+ }
+
}
--- /dev/null
+/*
+ * 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;
+ }
+}
listener.finish();
}
- public AltosTelemetryFile(FileInputStream input) {
+ public AltosTelemetryFile(FileInputStream input) throws IOException {
telems = new AltosTelemetryIterable(input);
}
}
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");
}
}
}
AltosSensorTGPS.java \
AltosState.java \
AltosStateName.java \
+ AltosStringInputStream.java \
AltosTelemetry.java \
AltosTelemetryConfiguration.java \
AltosTelemetryCompanion.java \
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);
}
}
- 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);
}
}
+ 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;
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;
}
static boolean process_kml(File input) {
- AltosRecordSet set = open_logfile(input);
+ AltosRecordSet set = record_set(input);
if (set == null)
return false;
}
}
- 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)
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(),
}
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;
}