X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.graph%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Frepresentation%2FTransferableGraphFileReader.java;h=6fc7ac5516f5cc9efb7da5f8cd3a4f588565fb54;hb=59cab457bcc89618e8557235fd65eaecbe583592;hp=077eb6617a1703f3cbffa913633e2b4c738f95ab;hpb=b6e702e0d16d951a25380f08de6dd2e7d4a0b127;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphFileReader.java b/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphFileReader.java index 077eb6617..6fc7ac551 100644 --- a/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphFileReader.java +++ b/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphFileReader.java @@ -14,6 +14,7 @@ import org.simantics.databoard.Bindings; import org.simantics.databoard.Datatypes; import org.simantics.databoard.binding.error.RuntimeDatatypeConstructionException; import org.simantics.databoard.binding.mutable.Variant; +import org.simantics.databoard.container.DataContainer; import org.simantics.databoard.container.DataContainers; import org.simantics.databoard.serialization.RuntimeSerializerConstructionException; import org.simantics.databoard.serialization.Serializer; @@ -22,6 +23,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * It is recommended to use {@link #read(File)} and {@link #read(InputStream)} + * for reading to ensure proper resource handling. + */ final public class TransferableGraphFileReader extends ByteFileReader { private static final Logger LOGGER = LoggerFactory.getLogger(TransferableGraphFileReader.class); @@ -48,42 +53,43 @@ final public class TransferableGraphFileReader extends ByteFileReader { } }; - - - final static class InputChannel implements ReadableByteChannel { - final private InputStream stream; - - public InputChannel(InputStream stream) { - this.stream = stream; - } - - @Override - public boolean isOpen() { - return true; - } + private static boolean init = true; + + final private static int SIZE = 1<<18; + final private static int HEADER = headerSize(); + final private int header; - @Override - public void close() throws IOException { + /** + * Reads a {@link DataContainer} containing a {@link TransferableGraph1} + * structure from the specified {@link File}. + * + * @param file the file to read from + * @return the TG contained by the file + * @throws IOException + */ + public static TransferableGraph1 read(File file) throws IOException { + try (TransferableGraphFileReader reader = new TransferableGraphFileReader(file)) { + return reader.readTG(); } + } - @Override - public int read(ByteBuffer dst) throws IOException { - int pos = dst.position(); - int limit = dst.limit(); - int i=stream.read(dst.array(), pos, limit-pos); - //LOGGER.warn("Read " + i + " (expected " + dst.array().length + ")"); - return i; + /** + * Reads a {@link DataContainer} containing a {@link TransferableGraph1} + * structure from the specified InputStream. Note that this implementation does + * not close the specified input stream, it is expected to be + * closed by the caller. + * + * @param input the input stream to read from + * @return the TG contained by the stream + * @throws IOException + */ + public static TransferableGraph1 read(InputStream input) throws IOException { + try (TransferableGraphFileReader reader = new TransferableGraphFileReader(input)) { + return reader.readTG(); } - } - - private static boolean init = true; - - final private static int SIZE = 1<<18; - final private static int HEADER = headerSize(); - final private int header; - + public TransferableGraphFileReader(File file) throws IOException { super(file, SIZE); if(init) { @@ -263,7 +269,7 @@ final public class TransferableGraphFileReader extends ByteFileReader { for(int i=0;i