1 /* 2 * Copyright (C) 2007 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.io; 18 19 import java.io.FilterOutputStream; 20 import java.io.IOException; 21 import java.io.OutputStream; 22 23 /** 24 * An OutputStream that counts the number of bytes written. 25 * 26 * @author Chris Nokleberg 27 * @since 2009.09.15 <b>tentative</b> 28 */ 29 public class CountingOutputStream extends FilterOutputStream { 30 31 private long count; 32 33 /** 34 * Wraps another output stream, counting the number of bytes written. 35 * 36 * @param out the output stream to be wrapped 37 */ 38 public CountingOutputStream(OutputStream out) { 39 super(out); 40 } 41 42 /** Returns the number of bytes written. */ 43 public long getCount() { 44 return count; 45 } 46 47 @Override public void write(byte[] b, int off, int len) throws IOException { 48 out.write(b, off, len); 49 count += len; 50 } 51 52 @Override public void write(int b) throws IOException { 53 out.write(b); 54 count++; 55 } 56 } 57