Skip to content

Commit b4b976f

Browse files
committed
Fixed issue #8
1 parent e15279a commit b4b976f

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ which would read text from file "input.txt", sort using about 20 megs of heap (n
4343

4444
Project jar is packaged such that it can be used as a primitive 'sort' tool like so:
4545

46-
java -jar java-merge-sort-0.5.0.jar [input-file]
46+
java -jar java-merge-sort-0.8.0.jar [input-file]
4747

4848
where sorted output gets printed to `stdout`; and argument is optional (if missing, reads input from stdout).
4949
(implementation note: this uses standard `TextFileSorter` mentioned above)

release-notes/VERSION

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
Project: java-merge-sort
22
License: Apache License 2.0
3-
Version: 0.8.0
4-
Release date: 24-Jul-2013
3+
Version: 0.8.1
4+
Release date: xx-Aug-2013
55

6-
* [Issue#6]: Incorrect handling of '\r'
7-
(reported, patch contributed by ndikan@github.bom)
6+
#8: Sorter._merge() not closing streams correctly
7+
(reported by tjoneslo@github)
88

99
------------------------------------------------------------------------
1010
=== History: ===
1111
------------------------------------------------------------------------
1212

13+
0.8.0 (24-Jul-2013)
14+
15+
* [Issue#6]: Incorrect handling of '\r'
16+
(reported, patch contributed by [email protected])
17+
1318
0.7.1 (29-May-2012)
1419

1520
* [Issue#5]: Unnecessary object retention, leading to too high memory usage

src/main/java/com/fasterxml/sort/Merger.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ protected static class PairwiseMerger<T>
6161

6262
protected T _data1;
6363
protected T _data2;
64+
65+
protected boolean _closed;
6466

6567
public PairwiseMerger(Comparator<T> comparator,
6668
DataReader<T> reader1, DataReader<T> reader2)
@@ -78,6 +80,8 @@ public T readNext() throws IOException
7880
{
7981
if (_data1 == null) {
8082
if (_data2 == null) {
83+
// [Issue#8]: Should auto-close merged input when there is no more data
84+
close();
8185
return null;
8286
}
8387
T result = _data2;
@@ -108,9 +112,13 @@ public int estimateSizeInBytes(T item) {
108112
}
109113

110114
@Override
111-
public void close() throws IOException {
112-
_reader1.close();
113-
_reader2.close();
115+
public void close() throws IOException
116+
{
117+
if (!_closed) {
118+
_reader1.close();
119+
_reader2.close();
120+
_closed = true;
121+
}
114122
}
115123
}
116124
}

src/main/java/com/fasterxml/sort/Sorter.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -326,18 +326,28 @@ protected void _presort(DataReader<T> inputReader, SegmentedBuffer buffer, T nex
326326
} while (nextValue != null);
327327
}
328328

329+
@SuppressWarnings("resource")
329330
protected File _writePresorted(Object[] items) throws IOException
330331
{
331332
File tmp = _config.getTempFileProvider().provide();
332333
@SuppressWarnings("unchecked")
333334
DataWriter<Object> writer = (DataWriter<Object>) _writerFactory.constructWriter(new FileOutputStream(tmp));
334-
++_presortFileCount;
335-
for (int i = 0, end = items.length; i < end; ++i) {
336-
writer.writeEntry(items[i]);
337-
// to further reduce transient mem usage, clear out the ref
338-
items[i] = null;
335+
boolean closed = false;
336+
try {
337+
++_presortFileCount;
338+
for (int i = 0, end = items.length; i < end; ++i) {
339+
writer.writeEntry(items[i]);
340+
// to further reduce transient mem usage, clear out the ref
341+
items[i] = null;
342+
}
343+
closed = true;
344+
writer.close();
345+
} finally {
346+
if (!closed) {
347+
// better swallow since most likely we are getting an exception already...
348+
try { writer.close(); } catch (IOException e) { }
349+
}
339350
}
340-
writer.close();
341351
return tmp;
342352
}
343353

@@ -385,6 +395,7 @@ protected void _writeAll(DataWriter<T> resultWriter, Object[] items)
385395
}
386396
}
387397

398+
@SuppressWarnings("resource")
388399
protected File _merge(List<File> inputs)
389400
throws IOException
390401
{
@@ -397,17 +408,23 @@ protected void _merge(List<File> inputs, DataWriter<T> writer)
397408
throws IOException
398409
{
399410
ArrayList<DataReader<T>> readers = new ArrayList<DataReader<T>>(inputs.size());
411+
DataReader<T> merger = null;
400412
try {
401413
for (File mergedInput : inputs) {
402414
readers.add(_readerFactory.constructReader(new FileInputStream(mergedInput)));
403415
}
404-
DataReader<T> merger = Merger.mergedReader(_comparator, readers);
416+
merger = Merger.mergedReader(_comparator, readers);
405417
T value;
406418
while ((value = merger.readNext()) != null) {
407419
writer.writeEntry(value);
408420
}
421+
merger.close(); // usually not necessary (reader should close on eof) but...
422+
merger = null;
409423
writer.close();
410424
} finally {
425+
if (merger != null) {
426+
try { merger.close(); } catch (IOException e) { }
427+
}
411428
for (File input : inputs) {
412429
input.delete();
413430
}

0 commit comments

Comments
 (0)