int to long type change in castor-1.1(v) objects
[vamsas.git] / src / uk / ac / vamsas / client / utils / ChecksummedReader.java
1 /**\r
2  * \r
3  */\r
4 package uk.ac.vamsas.client.utils;\r
5 \r
6 import java.io.IOException;\r
7 import java.io.Reader;\r
8 \r
9 /**\r
10  * @author JimP\r
11  * Simple wrapper for reader to keep track of position in stream.\r
12  * This is used in the document unmarshalling mechanism to compute\r
13  * a simple hash based on the distance between the read blocks containing\r
14  * the start and end tags of the XML.\r
15  */\r
16 public class ChecksummedReader extends Reader {\r
17   private Reader myReader=null;\r
18   private long count=0;\r
19   public ChecksummedReader(Reader myReader) {\r
20     super();\r
21     this.myReader = myReader;\r
22     count=0;\r
23   }\r
24 \r
25   /* (non-Javadoc)\r
26    * @see java.io.Reader#close()\r
27    */\r
28   public void close() throws IOException {\r
29     if (myReader!=null)\r
30       myReader.close();\r
31     else\r
32       throw new IOException("Close called on un-inited ChecksummedReader");\r
33   }\r
34 \r
35   /* (non-Javadoc)\r
36    * @see java.io.Reader#read(char[], int, int)\r
37    */\r
38   public int read(char[] cbuf, int off, int len) throws IOException {\r
39     int rlen = myReader.read(cbuf, off, len);\r
40     if (rlen>0)\r
41       count+=cbuf.hashCode();\r
42     return rlen;\r
43   }\r
44   /**\r
45    * Return current checksum of read bytes.\r
46    * @return stream checksum\r
47    */\r
48   public long getChecksum() {\r
49     return count;\r
50   }\r
51   /**\r
52    * Return existing checksum value and reset\r
53    * @return old count\r
54    */\r
55   public long getAndResetChecksum() {\r
56     long cnt=count;\r
57     count=0;\r
58     return cnt;\r
59   }\r
60 }\r