/// <summary>
 /// Creates a new iterator, buffering entries from the specified iterator
 /// </summary>
 public BufferingTermFreqIteratorWrapper(TermFreqIterator source)
 {
     this.comp = source.Comparator;
     BytesRef spare;
     int freqIndex = 0;
     while ((spare = source.Next()) != null)
     {
         entries.Append(spare);
         if (freqIndex >= freqs.Length)
         {
             freqs = ArrayUtil.Grow(freqs, freqs.Length + 1);
         }
         freqs[freqIndex++] = source.Weight;
     }
 }
        /// <summary>
        /// Creates a new iterator, buffering entries from the specified iterator
        /// </summary>
        public BufferingTermFreqIteratorWrapper(TermFreqIterator source)
        {
            this.comp = source.Comparator;
            BytesRef spare;
            int      freqIndex = 0;

            while ((spare = source.Next()) != null)
            {
                entries.Append(spare);
                if (freqIndex >= freqs.Length)
                {
                    freqs = ArrayUtil.Grow(freqs, freqs.Length + 1);
                }
                freqs[freqIndex++] = source.Weight;
            }
        }
        private OfflineSorter.ByteSequencesReader Sort()
        {
            string prefix    = this.GetType().Name;
            File   directory = OfflineSorter.DefaultTempDir();

            tempInput  = File.createTempFile(prefix, ".input", directory);
            tempSorted = File.createTempFile(prefix, ".sorted", directory);

            var  writer  = new OfflineSorter.ByteSequencesWriter(tempInput);
            bool success = false;

            try
            {
                BytesRef            spare;
                sbyte[]             buffer = new sbyte[0];
                ByteArrayDataOutput output = new ByteArrayDataOutput(buffer);

                while ((spare = source.Next()) != null)
                {
                    Encode(writer, output, buffer, spare, source.Weight);
                }
                writer.Dispose();
                (new OfflineSorter(tieBreakByCostComparator)).Sort(tempInput, tempSorted);
                OfflineSorter.ByteSequencesReader reader = new OfflineSorter.ByteSequencesReader(tempSorted);
                success = true;
                return(reader);
            }
            finally
            {
                if (success)
                {
                    IOUtils.Close(writer);
                }
                else
                {
                    try
                    {
                        IOUtils.CloseWhileHandlingException(writer);
                    }
                    finally
                    {
                        Close();
                    }
                }
            }
        }