/**
   * Validates the structure of the merged csv files.
   *
   * @param mFiles List of MasterFiles to check
   * @return Returns true if the data structure of all given files matches. Throws an user exception
   *     (MergeException) otherwise.
   * @throws MergeException
   */
  public static boolean dataStructureMatch(List<MasterFile> mFiles) throws MergeException {
    String[] headers = null;
    String headerLine = "";
    String currFile = "";
    BufferedReader reader;
    FileInputStream fis;
    boolean firstRun = true;
    try {
      int maxHeaderSize = 0;
      for (MasterFile mFile : mFiles) {
        String fPath = mFile.getLocalAbsolutePath();
        currFile = fPath;

        File csvFile = new File(fPath);
        FileReader freader = new FileReader(csvFile);
        CSVReader csvreader = new CSVReader(freader, '\t', CSVWriter.NO_QUOTE_CHARACTER);
        headers = csvreader.readNext();
        if (headers != null) {
          if (headers.length != maxHeaderSize) {

            maxHeaderSize = headers.length;
            // get header line
            fis = new FileInputStream(fPath);
            reader = new BufferedReader(new InputStreamReader(fis));
            headerLine = reader.readLine();
            reader.close();
            if (!firstRun) {
              throw new MergeException(
                  "Data structure has changed within the given interval. File:"
                      + mFile.getName()
                      + "; Time tag of the file:"
                      + mFile.getCreated()
                      + ". The number of columns has changed.",
                  1);
            }
            firstRun = false;
          }

        } else {
          throw new MergeException("Error reading csv file header: " + currFile, 1);
        }
        csvreader.close();
      }
      if (maxHeaderSize == 0 || headerLine.equals("")) {
        throw new MergeException("Zero length header in csv file!", 1);
      }
      return true;

    } catch (FileNotFoundException ex) {
      throw new MergeException("CSV file not found. " + currFile + " " + ex.getMessage(), 1);
    } catch (IOException ex) {
      throw new MergeException("Error reading csv file: " + currFile + " " + ex.getMessage(), 1);
    }
  }