1 7 8 package java.security; 9 10 import java.io.IOException ; 11 import java.io.EOFException ; 12 import java.io.OutputStream ; 13 import java.io.FilterOutputStream ; 14 import java.io.PrintStream ; 15 import java.io.ByteArrayOutputStream ; 16 17 38 public class DigestOutputStream extends FilterOutputStream { 39 40 private boolean on = true; 41 42 45 protected MessageDigest digest; 46 47 55 public DigestOutputStream(OutputStream stream, MessageDigest digest) { 56 super(stream); 57 setMessageDigest(digest); 58 } 59 60 66 public MessageDigest getMessageDigest() { 67 return digest; 68 } 69 70 76 public void setMessageDigest(MessageDigest digest) { 77 this.digest = digest; 78 } 79 80 97 public void write(int b) throws IOException { 98 if (on) { 99 digest.update((byte)b); 100 } 101 out.write(b); 102 } 103 104 127 public void write(byte[] b, int off, int len) throws IOException { 128 if (on) { 129 digest.update(b, off, len); 130 } 131 out.write(b, off, len); 132 } 133 134 143 public void on(boolean on) { 144 this.on = on; 145 } 146 147 151 public String toString() { 152 return "[Digest Output Stream] " + digest.toString(); 153 } 154 } 155 156 157 158 159 | Popular Tags |