1. Simon Allcorn
  2. carstain

Commits

Simon Allcorn  committed 6310175

made reader more effient

Comments (0)

Files changed (1)

File src/main/java/org/allcorn/carstain/read/CarReader.java Modified

View file
  • Ignore whitespace
  • Hide word diff
 package org.allcorn.carstain.read;
 
-import java.io.BufferedReader;
-import java.io.FileReader;
-import java.io.IOException;
+import java.io.*;
 import java.util.HashMap;
 import java.util.Set;
 import java.util.regex.Pattern;
  *
  */
 public class CarReader implements Runnable {
-  String myPipeFile;
   Core coreRef;
   Document carControlXML;
   Logger log = Logger.getLogger(this.getClass());
+  InputStreamReader pipeReader;
+  private static final char newLine = '\n';
+  StringBuilder nextCommand;
+  char nextChar;
 
   /*this will store all the car commands, messages and regexp to
    *look them up. Format is:
  * @param coreRef the object that has implemented org.allcorn.carstian.read.CarResponder
  */
 public CarReader(String myPipeFile, Document carControlXML, Core coreRef) {
-  this.myPipeFile = myPipeFile;
   this.coreRef = coreRef;
   this.carControlXML = carControlXML;
+  try {
+    File file = new File(myPipeFile);
+    pipeReader = new InputStreamReader(new FileInputStream(file));
+  } catch (FileNotFoundException e) {
+    log.error(e.toString());
+    e.printStackTrace();
+  }
+  nextCommand = new StringBuilder();
   log.debug("CarReader constructor complete");
 }
 
   initCarControlXML();
 
   BufferedReader reader = null;
-  String[] busAndCommand = {""};
   String bus;
   String command;
   CarCommandQueue thisQueue;
   Set<Pattern> patterns;
 
-  try {
-    reader = new BufferedReader(new FileReader(myPipeFile));
     String rawCommand;
 
-    while (( rawCommand = reader.readLine()) != null) {
+    while (true) {
+      rawCommand = getRawCommand();
       log.trace("Raw canbus cmd = [" + rawCommand + "]");
 
       bus = rawCommand.substring(0,busIdentifierLength);
         log.trace("bus [" + bus + "] not found in config");
       }
     }
+}
 
-    reader.close();
+private String getRawCommand() {
 
+  nextCommand.setLength(0);
+
+  try {
+    while( (nextChar = (char) pipeReader.read()) != -1 ) {
+      if( nextChar == newLine) {
+        break;
+      }
+      nextCommand.append(nextChar);
+    }
   } catch (IOException e) {
-        e.printStackTrace();
+    log.error(e.toString());
+    e.printStackTrace();
   }
 
+  return nextCommand.toString();
 }
 
 /**