1 package twcsckernel.projectbase.io;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.Serializable;
6
7 import twcsckernel.projectbase.common.ReaderFactory;
8
9 public class RemoteFileInputStream extends InputStream implements Serializable {
10
11 private static final long serialVersionUID = 1L;
12
13 private final ReaderFactory rf;
14
15 private final int streamID;
16
17 private static final IOException streamNotFound = new IOException(
18 "Stream not found");
19
20 public RemoteFileInputStream(int streamID, ReaderFactory rf) {
21 this.streamID = streamID;
22 this.rf = rf;
23 }
24
25 @Override
26 public int read() throws IOException {
27 int result = 0;
28 try {
29 result = rf.read(streamID).readResult;
30 } catch (ItemNotFoundException snfe) {
31 throw streamNotFound;
32 }
33 return result;
34 }
35
36 @Override
37 public int read(byte[] b, int off, int len) throws IOException {
38 ReadResult result = null;
39 if (b == null) {
40 throw new NullPointerException();
41 } else if ((off < 0) || (off > b.length) || (len < 0)
42 || ((off + len) > b.length) || ((off + len) < 0)) {
43 throw new IndexOutOfBoundsException();
44 } else if (len == 0) {
45 return 0;
46 }
47 try {
48 result = rf.read(streamID, len);
49 } catch (ItemNotFoundException snfe) {
50 throw streamNotFound;
51 }
52
53
54 for (int i=0;i<result.readResult;i++)
55 b[off+i]=result.buffer[i];
56
57
58 return result.readResult;
59 }
60
61 @Override
62 public int read(byte[] b) throws IOException {
63 return read(b, 0, b.length);
64 }
65
66 @Override
67 public long skip(long off) throws IOException {
68 long result = 0;
69 try {
70 result = rf.skip(streamID, off);
71 } catch (ItemNotFoundException snfe) {
72 throw streamNotFound;
73 }
74 return result;
75 }
76
77 @Override
78 public int available() throws IOException {
79 int result = 0;
80 try {
81 result = rf.available(streamID);
82 } catch (ItemNotFoundException snfe) {
83 throw streamNotFound;
84 }
85 return result;
86 }
87
88 @Override
89 public void close() throws IOException {
90 try {
91 rf.close(streamID);
92 } catch (ItemNotFoundException snfe) {
93 throw streamNotFound;
94 }
95 }
96 }