]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph/src/org/simantics/graph/representation/InputChannel.java
Remove duplicate InputChannel inner classes
[simantics/platform.git] / bundles / org.simantics.graph / src / org / simantics / graph / representation / InputChannel.java
1 package org.simantics.graph.representation;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.nio.ByteBuffer;
6 import java.nio.channels.ReadableByteChannel;
7
8 public class InputChannel implements ReadableByteChannel {
9
10         private final InputStream stream;
11
12         public InputChannel(InputStream stream) {
13                 this.stream = stream;
14         }
15
16         @Override
17         public boolean isOpen() {
18                 return true;
19         }
20
21         @Override
22         public void close() throws IOException {
23                 // NOTE: it is an intentional choice not to close the underlying stream here
24                 // InputStreams given directly to TransferableGraphFileReader are expected to
25                 // be closed outside of this implementation.
26         }
27
28         @Override
29         public int read(ByteBuffer dst) throws IOException {
30                 int nRead;
31                 int size = 0;
32                 int position = dst.position();
33                 int limit = dst.limit();
34                 // The users of this channel expect that the data is fully read at this point
35                 while ((nRead = stream.read(dst.array(), position, limit - position)) != -1 && limit - position > 0) {
36                         size += nRead;
37                         position += nRead;
38                 }
39                 return size;
40         }
41
42 }