51f773a3cf921e6c07963c1ff2b98972aeea64d2
[yangtools.git] / plugin / yang-maven-plugin / src / main / java / org / opendaylight / yangtools / yang2sources / plugin / CapturingOutputStream.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang2sources.plugin;
9
10 import com.google.common.hash.Hashing;
11 import com.google.common.hash.HashingOutputStream;
12 import java.io.FilterOutputStream;
13 import java.io.IOException;
14 import java.io.OutputStream;
15
16 /**
17  * An {@link OutputStream} which captures the sum of its contents.
18  */
19 final class CapturingOutputStream extends FilterOutputStream {
20     private long size;
21
22     CapturingOutputStream(final OutputStream out) {
23         super(new HashingOutputStream(Hashing.crc32c(), out));
24     }
25
26     @Override
27     @SuppressWarnings("checkstyle:parameterName")
28     public void write(final int b) throws IOException {
29         out().write(b);
30         size++;
31     }
32
33     @Override
34     @SuppressWarnings("checkstyle:parameterName")
35     public void write(final byte[] b, final int off, final int len) throws IOException {
36         out().write(b, off, len);
37         size += len;
38     }
39
40     long size() {
41         return size;
42     }
43
44     int crc32c() {
45         return out().hash().asInt();
46     }
47
48     private HashingOutputStream out() {
49         return (HashingOutputStream) out;
50     }
51 }