/**
   * DOC amaumont Comment method "beforeLoopFind".
   *
   * @throws IOException
   */
  private void beforeLoopFind() throws IOException {
    // File file = new File(workDirectory + "TEMP_" + count);

    // DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new
    // FileOutputStream(file)));
    int numFiles = files.size();
    diss = new ArrayList<DataInputStream>();
    datas = new ArrayList<DataContainer>();
    fileLengths = new ArrayList<Long>();

    boolean someFileStillHasRows = false;

    for (int i = 0; i < numFiles; i++) {
      diss.add(new DataInputStream(new BufferedInputStream(new FileInputStream(files.get(i)))));
      fileLengths.add(files.get(i).length());
      DataContainer dc = new DataContainer();
      DataInputStream dis = diss.get(i);
      byte[] bytes = new byte[dis.readInt()];
      dis.read(bytes);
      dc.object = iLightSerializable.createInstance(bytes);
      if (!someFileStillHasRows) {
        someFileStillHasRows = true;
      }
      datas.add(dc);
    }
  }
  /**
   * DOC amaumont Comment method "findNextData".
   *
   * @throws IOException
   */
  private void findNextData() throws IOException {
    DataContainer min = null;
    int minIndex = 0;
    DataContainer dataContainer = datas.get(0);

    if (dataContainer.object != null) {
      min = dataContainer;
      minIndex = 0;
    } else {
      min = null;
      minIndex = -1;
    }

    // check which one is min
    for (int i = 1; i < datas.size(); i++) {
      dataContainer = datas.get(i);

      if (min != null) {
        if (dataContainer.object != null
            && ((Comparable) (dataContainer.object)).compareTo(min.object) < 0) {
          minIndex = i;
          min = dataContainer;
        }
      } else {
        if (dataContainer.object != null) {
          min = dataContainer;
          minIndex = i;
        }
      }
    }

    if (minIndex < 0) {
      someFileStillHasRows = false;
    } else {
      // write to the sorted file
      // write(min.data, dos);

      currentObject = (V) min.object;

      min.reset();

      // get another data from the file
      DataInputStream dis = diss.get(minIndex);
      if (dis.available() > 0) {
        byte[] bytes = new byte[dis.readInt()];
        dis.read(bytes);
        min.object = iLightSerializable.createInstance(bytes);
        min.cursorPosition += 4 + bytes.length;
      }
      // check if one still has data
      someFileStillHasRows = false;
      for (int i = 0; i < datas.size(); i++) {
        if (datas.get(i).object != null) {
          someFileStillHasRows = true;
          break;
        }
      }
    }
  }