1 /* 2 * CountingOutputStream 3 * 4 * Author: Lasse Collin <lasse.collin@tukaani.org> 5 * 6 * This file has been put into the public domain. 7 * You can do whatever you want with this file. 8 */ 9 10 package org.tukaani.xz; 11 12 import java.io.OutputStream; 13 import java.io.IOException; 14 15 /** 16 * Counts the number of bytes written to an output stream. 17 * <p> 18 * The <code>finish</code> method does nothing. 19 * This is <code>FinishableOutputStream</code> instead 20 * of <code>OutputStream</code> solely because it allows 21 * using this as the output stream for a chain of raw filters. 22 */ 23 class CountingOutputStream extends FinishableOutputStream { 24 private final OutputStream out; 25 private long size = 0; 26 CountingOutputStream(OutputStream out)27 public CountingOutputStream(OutputStream out) { 28 this.out = out; 29 } 30 write(int b)31 public void write(int b) throws IOException { 32 out.write(b); 33 if (size >= 0) 34 ++size; 35 } 36 write(byte[] b, int off, int len)37 public void write(byte[] b, int off, int len) throws IOException { 38 out.write(b, off, len); 39 if (size >= 0) 40 size += len; 41 } 42 flush()43 public void flush() throws IOException { 44 out.flush(); 45 } 46 close()47 public void close() throws IOException { 48 out.close(); 49 } 50 getSize()51 public long getSize() { 52 return size; 53 } 54 } 55