X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.graph.compiler%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fcompiler%2FGraphCompiler.java;h=53060b767d072409834403e10bb810c870203e51;hb=refs%2Fchanges%2F49%2F1549%2F6;hp=26663b08a2a5f288e9f38753ff53162045a82c02;hpb=a8d030f7db1f59a5b51cdc34f18de7c0a0ee8549;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/GraphCompiler.java b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/GraphCompiler.java index 26663b08a..53060b767 100644 --- a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/GraphCompiler.java +++ b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/GraphCompiler.java @@ -11,6 +11,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Formatter; import java.util.Locale; +import java.util.TreeMap; import org.simantics.databoard.Bindings; import org.simantics.databoard.Files; @@ -56,23 +57,29 @@ public class GraphCompiler { } public static TransferableGraph1 read(InputStream stream) throws AdaptException, IOException { - DataContainer container = DataContainers.readFile(new DataInputStream(stream)); - stream.close(); - return (TransferableGraph1)container.content.getValue(TransferableGraph1.BINDING); + try (InputStream in = stream) { + DataContainer container = DataContainers.readFile(new DataInputStream(stream)); + return (TransferableGraph1)container.content.getValue(TransferableGraph1.BINDING); + } } public static InputStream write(TransferableGraph1 tg) throws BindingException, IOException { Binding binding = TransferableGraph1.BINDING; int hashCode = binding.hashValue(tg); - tg.extensions.put(Extensions.CACHED_HASHCODE, new Variant(Bindings.INTEGER, hashCode)); - try { - byte[] buffer = DataContainers.writeFile( - new DataContainer("graph", 1, new Variant(TransferableGraph1.BINDING, tg)) - ); - return new ByteArrayInputStream(buffer); - } finally { - tg.extensions.remove(Extensions.CACHED_HASHCODE); - } + TreeMap metadata = new TreeMap<>(); + metadata.put(Extensions.CACHED_HASHCODE, new Variant(Bindings.INTEGER, hashCode)); + byte[] buffer = DataContainers.writeFile( + new DataContainer("graph", 1, metadata, new Variant(binding, tg)) + ); + return new ByteArrayInputStream(buffer); + } + + public static void write(File file, TransferableGraph1 tg) throws BindingException, IOException { + Binding binding = TransferableGraph1.BINDING; + int hashCode = binding.hashValue(tg); + TreeMap metadata = new TreeMap<>(); + metadata.put(Extensions.CACHED_HASHCODE, new Variant(Bindings.INTEGER, hashCode)); + DataContainers.writeFile(file, new DataContainer("graph", 1, metadata, new Variant(binding, tg))); } public static CompilationResult compile(