Debian Codegraph @

src/org/tukaani/xz/CountingOutputStream.java

raw symbols SCIP
1 // SPDX-License-Identifier: 0BSD
2 // SPDX-FileCopyrightText: The XZ for Java authors and contributors
3 // SPDX-FileContributor: Lasse Collin <[email protected]>
4
5 package org.tukaani.xz;
6
7 import java.io.OutputStream;
8 import java.io.IOException;
9
10 /**
11 * Counts the number of bytes written to an output stream.
12 * <p>
13 * The {@code finish} method does nothing.
14 * This is {@code FinishableOutputStream} instead
15 * of {@code OutputStream} solely because it allows
16 * using this as the output stream for a chain of raw filters.
17 */
18 class CountingOutputStream extends FinishableOutputStream {
19 private final OutputStream out;
20 private long size = 0;
21
22 public CountingOutputStream(OutputStream out) {
23 this.out = out;
24 }
25
26 @Override
27 public void write(int b) throws IOException {
28 out.write(b);
29 if (size >= 0)
30 ++size;
31 }
32
33 @Override
34 public void write(byte[] b, int off, int len) throws IOException {
35 out.write(b, off, len);
36 if (size >= 0)
37 size += len;
38 }
39
40 @Override
41 public void flush() throws IOException {
42 out.flush();
43 }
44
45 @Override
46 public void close() throws IOException {
47 out.close();
48 }
49
50 public long getSize() {
51 return size;
52 }
53 }