View Javadoc

1   /*
2    * Copyright (c) 2005 The University of Reading
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    * 1. Redistributions of source code must retain the above copyright
9    *    notice, this list of conditions and the following disclaimer.
10   * 2. Redistributions in binary form must reproduce the above copyright
11   *    notice, this list of conditions and the following disclaimer in the
12   *    documentation and/or other materials provided with the distribution.
13   * 3. Neither the name of the University of Reading, nor the names of the
14   *    authors or contributors may be used to endorse or promote products
15   *    derived from this software without specific prior written permission.
16   * 
17   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20   * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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          // TODO: notify viewer
94          viewer.newData(data, count);
95      }
96      
97      public void error(DataRequest originalRequest, Exception e)
98      {
99          StreamViewer viewer = (StreamViewer)originalRequest.client;
100         // TODO: notify viewer
101         e.printStackTrace();
102     }
103     
104     public String getName()
105     {
106         return this.stream.getName();
107     }
108     
109     
110 }