Ensure PBF file is closed after reading completes

Fix the OsmosisReader class (--read-pbf) to correctly close the PBF
input stream on completion.
This commit is contained in:
Brett Henderson
2023-05-15 19:21:58 +10:00
parent ca999a2082
commit eaec695c79
2 changed files with 14 additions and 23 deletions

View File

@ -1,8 +1,9 @@
// This software is released into the Public Domain. See copying.txt for details.
package crosby.binary.osmosis;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import crosby.binary.file.BlockInputStream;
@ -16,17 +17,17 @@ import org.openstreetmap.osmosis.core.task.v0_6.Sink;
*/
public class OsmosisReader implements RunnableSource {
private Sink sink;
private File pbfFile;
private OsmosisBinaryParser parser;
private Sink sink;
/**
* Make a reader based on a target input stream.
* @param input The input stream to read from.
* @param pbfFile The PBF file to read from.
*/
public OsmosisReader(InputStream input) {
if (input == null) {
throw new Error("Null input");
}
this.input = input;
public OsmosisReader(File pbfFile) {
this.pbfFile = pbfFile;
parser = new OsmosisBinaryParser();
}
@ -40,8 +41,10 @@ public class OsmosisReader implements RunnableSource {
public void run() {
try {
sink.initialize(Collections.<String, Object>emptyMap());
(new BlockInputStream(input, parser)).process();
try (BlockInputStream blockInputStream = new BlockInputStream(new FileInputStream(pbfFile), parser)) {
blockInputStream.process();
}
} catch (IOException e) {
throw new OsmosisRuntimeException("Unable to process PBF stream", e);
@ -49,8 +52,4 @@ public class OsmosisReader implements RunnableSource {
sink.close();
}
}
/** Store the input stream we're using. */
InputStream input;
/** The binary parser object. */
OsmosisBinaryParser parser;
}

View File

@ -2,8 +2,6 @@
package crosby.binary.osmosis;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import org.openstreetmap.osmosis.core.pipeline.common.TaskConfiguration;
import org.openstreetmap.osmosis.core.pipeline.common.TaskManager;
@ -35,13 +33,7 @@ public class OsmosisReaderFactory extends TaskManagerFactory {
file = new File(fileName);
// Build the task object.
try {
task = new OsmosisReader(new FileInputStream(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
task = new OsmosisReader(file);
return new RunnableSourceManager(taskConfig.getId(), task, taskConfig
.getPipeArgs());