altoslib: Create data file open helper in AltosLib
authorKeith Packard <keithp@keithp.com>
Sun, 28 May 2017 21:00:15 +0000 (14:00 -0700)
committerKeith Packard <keithp@keithp.com>
Sun, 28 May 2017 21:04:21 +0000 (14:04 -0700)
Use InputStream everywhere, instead of Reader.
Create private string input stream as java one is deprecated.

Signed-off-by: Keith Packard <keithp@keithp.com>
13 files changed:
altoslib/AltosEepromFile.java
altoslib/AltosEepromNew.java
altoslib/AltosEepromRecordSet.java
altoslib/AltosJson.java
altoslib/AltosLib.java
altoslib/AltosStringInputStream.java [new file with mode: 0644]
altoslib/AltosTelemetryFile.java
altoslib/AltosTelemetryIterable.java
altoslib/Makefile.am
altosui/AltosLanded.java
altosui/AltosUI.java
altosuilib/AltosDataChooser.java
telegps/TeleGPS.java

index df19877..463948b 100644 (file)
@@ -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);
        }
 
index 0da3df7..c373bff 100644 (file)
@@ -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) {
index 183cb9a..0c60c1a 100644 (file)
@@ -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));
        }
 }
index 9191be6..ce50b87 100644 (file)
@@ -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();
index 355c7a2..6c1729d 100644 (file)
@@ -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 (file)
index 0000000..d574db2
--- /dev/null
@@ -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;
+       }
+}
index 40b9c9b..077ef9c 100644 (file)
@@ -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);
        }
 }
index 402bbf4..a752e24 100644 (file)
@@ -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");
                }
        }
 }
index ffa9278..1e26b72 100644 (file)
@@ -107,6 +107,7 @@ altoslib_JAVA = \
        AltosSensorTGPS.java \
        AltosState.java \
        AltosStateName.java \
+       AltosStringInputStream.java \
        AltosTelemetry.java \
        AltosTelemetryConfiguration.java \
        AltosTelemetryCompanion.java \
index a75d5a9..de0d742 100644 (file)
@@ -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);
index b302b67..ac12151 100644 (file)
@@ -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)
index c6d53a3..c26d367 100644 (file)
@@ -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(),
index 5e500e0..e032726 100644 (file)
@@ -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;
        }