KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > io > CountingDeviceDelegator


1 /*
2  * $Id: CountingDeviceDelegator.java,v 1.3 2004/12/01 07:54:08 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.io;
15
16 import java.io.IOException JavaDoc;
17
18 public final class CountingDeviceDelegator implements Device {
19     private final Device deligee;
20     private long byteCount;
21
22     public CountingDeviceDelegator(Device d) {
23         deligee = d;
24         byteCount = 0;
25     }
26
27     public boolean isSizePreserving() { return deligee.isSizePreserving(); }
28
29     /**
30      * Flush this Device.
31      */

32     public void flush() throws IOException JavaDoc { deligee.flush(); }
33
34     public void close() throws IOException JavaDoc { deligee.close(); }
35
36     /**
37      * returns the number of bytes written to this data sink.
38      */

39     public long getSize() { return byteCount; }
40
41     /**
42      * reset the number of bytes to zero.
43      */

44     public void resetSize() { byteCount = 0; }
45
46     /**
47      * Print a character.
48      */

49     public Device print(char c) throws IOException JavaDoc {
50         ++byteCount;
51         deligee.print(c);
52         return this;
53     }
54
55     /**
56      * Print a character array.
57      */

58     public Device print(char[] c) throws IOException JavaDoc {
59         if (c != null) byteCount += c.length;
60         deligee.print(c);
61         return this;
62     }
63
64     /**
65      * Print len characters from the specified char array starting at offset
66      * off to this Device.
67      */

68     public Device print(char[] c, int start, int len) throws IOException JavaDoc {
69         byteCount += len;
70         deligee.print(c, start, len);
71         return this;
72     }
73
74     //-- print basic objects --
75

76     /**
77      * Print a String.
78      */

79     public Device print(String JavaDoc s) throws IOException JavaDoc {
80         if (s != null) byteCount += s.length();
81         deligee.print(s);
82         return this;
83     }
84
85     /**
86      * Print an integer.
87      */

88     public Device print(int i) throws IOException JavaDoc {
89         byteCount += String.valueOf(i).length();
90         deligee.print(i);
91         return this;
92     }
93
94     /**
95      * Print any Object
96      */

97     public Device print(Object JavaDoc o) throws IOException JavaDoc {
98         if (o != null) byteCount += o.toString().length();
99         deligee.print(o);
100         return this;
101     }
102
103     /*-------------*
104      ** Methods which write raw bytes to the Device. Much like an OutputStream.
105      **-------------*/

106
107     /**
108      * Writes the specified byte to this data output stream.
109      */

110     public Device write(int c) throws IOException JavaDoc {
111         ++byteCount;
112         deligee.write(c);
113         return this;
114     }
115
116     /**
117      * Writes b.length bytes from the specified byte array to this
118      * output stream.
119      */

120     public Device write(byte b[]) throws IOException JavaDoc {
121         if (b != null) byteCount += b.length;
122         deligee.write(b);
123         return this;
124     }
125
126     /**
127      * Writes len bytes from the specified byte array starting at offset
128      * off to this Device.
129      */

130     public Device write(byte b[], int off, int len) throws IOException JavaDoc {
131         if (b != null) byteCount += len;
132         deligee.write(b, off, len);
133         return this;
134     }
135 }
136
137
138
Popular Tags