1 28 29 package HTTPClient; 30 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.io.FilterInputStream ; 34 import java.net.ProtocolException ; 35 36 37 45 class MD5InputStream extends FilterInputStream 46 { 47 private HashVerifier verifier; 48 private MD5 md5; 49 private long rcvd = 0; 50 private boolean closed = false; 51 52 53 57 public MD5InputStream(InputStream is, HashVerifier verifier) 58 { 59 super(is); 60 this.verifier = verifier; 61 md5 = new MD5(); 62 } 63 64 65 public synchronized int read() throws IOException 66 { 67 int b = in.read(); 68 if (b != -1) 69 md5.Update((byte) b); 70 else 71 real_close(); 72 73 rcvd++; 74 return b; 75 } 76 77 78 public synchronized int read(byte[] buf, int off, int len) 79 throws IOException 80 { 81 int num = in.read(buf, off, len); 82 if (num > 0) 83 md5.Update(buf, off, num); 84 else 85 real_close(); 86 87 rcvd += num; 88 return num; 89 } 90 91 92 public synchronized long skip(long num) throws IOException 93 { 94 byte[] tmp = new byte[(int) num]; 95 int got = read(tmp, 0, (int) num); 96 97 if (got > 0) 98 return (long) got; 99 else 100 return 0L; 101 } 102 103 104 113 public synchronized void close() throws IOException 114 { 115 while (skip(10000) > 0) ; 116 real_close(); 117 } 118 119 120 127 private void real_close() throws IOException 128 { 129 if (closed) return; 130 closed = true; 131 132 in.close(); 133 verifier.verifyHash(md5.Final(), rcvd); 134 } 135 } 136 137 | Popular Tags |