|
| 1 | +// Copyright 2020 The Bazel Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.devtools.build.lib.bazel.repository.downloader; |
| 16 | + |
| 17 | +import com.google.common.hash.HashCode; |
| 18 | +import com.google.common.hash.Hasher; |
| 19 | +import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadCompatible; |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.OutputStream; |
| 22 | +import javax.annotation.Nullable; |
| 23 | +import javax.annotation.WillCloseWhenClosed; |
| 24 | + |
| 25 | +/** |
| 26 | + * Output stream that guarantees its contents matches a hash code. |
| 27 | + * |
| 28 | + * <p>The actual checksum is computed gradually as the output is written. If it doesn't match, then |
| 29 | + * an {@link IOException} will be thrown when {@link #close()} is called. This error will be thrown |
| 30 | + * multiple times if these methods are called again for some reason. |
| 31 | + * |
| 32 | + * <p>Note that as the checksum can only be computed once the stream is closed, data will be written |
| 33 | + * to the underlying stream regardless of whether it matches the expected checksum. |
| 34 | + * |
| 35 | + * <p>This class is not thread safe, but it is safe to message pass this object between threads. |
| 36 | + */ |
| 37 | +@ThreadCompatible |
| 38 | +public final class HashOutputStream extends OutputStream { |
| 39 | + |
| 40 | + private final OutputStream delegate; |
| 41 | + private final Hasher hasher; |
| 42 | + private final HashCode code; |
| 43 | + @Nullable private volatile HashCode actual; |
| 44 | + |
| 45 | + public HashOutputStream(@WillCloseWhenClosed OutputStream delegate, Checksum checksum) { |
| 46 | + this.delegate = delegate; |
| 47 | + this.hasher = checksum.getKeyType().newHasher(); |
| 48 | + this.code = checksum.getHashCode(); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void write(int buffer) throws IOException { |
| 53 | + hasher.putByte((byte) buffer); |
| 54 | + delegate.write(buffer); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void write(byte[] buffer) throws IOException { |
| 59 | + hasher.putBytes(buffer); |
| 60 | + delegate.write(buffer); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void write(byte[] buffer, int offset, int length) throws IOException { |
| 65 | + hasher.putBytes(buffer, offset, length); |
| 66 | + delegate.write(buffer, offset, length); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void flush() throws IOException { |
| 71 | + delegate.flush(); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void close() throws IOException { |
| 76 | + delegate.close(); |
| 77 | + check(); |
| 78 | + } |
| 79 | + |
| 80 | + private void check() throws IOException { |
| 81 | + if (actual == null) { |
| 82 | + actual = hasher.hash(); |
| 83 | + } |
| 84 | + if (!code.equals(actual)) { |
| 85 | + throw new UnrecoverableHttpException( |
| 86 | + String.format("Checksum was %s but wanted %s", actual, code)); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments