001package org.apache.commons.jcs.admin; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.IOException; 023import java.io.OutputStream; 024 025/** 026 * Keeps track of the number of bytes written to it, but doesn't write them anywhere. 027 */ 028public class CountingOnlyOutputStream 029 extends OutputStream 030{ 031 /** number of bytes passed through */ 032 private int count; // TODO should this be long? 033 034 /** 035 * count as we write. 036 * <p> 037 * @param b 038 * @throws IOException 039 */ 040 @Override 041 public void write( byte[] b ) 042 throws IOException 043 { 044 this.count += b.length; 045 } 046 047 /** 048 * count as we write. 049 * <p> 050 * @param b 051 * @param off 052 * @param len 053 * @throws IOException 054 */ 055 @Override 056 public void write( byte[] b, int off, int len ) 057 throws IOException 058 { 059 this.count += len; 060 } 061 062 /** 063 * count as we write. 064 * <p> 065 * @param b 066 * @throws IOException 067 */ 068 @Override 069 public void write( int b ) 070 throws IOException 071 { 072 this.count++; 073 } 074 075 /** 076 * The number of bytes that have passed through this stream. 077 * <p> 078 * @return int 079 */ 080 public int getCount() 081 { 082 return this.count; 083 } 084}