Index: lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/Sort.java =================================================================== --- lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/Sort.java (revision 1359952) +++ lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/Sort.java (working copy) @@ -193,50 +193,60 @@ output.delete(); ArrayList merges = new ArrayList(); - ByteSequencesReader is = new ByteSequencesReader(input); - boolean success = false; + boolean success2 = false; try { - int lines = 0; - while ((lines = readPartition(is)) > 0) { - merges.add(sortPartition(lines)); - sortInfo.tempMergeFiles++; - sortInfo.lines += lines; + ByteSequencesReader is = new ByteSequencesReader(input); + boolean success = false; + try { + int lines = 0; + while ((lines = readPartition(is)) > 0) { + merges.add(sortPartition(lines)); + sortInfo.tempMergeFiles++; + sortInfo.lines += lines; - // Handle intermediate merges. - if (merges.size() == maxTempFiles) { - File intermediate = File.createTempFile("sort", "intermediate", tempDirectory); - mergePartitions(merges, intermediate); - for (File file : merges) { - file.delete(); + // Handle intermediate merges. + if (merges.size() == maxTempFiles) { + File intermediate = File.createTempFile("sort", "intermediate", tempDirectory); + try { + mergePartitions(merges, intermediate); + } finally { + for (File file : merges) { + file.delete(); + } + merges.clear(); + merges.add(intermediate); + } + sortInfo.tempMergeFiles++; } - merges.clear(); - merges.add(intermediate); - sortInfo.tempMergeFiles++; } + success = true; + } finally { + if (success) + IOUtils.close(is); + else + IOUtils.closeWhileHandlingException(is); } - success = true; - } finally { - if (success) - IOUtils.close(is); - else - IOUtils.closeWhileHandlingException(is); - } - // One partition, try to rename or copy if unsuccessful. - if (merges.size() == 1) { - File single = merges.get(0); - // If simple rename doesn't work this means the output is - // on a different volume or something. Copy the input then. - if (!single.renameTo(output)) { - copy(single, output); - single.delete(); + // One partition, try to rename or copy if unsuccessful. + if (merges.size() == 1) { + File single = merges.get(0); + // If simple rename doesn't work this means the output is + // on a different volume or something. Copy the input then. + if (!single.renameTo(output)) { + copy(single, output); + } + } else { + // otherwise merge the partitions with a priority queue. + mergePartitions(merges, output); } - } else { - // otherwise merge the partitions with a priority queue. - mergePartitions(merges, output); + success2 = true; + } finally { for (File file : merges) { file.delete(); } + if (!success2) { + output.delete(); + } } sortInfo.totalTime = (System.currentTimeMillis() - sortInfo.totalTime);