001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.io;
014:
015: import java.io.IOException;
016:
017: public final class CountingDeviceDelegator implements Device {
018: private final Device deligee;
019: private long byteCount;
020:
021: public CountingDeviceDelegator(Device d) {
022: deligee = d;
023: byteCount = 0;
024: }
025:
026: public boolean isSizePreserving() {
027: return deligee.isSizePreserving();
028: }
029:
030: /**
031: * Flush this Device.
032: */
033: public void flush() throws IOException {
034: deligee.flush();
035: }
036:
037: public void close() throws IOException {
038: deligee.close();
039: }
040:
041: /**
042: * returns the number of bytes written to this data sink.
043: */
044: public long getSize() {
045: return byteCount;
046: }
047:
048: /**
049: * reset the number of bytes to zero.
050: */
051: public void resetSize() {
052: byteCount = 0;
053: }
054:
055: /**
056: * Print a character.
057: */
058: public Device print(char c) throws IOException {
059: ++byteCount;
060: deligee.print(c);
061: return this ;
062: }
063:
064: /**
065: * Print a character array.
066: */
067: public Device print(char[] c) throws IOException {
068: if (c != null)
069: byteCount += c.length;
070: deligee.print(c);
071: return this ;
072: }
073:
074: /**
075: * Print len characters from the specified char array starting at offset
076: * off to this Device.
077: */
078: public Device print(char[] c, int start, int len)
079: throws IOException {
080: byteCount += len;
081: deligee.print(c, start, len);
082: return this ;
083: }
084:
085: //-- print basic objects --
086:
087: /**
088: * Print a String.
089: */
090: public Device print(String s) throws IOException {
091: if (s != null)
092: byteCount += s.length();
093: deligee.print(s);
094: return this ;
095: }
096:
097: /**
098: * Print an integer.
099: */
100: public Device print(int i) throws IOException {
101: byteCount += String.valueOf(i).length();
102: deligee.print(i);
103: return this ;
104: }
105:
106: /**
107: * Print any Object
108: */
109: public Device print(Object o) throws IOException {
110: if (o != null)
111: byteCount += o.toString().length();
112: deligee.print(o);
113: return this ;
114: }
115:
116: /*-------------*
117: ** Methods which write raw bytes to the Device. Much like an OutputStream.
118: **-------------*/
119:
120: /**
121: * Writes the specified byte to this data output stream.
122: */
123: public Device write(int c) throws IOException {
124: ++byteCount;
125: deligee.write(c);
126: return this ;
127: }
128:
129: /**
130: * Writes b.length bytes from the specified byte array to this
131: * output stream.
132: */
133: public Device write(byte b[]) throws IOException {
134: if (b != null)
135: byteCount += b.length;
136: deligee.write(b);
137: return this ;
138: }
139:
140: /**
141: * Writes len bytes from the specified byte array starting at offset
142: * off to this Device.
143: */
144: public Device write(byte b[], int off, int len) throws IOException {
145: if (b != null)
146: byteCount += len;
147: deligee.write(b, off, len);
148: return this;
149: }
150: }
|