1 7 8 package java.security; 9 10 import java.io.IOException ; 11 import java.io.EOFException ; 12 import java.io.InputStream ; 13 import java.io.FilterInputStream ; 14 import java.io.PrintStream ; 15 import java.io.ByteArrayInputStream ; 16 17 46 47 public class DigestInputStream extends FilterInputStream { 48 49 50 51 52 private boolean on = true; 53 54 57 protected MessageDigest digest; 58 59 67 public DigestInputStream(InputStream stream, MessageDigest digest) { 68 super(stream); 69 setMessageDigest(digest); 70 } 71 72 78 public MessageDigest getMessageDigest() { 79 return digest; 80 } 81 82 88 public void setMessageDigest(MessageDigest digest) { 89 this.digest = digest; 90 } 91 92 106 public int read() throws IOException { 107 int ch = in.read(); 108 if (on && ch != -1) { 109 digest.update((byte)ch); 110 } 111 return ch; 112 } 113 114 143 public int read(byte[] b, int off, int len) throws IOException { 144 int result = in.read(b, off, len); 145 if (on && result != -1) { 146 digest.update(b, off, result); 147 } 148 return result; 149 } 150 151 160 public void on(boolean on) { 161 this.on = on; 162 } 163 164 168 public String toString() { 169 return "[Digest Input Stream] " + digest.toString(); 170 } 171 } 172 173 174 175 176 | Popular Tags |