X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=altoslib%2FAltosEepromNew.java;h=c373bff36170f265d40ffef8b92301c9a36b2cdb;hb=0e586ab3604bb695c36ae86c95f533197522fc65;hp=c8f44509d19e8a33b9dce623a01d506bc59c2909;hpb=e67a5c6ffa7174d66e985483fab4bf52ccaea5ca;p=fw%2Faltos diff --git a/altoslib/AltosEepromNew.java b/altoslib/AltosEepromNew.java index c8f44509..c373bff3 100644 --- a/altoslib/AltosEepromNew.java +++ b/altoslib/AltosEepromNew.java @@ -57,7 +57,8 @@ public class AltosEepromNew { if (config_data == null) config_data = new AltosConfigData(); - if (config_data.log_format == AltosLib.MISSING) { + if (config_data.log_format == AltosLib.AO_LOG_FORMAT_UNKNOWN) { + config_data.log_format = AltosLib.AO_LOG_FORMAT_FULL; if (config_data.product != null) { if (config_data.product.startsWith("TeleMetrum")) config_data.log_format = AltosLib.AO_LOG_FORMAT_FULL; @@ -69,10 +70,6 @@ public class AltosEepromNew { return config_data; } - public void reset_config_data() { - config_data = null; - } - private void write_config(Writer w) throws IOException { config.write(w, 0, true); w.append('\n'); @@ -96,60 +93,68 @@ 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; } - static private byte[] byte_list_to_array(List bytes) { - byte[] data = new byte[bytes.size()]; - int i = 0; + private String read_line(InputStream stream) throws IOException { + StringBuffer buffer = null; + int c; - for (Byte b : bytes) { - data[i++] = b; + 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); } - return data; } - private boolean read_data(Reader r) throws IOException { - BufferedReader br = new BufferedReader(r); - String s; + private boolean read_data(InputStream stream) throws IOException { + String s; data = new ArrayList(); - while ((s = br.readLine()) != null) { + while ((s = read_line(stream)) != null) { + String[] tokens = s.split("\\s+"); for (int i = 0; i < tokens.length; i++) { - try { - data.add((byte) AltosLib.fromhex(tokens[i])); - } catch (NumberFormatException e) { - throw new IOException(e.toString()); + if (tokens[i].length() > 0) { + try { + data.add((byte) AltosLib.fromhex(tokens[i])); + } catch (NumberFormatException e) { + throw new IOException(e.toString()); + } } } } 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); @@ -158,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(); - 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 */ @@ -175,7 +180,8 @@ public class AltosEepromNew { int start = data.size(); if (config_data().log_format != AltosLib.AO_LOG_FORMAT_TINY) { - data.add((byte) tokens[0].codePointAt(0)); + byte cmd = (byte) tokens[0].codePointAt(0); + data.add(cmd); int time = AltosLib.fromhex(tokens[1]); @@ -216,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"); } } @@ -262,12 +268,22 @@ 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 data) { + this.config = config; + this.data = data; + } + + public AltosEepromNew(AltosConfigData config_data, ArrayList data) { + this.config = new AltosJson(config_data); + this.data = data; } public AltosEepromNew() {