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 df19877b643dda6c5dbaf19c5fd7f8c642181c6f..463948b11b20e769510b296994a2e0dbd3afc58f 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 0da3df71c98740f282f785894669dc1f24786cbe..c373bff36170f265d40ffef8b92301c9a36b2cdb 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 183cb9aecc0f4deca8cd269d40d01df6a8639324..0c60c1a5ab5b8db2196a626b3a22a822b0b55748 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 9191be68004d6297d4fea25ae61747669478816c..ce50b872f13c6c4bc9b68ccd9dcff4b352396171 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 355c7a27a9c337f71fca43e0dbfee9f58399b709..6c1729dfc03a32a51f64f3d8fbf2eabf092ec652 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 40b9c9bfb85f1f25487112bae905f342bf197a7d..077ef9c6336560084fee5b37232939467b8c55a1 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 402bbf4f4276b89992fc092a0abd6d1faf257616..a752e24e4bfd709125ab55ce5e53ebc927bcd70b 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 ffa9278320b3f8cc68097a6b91fbff193de73311..1e26b724f9c7335fd92e709c1f65866fd045e293 100644 (file)
@@ -107,6 +107,7 @@ altoslib_JAVA = \
        AltosSensorTGPS.java \
        AltosState.java \
        AltosStateName.java \
+       AltosStringInputStream.java \
        AltosTelemetry.java \
        AltosTelemetryConfiguration.java \
        AltosTelemetryCompanion.java \
index a75d5a9f504786cc09d7df89fb6668ed473396e6..de0d7425ea2e327a605aeb0fd02ce1d0fa1cf9fe 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 b302b670677a73fb533eb00c01ad34db656b8438..ac121512c3a544eef1b8a9429ee7a0211bf61393 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 c6d53a311eaf37c8110d73b9e38c6e6251878208..c26d367301ad9a089be57cf99a9afe72dbc52d1d 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 5e500e0259a74623e6d934d2e85b876bc0107d2a..e032726a1dfadd268e8fd96582bfe386fca505c0 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;
        }