001/* 002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved. 003 * 004 * http://www.izforge.com/izpack/ 005 * http://developer.berlios.de/projects/izpack/ 006 * 007 * Copyright 2001 Johannes Lehtinen 008 * Copyright 2002 Paul Wilkinson 009 * 010 * Licensed under the Apache License, Version 2.0 (the "License"); 011 * you may not use this file except in compliance with the License. 012 * You may obtain a copy of the License at 013 * 014 * http://www.apache.org/licenses/LICENSE-2.0 015 * 016 * Unless required by applicable law or agreed to in writing, software 017 * distributed under the License is distributed on an "AS IS" BASIS, 018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 019 * See the License for the specific language governing permissions and 020 * limitations under the License. 021 */ 022 023package com.izforge.izpack.compiler; 024 025import java.io.IOException; 026import java.io.OutputStream; 027 028/** 029 * Stream which countes the bytes written through it. Be sure to flush before checking size. 030 */ 031public class ByteCountingOutputStream extends OutputStream 032{ 033 034 private long count; 035 036 private OutputStream os; 037 038 public ByteCountingOutputStream(OutputStream os) 039 { 040 this.os = os; 041 } 042 043 public void write(byte[] b, int off, int len) throws IOException 044 { 045 os.write(b, off, len); 046 count += len; 047 } 048 049 public void write(byte[] b) throws IOException 050 { 051 os.write(b); 052 count += b.length; 053 } 054 055 public void write(int b) throws IOException 056 { 057 os.write(b); 058 count += 4; 059 } 060 061 public void close() throws IOException 062 { 063 os.close(); 064 } 065 066 public void flush() throws IOException 067 { 068 os.flush(); 069 } 070 071 public long getByteCount() 072 { 073 return count; 074 } 075}