1 package org.apache.commons.jcs.admin; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import java.io.IOException; 23 import java.io.OutputStream; 24 25 /** 26 * Keeps track of the number of bytes written to it, but doesn't write them anywhere. 27 */ 28 public class CountingOnlyOutputStream 29 extends OutputStream 30 { 31 /** number of bytes passed through */ 32 private int count; // TODO should this be long? 33 34 /** 35 * count as we write. 36 * <p> 37 * @param b 38 * @throws IOException 39 */ 40 @Override 41 public void write( byte[] b ) 42 throws IOException 43 { 44 this.count += b.length; 45 } 46 47 /** 48 * count as we write. 49 * <p> 50 * @param b 51 * @param off 52 * @param len 53 * @throws IOException 54 */ 55 @Override 56 public void write( byte[] b, int off, int len ) 57 throws IOException 58 { 59 this.count += len; 60 } 61 62 /** 63 * count as we write. 64 * <p> 65 * @param b 66 * @throws IOException 67 */ 68 @Override 69 public void write( int b ) 70 throws IOException 71 { 72 this.count++; 73 } 74 75 /** 76 * The number of bytes that have passed through this stream. 77 * <p> 78 * @return int 79 */ 80 public int getCount() 81 { 82 return this.count; 83 } 84 }