X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.graph%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Frepresentation%2FInputChannel.java;fp=bundles%2Forg.simantics.graph%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Frepresentation%2FInputChannel.java;h=1eb00c2f6dfe48eb84e4a238ed9ada416578fff1;hb=59cab457bcc89618e8557235fd65eaecbe583592;hp=0000000000000000000000000000000000000000;hpb=85eac68ed3cae0996c54b536a2f2a49abf8fc303;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.graph/src/org/simantics/graph/representation/InputChannel.java b/bundles/org.simantics.graph/src/org/simantics/graph/representation/InputChannel.java new file mode 100644 index 000000000..1eb00c2f6 --- /dev/null +++ b/bundles/org.simantics.graph/src/org/simantics/graph/representation/InputChannel.java @@ -0,0 +1,42 @@ +package org.simantics.graph.representation; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.channels.ReadableByteChannel; + +public class InputChannel implements ReadableByteChannel { + + private final InputStream stream; + + public InputChannel(InputStream stream) { + this.stream = stream; + } + + @Override + public boolean isOpen() { + return true; + } + + @Override + public void close() throws IOException { + // NOTE: it is an intentional choice not to close the underlying stream here + // InputStreams given directly to TransferableGraphFileReader are expected to + // be closed outside of this implementation. + } + + @Override + public int read(ByteBuffer dst) throws IOException { + int nRead; + int size = 0; + int position = dst.position(); + int limit = dst.limit(); + // The users of this channel expect that the data is fully read at this point + while ((nRead = stream.read(dst.array(), position, limit - position)) != -1 && limit - position > 0) { + size += nRead; + position += nRead; + } + return size; + } + +} \ No newline at end of file