KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: NullDevice.java,v 1.4 2005/02/11 15:10:35 blueshift 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.Serializable JavaDoc;
17
18
19 /**
20  * Device, that discards everything. For debugging purposes.
21  * Counts the number of bytes written (not exactly, since for print() methods,
22  * it counts the number of characters, that might not be the same as
23  * bytes).
24  *
25  * @author <a HREF="mailto:H.Zeller@acm.org">Henner Zeller</a>
26  * @version $Revision: 1.4 $
27  */

28 public final class NullDevice implements Device, Serializable JavaDoc {
29
30     public static NullDevice DEFAULT = new NullDevice();
31
32     private long byteCount;
33
34     public NullDevice() {
35         byteCount = 0;
36     }
37
38     public boolean isSizePreserving() { return true; }
39
40     /**
41      * Flush this Device.
42      */

43     public void flush() { }
44
45     public void close() { }
46
47     /**
48      * returns the number of bytes written to this data sink.
49      */

50     public long getSize() { return byteCount; }
51
52     /**
53      * reset the number of bytes to zero.
54      */

55     public void resetSize() { byteCount = 0; }
56
57     /**
58      * Print a character.
59      */

60     public Device print(char c) {
61         ++byteCount;
62         return this;
63     }
64
65     /**
66      * Print a character array.
67      */

68     public Device print(char[] c) {
69         if (c != null) byteCount += c.length;
70         return this;
71     }
72
73     /**
74      * Print len characters from the specified char array starting at offset
75      * off to this Device.
76      */

77     public Device print(char[] c, int start, int len) {
78         byteCount += len;
79         return this;
80     }
81
82     //-- print basic objects --
83

84     /**
85      * Print a String.
86      */

87     public Device print(String JavaDoc s) {
88         if (s != null) byteCount += s.length();
89         return this;
90     }
91
92     /**
93      * Print an integer.
94      */

95     public Device print(int i) {
96         byteCount += String.valueOf(i).length();
97         return this;
98     }
99
100     /**
101      * Print any Object
102      */

103     public Device print(Object JavaDoc o) {
104         if (o != null) byteCount += o.toString().length();
105         return this;
106     }
107
108     /*-------------*
109      ** Methods which write raw bytes to the Device. Much like an OutputStream.
110      **-------------*/

111
112     /**
113      * Writes the specified byte to this data output stream.
114      */

115     public Device write(int c) {
116         ++byteCount;
117         return this;
118     }
119
120     /**
121      * Writes b.length bytes from the specified byte array to this
122      * output stream.
123      */

124     public Device write(byte b[]) {
125         if (b != null) byteCount += b.length;
126         return this;
127     }
128
129     /**
130      * Writes len bytes from the specified byte array starting at offset
131      * off to this Device.
132      */

133     public Device write(byte b[], int off, int len) {
134         if (b != null) byteCount += len;
135         return this;
136     }
137 }
138
139
140
Popular Tags