/** * */ package uk.ac.vamsas.client.utils; import java.io.IOException; import java.io.Reader; /** * @author JimP * Simple wrapper for reader to keep track of position in stream. * This is used in the document unmarshalling mechanism to compute * a simple hash based on the distance between the read blocks containing * the start and end tags of the XML. */ public class ChecksummedReader extends Reader { private Reader myReader=null; private long count=0; public ChecksummedReader(Reader myReader) { super(); this.myReader = myReader; count=0; } /* (non-Javadoc) * @see java.io.Reader#close() */ public void close() throws IOException { if (myReader!=null) myReader.close(); else throw new IOException("Close called on un-inited ChecksummedReader"); } /* (non-Javadoc) * @see java.io.Reader#read(char[], int, int) */ public int read(char[] cbuf, int off, int len) throws IOException { int rlen = myReader.read(cbuf, off, len); if (rlen>0) count+=cbuf.hashCode(); return rlen; } /** * Return current checksum of read bytes. * @return stream checksum */ public long getChecksum() { return count; } /** * Return existing checksum value and reset * @return old count */ public long getAndResetChecksum() { long cnt=count; count=0; return cnt; } }