KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: DeviceBuffer.java,v 1.5 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 /**
19  * A Device, that buffers the data written to it to be
20  * written to some other Device later (see {@link #writeTo(Device)})
21  *
22  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
23  * @version $Revision: 1.5 $
24  */

25 public final class DeviceBuffer implements Device {
26     // private static final byte[] NULL_STRING = "null".getBytes();
27

28     private byte[] buffer;
29
30     private int position = 0;
31
32     private int capacityIncrement;
33
34     public DeviceBuffer(int initialCapacity, int capacityIncrement) {
35         buffer = new byte[initialCapacity];
36         this.capacityIncrement = capacityIncrement;
37     }
38
39     public boolean isSizePreserving() { return true; }
40
41
42     public DeviceBuffer(int initialCapacity) {
43         this(initialCapacity, -1);
44     }
45
46
47     public DeviceBuffer() {
48         this(2000);
49     }
50
51     public void flush() { }
52
53     public void close() { }
54
55     /**
56      * Print a String.
57      */

58     public Device print(String JavaDoc s) throws IOException JavaDoc {
59
60         if (s == null)
61             return print("null");
62
63         int len = s.length();
64         for (int i = 0; i < len; i++) {
65             //
66
// XXX NOTE: This is clearly incorrect for many strings,
67
// but is the only consistent approach within the current
68
// servlet framework. It must suffice until servlet output
69
// streams properly encode their output.
70
//
71
write((byte) s.charAt(i));
72         }
73         return this;
74
75         /*
76           correct implementation would be someting like
77          byte[] bytes = NULL_STRING;
78          if ( s!=null )
79          bytes = s.getBytes();
80
81          return write(bytes);
82          */

83     }
84
85     /**
86      * Print an array of chars.
87      */

88     public Device print(char[] c) throws IOException JavaDoc {
89         return print(c, 0, c.length - 1);
90     }
91
92     /**
93      * Print a character array.
94      */

95     public Device print(char[] c, int start, int len) throws IOException JavaDoc {
96         final int end = start + len;
97         for (int i = start; i < end; i++)
98             print(c[i]);
99         return this;
100     }
101
102     /**
103      * Print an integer.
104      */

105     public Device print(int i) throws IOException JavaDoc {
106         return print(String.valueOf(i));
107     }
108
109     /**
110      * Print any Object
111      */

112     public Device print(Object JavaDoc o) throws IOException JavaDoc {
113         if (o != null)
114             return print(o.toString());
115         else
116             return print("null");
117     }
118
119     /**
120      * Print a character.
121      */

122     public Device print(char c) throws IOException JavaDoc {
123         return print(String.valueOf(c));
124     }
125
126     /**
127      * Writes the specified byte to this data output stream.
128      */

129     public Device write(int c) throws IOException JavaDoc {
130         return print(String.valueOf(c));
131     }
132
133     /**
134      * Writes b.length bytes from the specified byte array to this
135      * output stream.
136      *
137      * @throws IOException
138      */

139     public Device write(byte b) throws IOException JavaDoc {
140         while (position + 1 > buffer.length)
141             incrementCapacity();
142         buffer[position++] = b;
143         return this;
144     }
145
146     /**
147      * Writes b.length bytes from the specified byte array to this
148      * output stream.
149      */

150     public Device write(byte b[]) throws IOException JavaDoc {
151         return write(b, 0, b.length);
152     }
153
154
155     public void clear() {
156         position = 0;
157     }
158
159     private void incrementCapacity() {
160         byte[] oldBuffer = buffer;
161
162         int newCapacity = (capacityIncrement > 0) ?
163                 (buffer.length + capacityIncrement) : (buffer.length * 2);
164
165         buffer = new byte[newCapacity];
166         System.arraycopy(oldBuffer, 0, buffer, 0, position);
167     }
168
169     /**
170      * Writes len bytes from the specified byte array starting at offset
171      * off to this output stream.
172      */

173     public Device write(byte b[], int off, int len) throws IOException JavaDoc {
174         while (position + len > buffer.length)
175             incrementCapacity();
176
177         System.arraycopy(b, off, buffer, position, len);
178         position += len;
179
180         return this;
181     }
182
183
184     public void writeTo(Device d) {
185         try {
186             d.write(buffer, 0, position);
187         } catch (IOException JavaDoc ignore) {}
188     }
189 }
190
191
192
Popular Tags