1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package uk.ac.rdg.resc.jstyx.gridservice.client;
30
31 import java.io.File;
32 import java.io.IOException;
33
34 import uk.ac.rdg.resc.jstyx.client.CStyxFile;
35 import uk.ac.rdg.resc.jstyx.client.CStyxFileInputStream;
36
37 import uk.ac.rdg.resc.jstyx.gridservice.server.GeneralCachingStreamReader;
38
39 /***
40 * Reads a CStyxFileInputStream and caches the results locally, allowing multiple
41 * windows to be opened on the same data feed whilst saving network traffic
42 *
43 * @author Jon Blower
44 * $Revision: 377 $
45 * $Date: 2005-08-31 18:03:19 +0100 (Wed, 31 Aug 2005) $
46 * $Log$
47 * Revision 1.4 2005/08/31 17:03:19 jonblower
48 * Renamed "StyxFile*putStream*" to "CStyxFile*putStream*" for consistency with CStyxFile class
49 *
50 * Revision 1.3 2005/06/07 16:44:45 jonblower
51 * Fixed problem with caching stream reader on client side
52 *
53 * Revision 1.2 2005/05/27 21:22:39 jonblower
54 * Further development of caching stream readers
55 *
56 * Revision 1.1 2005/05/27 17:02:59 jonblower
57 * Initial import
58 */
59 public class CachedStreamReader extends GeneralCachingStreamReader
60 {
61 private CStyxFile stream;
62 private boolean started;
63
64 public CachedStreamReader(CStyxFile stream) throws IOException
65 {
66 this.stream = stream;
67 this.started = false;
68 this.setCacheFile(File.createTempFile(stream.getName(), ".tmp"));
69 }
70
71 /***
72 * Starts reading from the stream. Does nothing if already started.
73 */
74 public void start() throws IOException
75 {
76 if (!this.started)
77 {
78 this.started = true;
79 this.startReading(new CStyxFileInputStream(this.stream));
80 }
81 }
82
83 public void read(StreamViewer viewer, long offset, int count)
84 {
85 DataRequest dr = new DataRequest(viewer, offset, count);
86 this.read(dr);
87 }
88
89 public void newData(DataRequest originalRequest, byte[] data,
90 int offset, int count)
91 {
92 StreamViewer viewer = (StreamViewer)originalRequest.client;
93
94 viewer.newData(data, count);
95 }
96
97 public void error(DataRequest originalRequest, Exception e)
98 {
99 StreamViewer viewer = (StreamViewer)originalRequest.client;
100
101 e.printStackTrace();
102 }
103
104 public String getName()
105 {
106 return this.stream.getName();
107 }
108
109
110 }