X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fcontainer%2FDataContainers.java;h=dba475f0e58ef26d2d64f0b60a1f86f1988f4b4c;hp=21e54d8975d5379d657e3f6ce468467de9452e24;hb=77921feee3f8331ab54796ff0832921405bea874;hpb=9436bf9b21fc26aa7eb41d128ebe42c9668cedf4 diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/container/DataContainers.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/container/DataContainers.java index 21e54d897..dba475f0e 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/container/DataContainers.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/container/DataContainers.java @@ -59,24 +59,24 @@ public class DataContainers { String format = (String)STRING_SERIALIZER.deserialize(input); int version = (Integer)INTEGER_SERIALIZER.deserialize(input); @SuppressWarnings("unchecked") - TreeMap metadata = (TreeMap)METADATA_SERIALIZER.deserialize(input); + TreeMap metadata = (TreeMap)METADATA_SERIALIZER.deserialize(input); return new DataContainer(format, version, metadata, null); } - + /** * Consumes a header from a given stream and checks that the header satisfies the given format and version. * Returns the obtained header if the check fails and null on success. * @throws IOException */ public static DataContainer requireHeader(DataInput input, String format, int version) { - try { - DataContainer header = readHeader(input); - if(!format.equals(header.format) || version != header.version) - return header; - else return null; - } catch (Throwable t) { - return new DataContainer("unknown", 0, null, null); - } + try { + DataContainer header = readHeader(input); + if(!format.equals(header.format) || version != header.version) + return header; + else return null; + } catch (Throwable t) { + return new DataContainer("unknown", 0, null, null); + } } /** @@ -104,14 +104,9 @@ public class DataContainers { * @throws IOException */ public static DataContainer validateHeader(File file, String format, int version) throws IOException { - - InputStream stream = new FileInputStream( file ); - try { - return DataContainers.requireHeader(new DataInputStream(stream), format, version); - } finally { - stream.close(); - } - + try (InputStream stream = new FileInputStream( file )) { + return DataContainers.requireHeader(new DataInputStream(stream), format, version); + } } /** @@ -120,14 +115,9 @@ public class DataContainers { * @throws IOException */ public static DataContainer validateHeader(File file, String... allowedFormatStrings) throws IOException { - - InputStream stream = new FileInputStream( file ); - try { + try (InputStream stream = new FileInputStream( file )) { return DataContainers.requireHeader(new DataInputStream(stream), allowedFormatStrings); - } finally { - stream.close(); } - } /** @@ -136,14 +126,11 @@ public class DataContainers { * @throws IOException */ public static DataContainer readHeader(File input) throws IOException { - BinaryFile rf = new BinaryFile( input, "r" ); - try { + try (BinaryFile rf = new BinaryFile( input, "r" )) { return readHeader(rf); - } finally { - rf.close(); - } + } } - + /** * Reads a data container including the content data. * @throws IOException @@ -154,6 +141,23 @@ public class DataContainers { return result; } + /** + * Reads a data container including the content data. + * @throws IOException + */ + public static DataContainer readFile(DataInput input, Binding expectedBinding) throws IOException, DataFormatException { + DataContainer result = readHeader(input); + Datatype contentType = (Datatype) DATATYPE_SERIALIZER.deserialize(input); + if (!expectedBinding.type().equals(contentType)) + throw new DataFormatException( + "Content type didn't match the type expected for the binding " + expectedBinding + + ":\nexpected type: " + expectedBinding.type() + + "\nactual type: " + contentType); + Object value = Bindings.getSerializerUnchecked(expectedBinding).deserialize(input); + result.content = new Variant(expectedBinding, value); + return result; + } + /** * Process a data container using a format handler matching the format and version * of the file. @@ -161,49 +165,54 @@ public class DataContainers { */ public static T readFile(DataInput input, Map> handlers) throws Exception { DataContainer result = readHeader(input); - + FormatHandler handler = handlers.get(result.format + ":" + result.version); if(handler == null) throw new DataFormatException("Unknown data format " + result.format + " version " + result.version + "."); Binding binding = handler.getBinding(); - + Datatype contentType = (Datatype)DATATYPE_SERIALIZER.deserialize(input); if(!binding.type().equals(contentType)) throw new DataFormatException("Content type didn't match the type expected for the format " + result.format + " version " + result.version + "."); - + Object value = Bindings.getSerializerUnchecked(binding).deserialize(input); - + result.content = new Variant(binding, value); return handler.process(result); } - + /** * Reads a data container including the content data. * @throws IOException */ public static DataContainer readFile(File input) throws IOException { - BinaryFile rf = new BinaryFile( input, "r" ); - try { + try (BinaryFile rf = new BinaryFile( input, "r" )) { return readFile(rf); - } finally { - rf.close(); - } + } + } + + /** + * Reads a data container including the content data. + * @throws IOException + * @throws DataFormatException + */ + public static DataContainer readFile(File input, Binding expectedBinding) throws IOException, DataFormatException { + try (BinaryFile rf = new BinaryFile( input, "r" )) { + return readFile(rf, expectedBinding); + } } - + /** * Process a data container using a format handler matching the format and version * of the file. * @param handlers Map of handlers. Keys are strings of form "format:version". */ public static T readFile(File input, Map> handlers) throws Exception { - BinaryFile rf = new BinaryFile( input, "r" ); - try { + try (BinaryFile rf = new BinaryFile( input, "r" )) { return readFile(rf, handlers); - } finally { - rf.close(); - } + } } - + /** * Writes header fields of a container to the given output. Content field is * ignored. @@ -214,7 +223,7 @@ public class DataContainers { INTEGER_SERIALIZER.serialize(output, container.version); METADATA_SERIALIZER.serialize(output, container.metadata); } - + /** * Writes a data container to the given output. * @throws IOException @@ -223,22 +232,20 @@ public class DataContainers { writeHeader(output, container); VARIANT_SERIALIZER.serialize(output, container.content); } - + /** * Writes a data container to the given file * @throws IOException */ public static void writeFile(File output, DataContainer container) throws IOException { - BinaryFile wf = new BinaryFile( output); - try { + try (BinaryFile wf = new BinaryFile(output)) { writeFile(wf, container); wf.setLength(wf.position()); - } finally { - wf.close(); - } + } } - + public static byte[] writeFile(DataContainer container) throws IOException { return DATA_CONTAINER_SERIALIZER.serialize(container); } + }