@Test
  public void encodesAndDecodes() throws Exception {
    List<String> original = newArrayList("file1", "file2", "file3");

    String encoded = FileEncoder.encode(original);
    List<String> decoded = FileEncoder.decode(encoded);

    assertThat(decoded, is(original));
  }
  private static void RunProgram() throws FileNotFoundException {
    long tempTime;

    totalStart = System.currentTimeMillis();

    tempTime = System.currentTimeMillis();

    CodeBuhk codeBook = new CodeBuhk(codeDictionary); // Creating the code book	

    System.out.printf(
        "Creating the code book: %d ms from %s",
        (System.currentTimeMillis() - tempTime), codeDictionary);

    /*
     *
     *  The above generates a code book from the 900 common words by
     *
     */

    tempTime = System.currentTimeMillis();

    DecodeBuhk decodeBook = new DecodeBuhk(codeBook); // Creating the decode book

    System.out.printf("\nCreating the decode book: %d ms", (System.currentTimeMillis() - tempTime));

    /*
     *
     *  The above generates a decode book from the code book.
     *  It takes the code book and for each keyset(word) picks a
     *  random code value for it and assigns it to the decodebook
     *
     */

    tempTime = System.currentTimeMillis();

    FileEncoder fileEnc = new FileEncoder(zimmerman, codeBook, decodeBook);

    fileEnc.EncodeFile();

    System.out.printf(
        "\nFile Encoder: %d ms", (System.currentTimeMillis() - tempTime)); // Encoding the File

    /*
     *
     *  Reads in a file, creates codes for the words not in the code book yet, then writes the encoded message to file
     *
     */

    tempTime = System.currentTimeMillis();

    FileDecoder fileDec = new FileDecoder(decodeBook, decodeName);

    fileDec.DecodeFile();

    System.out.printf(
        "\nFile Decoder: %d ms", (System.currentTimeMillis() - tempTime)); // Decoding the File

    /*
     *
     *  Uses the decode book as a look up while reading in the 5 digit integers and decoding them to a file names "Decode.txt"
     *
     */

    System.out.printf("\nTotal run time: %d ms \n", (System.currentTimeMillis() - totalStart));

    /*
     *
     * ###########									###########
     * ###########		E X T R A  --- S T U F F	###########
     * ###########									###########
     *
     */

    /*
     	tempTime = System.currentTimeMillis();

    	BookWriter book = new BookWriter(codeBook);
    	book.PrintCodeBook();

    	System.out.printf("\nBookWriter: %d ms",(System.currentTimeMillis() - tempTime)); // Encoding the File

     // Prints out the code book passed to it.
     // Can be either the code or decode book

    */

    /*
    	Serializer fun = new Serializer(zimmerman);
    	fun.SerThisFile();
    	fun.DerSerThisFile();

     // Creates a serialized version of the file sent to it.
     // Threw this method in to show that I've learned to
     // do it through the learning process with this project.

    */

    /*
    	try
    	{
    		 BasicOneTimePass vPass = new BasicOneTimePass();
    		 vPass.basicEncryption(zimmerman);
    	}
    	catch(Exception e)
    	{

    	}

    //  Does a very basic one time pass on whatever file is passed to it

    */

    /*

    Dupe dupeFind = new Dupe();

    try
    {
    	dupeFind.findDupe(codeDictionary);
    }
    catch (IOException e)
    {
    	e.printStackTrace();
    }

       // Checks a file passed to to it for duplicates.

       */

  } // RunProgram
Пример #3
0
  public static void main(String args[]) {
    final int DATA_OR_DEFINITION_SEARCH_COUNT = 2;
    String in = "";
    String out = "";
    ArrayList<String> mesgDefinitionsToOutput = new ArrayList<String>();
    ArrayList<String> dataMessagesToOutput = new ArrayList<String>();
    boolean fitToCsv = false;
    boolean csvToFit = false;
    boolean test = false;
    boolean checkIntegrity = false;
    boolean showInvalidValues = false;
    boolean invalidsToEmpty = false;
    boolean hideUnknownData = false;
    int nextArgumentDefinition = 0;
    int nextArgumentData = 0;
    int numUnknownFields = 0;
    int numUnknownMesgs = 0;

    int arg = 0;

    System.out.printf(
        "FIT CSV Tool - Protocol %d.%d Profile %.2f %s\n",
        Fit.PROTOCOL_VERSION_MAJOR,
        Fit.PROTOCOL_VERSION_MINOR,
        Fit.PROFILE_VERSION / 100.0,
        Fit.PROFILE_TYPE);

    while (arg < args.length) {
      if (args[arg].equals("-b")) {
        if ((args.length - arg) < 3) {
          printUsage();
          return;
        }

        fitToCsv = true;
        in = args[arg + 1];
        out = args[arg + 2];

        arg += 2;
      } else if (args[arg].equals("-c")) {
        if ((args.length - arg) < 3) {
          printUsage();
          return;
        }

        csvToFit = true;
        in = args[arg + 1];
        out = args[arg + 2];

        arg += 2;
      } else if (args[arg].equals("-t")) {
        test = true;
      } else if (args[arg].equals("-d")) {
        Fit.debug = true;
        test = true;
      } else if (args[arg].equals("-i")) {
        checkIntegrity = true;
      } else if (args[arg].equals("--defn")) {
        nextArgumentDefinition = DATA_OR_DEFINITION_SEARCH_COUNT;
      } else if (args[arg].equals("--data")) {
        nextArgumentData = DATA_OR_DEFINITION_SEARCH_COUNT;
      } else if (args[arg].charAt(0) != '-') {

        if (nextArgumentDefinition > 0) {
          mesgDefinitionsToOutput =
              new ArrayList<String>(Arrays.asList(args[arg].toLowerCase().split(",")));
        } else if (nextArgumentData > 0) {
          dataMessagesToOutput =
              new ArrayList<String>(Arrays.asList(args[arg].toLowerCase().split(",")));
        } else {
          in = args[arg];
          if (in.endsWith(".fit")) {
            fitToCsv = true;
            out = in.substring(0, in.length() - 4) + ".csv";
          } else if (in.endsWith(".csv")) {
            csvToFit = true;
            out = in.substring(0, in.length() - 4) + ".fit";
          }
        }
      } else if (args[arg].equals("-s")) {
        showInvalidValues = true;
      } else if (args[arg].equals("-se")) {
        showInvalidValues = true;
        invalidsToEmpty = true;
      } else if (args[arg].equals("-u")) {
        hideUnknownData = true;
      }

      if (nextArgumentDefinition > 0) {
        nextArgumentDefinition--;
        if ((nextArgumentDefinition == 0) && (mesgDefinitionsToOutput.isEmpty())) {
          System.out.println(
              "No mesg definitions defined for --defn option.  Use 'none' if no definitions are desired.");
          return;
        }
      }
      if (nextArgumentData > 0) {
        nextArgumentData--;
        if ((nextArgumentData == 0) && (dataMessagesToOutput.isEmpty())) {
          System.out.println(
              "No data messages defined for --data option.  Use 'none' if no data is desired.");
          return;
        }
      }
      arg++;
    }

    if (fitToCsv) {
      if ((out.length() >= 4)
          && (out.substring(out.length() - 4, out.length()).compareTo(".csv") == 0))
        out = out.substring(0, out.length() - 4); // Remove .csv extension.

      if (checkIntegrity) {
        try {
          if (!Decode.checkIntegrity((InputStream) new FileInputStream(in))) {
            if (!Decode.getInvalidDataSize())
              throw new RuntimeException("FIT file integrity failure.");
            else {
              System.out.println("FIT file integrity failure. Invalid file size in header.");
              System.out.println("Trying to continue...");
            }
          }
        } catch (java.io.IOException e) {
          throw new RuntimeException(e);
        }
      }

      if (test) {
        Tests tests = new Tests();
        System.out.println("Running FIT verification tests...");
        if (tests.run(in)) System.out.println("Passed FIT verification.");
        else System.out.println("Failed FIT verification.");
      }

      try {
        Decode decode = new Decode();
        MesgCSVWriter mesgWriter = new MesgCSVWriter(out + ".csv");
        FileInputStream fileInputStream = new FileInputStream(in);
        if (showInvalidValues == true) decode.showInvalidValues();

        MesgFilter mesgFilter = new MesgFilter();
        mesgFilter.setMesgDefinitionsToOutput(mesgDefinitionsToOutput);
        mesgFilter.setDataMessagesToOutput(dataMessagesToOutput);

        MesgDataCSVWriter dataMesgWriter = new MesgDataCSVWriter(out + "_data.csv");
        if (invalidsToEmpty) {
          mesgWriter.showInvalidsAsEmptyCells();
          dataMesgWriter.showInvalidsAsEmptyCells();
        }

        if (hideUnknownData) {
          mesgWriter.hideUnknownData();
          dataMesgWriter.hideUnknownData();
        }

        mesgFilter.addListener((MesgDefinitionListener) mesgWriter);
        mesgFilter.addListener((MesgListener) mesgWriter);
        mesgFilter.addListener((MesgListener) dataMesgWriter);

        decode.addListener((MesgDefinitionListener) mesgFilter);
        decode.addListener((MesgListener) mesgFilter);

        while (fileInputStream.available()
            > 0) { // Try to read a file while more data is available.
          try {
            decode.read((InputStream) fileInputStream);
            decode.nextFile(); // Initialize to read next file (if any).
          } catch (FitRuntimeException e) {
            if (decode.getInvalidDataSize()) continue;
          }
        }

        mesgWriter.close();
        dataMesgWriter.close();

        numUnknownFields = mesgWriter.getNumUnknownFields();
        numUnknownMesgs = mesgWriter.getNumUnknownMesgs();

      } catch (java.io.IOException e) {
        throw new RuntimeException(e);
      }

      if (hideUnknownData)
        System.out.printf(
            "Hid %d unknown field(s) and %d unknown message(s).\n",
            numUnknownFields, numUnknownMesgs);
      System.out.printf("FIT binary file %s decoded to %s*.csv files.\n", in, out);
    } else if (csvToFit) {
      try {
        FileEncoder encoder = new FileEncoder(new File(out));
        if (!CSVReader.read((InputStream) new FileInputStream(in), encoder, encoder))
          throw new RuntimeException("FIT encoding error.");
        encoder.close();

        System.out.printf("%s encoded into FIT binary file %s.\n", in, out);
      } catch (java.io.IOException e) {
        throw new RuntimeException(e);
      }
    } else {
      printUsage();
    }
  }