KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > io > CounterOutputStream


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.io.CounterOutputStream
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    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, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.services.io;
23
24 import java.io.OutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.EOFException JavaDoc;
27
28 /**
29     An OutputStream that simply provides methods to count the number
30     of bytes written to an underlying stream.
31 */

32
33 public class CounterOutputStream extends OutputStream JavaDoc implements Limit {
34
35     protected OutputStream JavaDoc out;
36     private int count;
37     private int limit;
38
39     /**
40         Create a CounterOutputStream that will discard any bytes
41         written but still coutn them and call its reset method
42         so that the count is intially zero.
43     */

44     public CounterOutputStream() {
45         super();
46     }
47
48     public void setOutputStream(OutputStream JavaDoc out) {
49         this.out = out;
50         setLimit(-1);
51     }
52
53     /**
54         Get count of bytes written to the stream since the last
55         reset() call.
56     */

57     public int getCount() {
58         return count;
59     }
60
61     /**
62         Set a limit at which an exception will be thrown. This allows callers
63         to count the number of bytes up to some point, without having to complete
64         the count. E.g. a caller may only want to see if some object will write out
65         over 4096 bytes, without waiting for all 200,000 bytes of the object to be written.
66         <BR>
67         If the passed in limit is 0 or negative then the stream will count bytes without
68         throwing an exception.
69
70         @see EOFException
71     */

72     public void setLimit(int limit) {
73
74         count = 0;
75
76         this.limit = limit;
77
78         return;
79     }
80
81     public int clearLimit() {
82
83         int unused = limit - count;
84         limit = 0;
85
86         return unused;
87     }
88
89     /*
90     ** Methods of OutputStream
91     */

92
93     /**
94         Add 1 to the count.
95
96         @see OutputStream#write
97     */

98     public void write(int b) throws IOException JavaDoc {
99         
100         if ((limit >= 0) && ((count + 1) > limit)) {
101             throw new EOFException JavaDoc();
102         }
103
104         out.write(b);
105         count++;
106     }
107
108     /**
109         Add b.length to the count.
110
111         @see OutputStream#write
112     */

113     public void write(byte b[]) throws IOException JavaDoc {
114         
115         if ((limit >= 0) && ((count + b.length) > limit)) {
116             throw new EOFException JavaDoc();
117         }
118
119         out.write(b);
120         count += b.length;
121     }
122
123     /**
124         Add len to the count, discard the data.
125
126         @see OutputStream#write
127     */

128     public void write(byte b[], int off, int len) throws IOException JavaDoc {
129
130         if ((limit >= 0) && ((count + len) > limit)) {
131             throw new EOFException JavaDoc();
132         }
133
134         out.write(b, off, len);
135         count += len;
136     }
137 }
138
Popular Tags