@SuppressWarnings("unchecked")
  public void write(List<BaseRoute> routes, MultipleRoutesFormat format, File target)
      throws IOException {
    log.info(
        "Writing '"
            + format.getName()
            + "' with with "
            + routes.size()
            + " routes and "
            + getPositionCounts(routes)
            + " positions");

    List<BaseRoute> routesToWrite = new ArrayList<>(routes.size());
    for (BaseRoute route : routes) {
      BaseRoute routeToWrite = asFormat(route, format);
      commentRoute(routeToWrite);
      preprocessRoute(routeToWrite, format, false, null);
      routesToWrite.add(routeToWrite);
      postProcessRoute(routeToWrite, format, false);
    }

    try (OutputStream outputStream = new FileOutputStream(target)) {
      format.write(routesToWrite, outputStream);
      log.info("Wrote '" + target.getAbsolutePath() + "'");
    }
  }
  @SuppressWarnings("unchecked")
  private void write(
      BaseRoute route,
      NavigationFormat format,
      boolean duplicateFirstPosition,
      boolean ignoreMaximumPositionCount,
      ParserCallback parserCallback,
      OutputStream... targets)
      throws IOException {
    log.info(
        "Writing '"
            + format.getName()
            + "' position lists with 1 route and "
            + route.getPositionCount()
            + " positions");

    BaseRoute routeToWrite = asFormat(route, format);
    commentRoute(routeToWrite);
    preprocessRoute(routeToWrite, format, duplicateFirstPosition, parserCallback);

    int positionsToWrite = routeToWrite.getPositionCount();
    int writeInOneChunk = format.getMaximumPositionCount();

    // check if the positions to write fit within the given files
    if (positionsToWrite > targets.length * writeInOneChunk) {
      if (ignoreMaximumPositionCount) writeInOneChunk = positionsToWrite;
      else
        throw new IOException(
            "Found "
                + positionsToWrite
                + " positions, "
                + format.getName()
                + " format may only contain "
                + writeInOneChunk
                + " positions in one position list.");
    }

    int startIndex = 0;
    for (int i = 0; i < targets.length; i++) {
      OutputStream target = targets[i];
      int endIndex = min(startIndex + writeInOneChunk, positionsToWrite);
      renameRoute(route, routeToWrite, startIndex, endIndex, i, targets);
      format.write(routeToWrite, target, startIndex, endIndex);
      log.info("Wrote position list from " + startIndex + " to " + endIndex);
      startIndex += writeInOneChunk;
    }

    postProcessRoute(routeToWrite, format, duplicateFirstPosition);
  }