Source code: com/sixlegs/image/png/CRCInputStream.java
1 // Copyright (C) 1998, 1999, 2001 Chris Nokleberg
2 // Please see included LICENSE.TXT
3
4 package com.sixlegs.image.png;
5
6 import java.io.FilterInputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.util.zip.CRC32;
10
11 final class CRCInputStream
12 extends FilterInputStream
13 {
14 private CRC32 crc = new CRC32();
15 private int byteCount = 0;
16
17 public CRCInputStream(InputStream in)
18 {
19 super(in);
20 }
21
22 public long getValue()
23 {
24 return (long)crc.getValue();
25 }
26
27 public void reset()
28 {
29 byteCount = 0;
30 crc.reset();
31 }
32
33 public int count()
34 {
35 return byteCount;
36 }
37
38 public int read()
39 throws IOException
40 {
41 int x = in.read();
42 if (x != -1) {
43 crc.update(x);
44 byteCount++;
45 }
46 return x;
47 }
48
49 public int read(byte[] b, int off, int len)
50 throws IOException
51 {
52 int x = in.read(b, off, len);
53 if (x != -1) {
54 crc.update(b, off, x);
55 byteCount += x;
56 }
57 return x;
58 }
59
60 private byte[] byteArray = new byte[0];
61
62 public long skip(long n)
63 throws IOException
64 {
65 // TODO: what if n > Integer.MAX_VALUE ?
66 if (byteArray.length < n) byteArray = new byte[(int)n];
67 return read(byteArray, 0, (int)n);
68 }
69 }
70