Safe image exporting

This commit is contained in:
SiboVG 2023-08-23 20:04:53 +02:00
parent 04df65cdfb
commit 232b338fb9
2 changed files with 22 additions and 9 deletions

View File

@ -50,14 +50,19 @@ public class ResourceDecalImage implements DecalImage {
@Override
public void exportImage(File file) throws IOException {
InputStream is;
is = getBytes();
InputStream is = getBytes();
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
FileUtils.copy(is, os);
if (is == null) {
return;
}
is.close();
os.close();
try {
FileUtils.copy(is, os);
} finally {
is.close();
os.close();
}
}
@Override

View File

@ -35,6 +35,8 @@ import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.rocketcomponent.Transition;
import net.sf.openrocket.rocketcomponent.TubeFinSet;
import net.sf.openrocket.util.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileOutputStream;
@ -68,6 +70,8 @@ public class OBJExporterFactory {
private final OBJExportOptions options;
private final File file;
private static final Logger log = LoggerFactory.getLogger(OBJExporterFactory.class);
// The different exporters for each component
private static final Map<Class<? extends RocketComponent>, ExporterFactory<?>> EXPORTER_MAP = Map.of(
BodyTube.class, (ExporterFactory<BodyTube>) BodyTubeExporter::new,
@ -188,10 +192,14 @@ public class OBJExporterFactory {
if (options.isExportAppearance()) {
String mtlFilePath = FileUtils.removeExtension(filePath) + ".mtl";
List<DefaultMtl> mtls = materials.get(obj);
try (OutputStream mtlOutputStream = new FileOutputStream(mtlFilePath, false)) {
DefaultMtlWriter.write(mtls, mtlOutputStream);
} catch (IOException e) {
throw new RuntimeException(e);
if (mtls != null) {
try (OutputStream mtlOutputStream = new FileOutputStream(mtlFilePath, false)) {
DefaultMtlWriter.write(mtls, mtlOutputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
log.debug("No materials to export for {}", filePath);
}
obj.setMtlFileNames(List.of(mtlFilePath));
}