KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > compiler > ByteCountingOutputStream


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2001 Johannes Lehtinen
8  * Copyright 2002 Paul Wilkinson
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */

22
23 package com.izforge.izpack.compiler;
24
25 import java.io.IOException JavaDoc;
26 import java.io.OutputStream JavaDoc;
27
28 /**
29  * Stream which countes the bytes written through it. Be sure to flush before checking size.
30  */

31 public class ByteCountingOutputStream extends OutputStream JavaDoc
32 {
33
34     private long count;
35
36     private OutputStream JavaDoc os;
37
38     public ByteCountingOutputStream(OutputStream JavaDoc os)
39     {
40         this.os = os;
41     }
42
43     public void write(byte[] b, int off, int len) throws IOException JavaDoc
44     {
45         os.write(b, off, len);
46         count += len;
47     }
48
49     public void write(byte[] b) throws IOException JavaDoc
50     {
51         os.write(b);
52         count += b.length;
53     }
54
55     public void write(int b) throws IOException JavaDoc
56     {
57         os.write(b);
58         count += 4;
59     }
60
61     public void close() throws IOException JavaDoc
62     {
63         os.close();
64     }
65
66     public void flush() throws IOException JavaDoc
67     {
68         os.flush();
69     }
70
71     public long getByteCount()
72     {
73         return count;
74     }
75 }
76
Popular Tags