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.Serializable;
016:
017: /**
018: * Device, that discards everything. For debugging purposes.
019: * Counts the number of bytes written (not exactly, since for print() methods,
020: * it counts the number of characters, that might not be the same as
021: * bytes).
022: *
023: * @author <a href="mailto:H.Zeller@acm.org">Henner Zeller</a>
024: */
025: public final class NullDevice implements Device, Serializable {
026:
027: public static final NullDevice DEFAULT = new NullDevice();
028:
029: private long byteCount;
030:
031: public NullDevice() {
032: byteCount = 0;
033: }
034:
035: public boolean isSizePreserving() {
036: return true;
037: }
038:
039: /**
040: * Flush this Device.
041: */
042: public void flush() {
043: }
044:
045: public void close() {
046: }
047:
048: /**
049: * returns the number of bytes written to this data sink.
050: */
051: public long getSize() {
052: return byteCount;
053: }
054:
055: /**
056: * reset the number of bytes to zero.
057: */
058: public void resetSize() {
059: byteCount = 0;
060: }
061:
062: /**
063: * Print a character.
064: */
065: public Device print(char c) {
066: ++byteCount;
067: return this ;
068: }
069:
070: /**
071: * Print a character array.
072: */
073: public Device print(char[] c) {
074: if (c != null)
075: byteCount += c.length;
076: return this ;
077: }
078:
079: /**
080: * Print len characters from the specified char array starting at offset
081: * off to this Device.
082: */
083: public Device print(char[] c, int start, int len) {
084: byteCount += len;
085: return this ;
086: }
087:
088: //-- print basic objects --
089:
090: /**
091: * Print a String.
092: */
093: public Device print(String s) {
094: if (s != null)
095: byteCount += s.length();
096: return this ;
097: }
098:
099: /**
100: * Print an integer.
101: */
102: public Device print(int i) {
103: byteCount += String.valueOf(i).length();
104: return this ;
105: }
106:
107: /**
108: * Print any Object
109: */
110: public Device print(Object o) {
111: if (o != null)
112: byteCount += o.toString().length();
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) {
124: ++byteCount;
125: return this ;
126: }
127:
128: /**
129: * Writes b.length bytes from the specified byte array to this
130: * output stream.
131: */
132: public Device write(byte b[]) {
133: if (b != null)
134: byteCount += b.length;
135: return this ;
136: }
137:
138: /**
139: * Writes len bytes from the specified byte array starting at offset
140: * off to this Device.
141: */
142: public Device write(byte b[], int off, int len) {
143: if (b != null)
144: byteCount += len;
145: return this;
146: }
147: }
|