package com.czhj.wire.okio; import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public final class HashingSink extends ForwardingSink { private final MessageDigest a; private HashingSink(Sink sink, String str) { super(sink); try { this.a = MessageDigest.getInstance(str); } catch (NoSuchAlgorithmException unused) { throw new AssertionError(); } } public static HashingSink md5(Sink sink) { return new HashingSink(sink, "MD5"); } public static HashingSink sha1(Sink sink) { return new HashingSink(sink, "SHA-1"); } public static HashingSink sha256(Sink sink) { return new HashingSink(sink, "SHA-256"); } public ByteString hash() { return ByteString.of(this.a.digest()); } @Override // com.czhj.wire.okio.Sink, com.czhj.wire.okio.ForwardingSink public void write(Buffer buffer, long j) throws IOException { Util.checkOffsetAndCount(buffer.c, 0, j); Segment segment = buffer.b; long j2 = 0; while (j2 < j) { int min = (int) Math.min(j - j2, (long) (segment.e - segment.d)); this.a.update(segment.c, segment.d, min); j2 += (long) min; segment = segment.h; } super.write(buffer, j); } }