private ByteSequencesReader sort() throws IOException {
    String prefix = getClass().getSimpleName();
    Path directory = OfflineSorter.getDefaultTempDir();
    tempInput = Files.createTempFile(directory, prefix, ".input");
    tempSorted = Files.createTempFile(directory, prefix, ".sorted");

    final OfflineSorter.ByteSequencesWriter writer =
        new OfflineSorter.ByteSequencesWriter(tempInput);
    boolean success = false;
    try {
      BytesRef spare;
      byte[] buffer = new byte[0];
      ByteArrayDataOutput output = new ByteArrayDataOutput(buffer);

      while ((spare = source.next()) != null) {
        encode(writer, output, buffer, spare, source.payload(), source.contexts(), source.weight());
      }
      writer.close();
      new OfflineSorter(tieBreakByCostComparator).sort(tempInput, tempSorted);
      ByteSequencesReader reader = new OfflineSorter.ByteSequencesReader(tempSorted);
      success = true;
      return reader;

    } finally {
      if (success) {
        IOUtils.close(writer);
      } else {
        try {
          IOUtils.closeWhileHandlingException(writer);
        } finally {
          close();
        }
      }
    }
  }
 /** Creates a new sorted wrapper, sorting by BytesRef (ascending) then cost (ascending). */
 public SortedInputIterator(InputIterator source, Comparator<BytesRef> comparator)
     throws IOException {
   this.hasPayloads = source.hasPayloads();
   this.hasContexts = source.hasContexts();
   this.source = source;
   this.comparator = comparator;
   this.reader = sort();
 }
 /** Creates a new iterator, buffering entries from the specified iterator */
 public BufferedInputIterator(InputIterator source) throws IOException {
   BytesRef spare;
   int freqIndex = 0;
   hasPayloads = source.hasPayloads();
   hasContexts = source.hasContexts();
   while ((spare = source.next()) != null) {
     entries.append(spare);
     if (hasPayloads) {
       payloads.append(source.payload());
     }
     if (hasContexts) {
       contextSets.add(source.contexts());
     }
     if (freqIndex >= freqs.length) {
       freqs = ArrayUtil.grow(freqs, freqs.length + 1);
     }
     freqs[freqIndex++] = source.weight();
   }
 }