1 7 8 package java.util.zip; 9 10 import java.io.FilterInputStream ; 11 import java.io.InputStream ; 12 import java.io.IOException ; 13 14 22 public 23 class CheckedInputStream extends FilterInputStream { 24 private Checksum cksum; 25 26 31 public CheckedInputStream(InputStream in, Checksum cksum) { 32 super(in); 33 this.cksum = cksum; 34 } 35 36 41 public int read() throws IOException { 42 int b = in.read(); 43 if (b != -1) { 44 cksum.update(b); 45 } 46 return b; 47 } 48 49 59 public int read(byte[] buf, int off, int len) throws IOException { 60 len = in.read(buf, off, len); 61 if (len != -1) { 62 cksum.update(buf, off, len); 63 } 64 return len; 65 } 66 67 73 public long skip(long n) throws IOException { 74 byte[] buf = new byte[512]; 75 long total = 0; 76 while (total < n) { 77 long len = n - total; 78 len = read(buf, 0, len < buf.length ? (int)len : buf.length); 79 if (len == -1) { 80 return total; 81 } 82 total += len; 83 } 84 return total; 85 } 86 87 91 public Checksum getChecksum() { 92 return cksum; 93 } 94 } 95 | Popular Tags |