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: /**
018: * A Device, that buffers the data written to it to be
019: * written to some other Device later (see {@link #writeTo(Device)})
020: *
021: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
022: */
023: public final class DeviceBuffer implements Device {
024: // private static final byte[] NULL_STRING = "null".getBytes();
025:
026: private byte[] buffer;
027:
028: private int position = 0;
029:
030: private int capacityIncrement;
031:
032: public DeviceBuffer(int initialCapacity, int capacityIncrement) {
033: buffer = new byte[initialCapacity];
034: this .capacityIncrement = capacityIncrement;
035: }
036:
037: public boolean isSizePreserving() {
038: return true;
039: }
040:
041: public DeviceBuffer(int initialCapacity) {
042: this (initialCapacity, -1);
043: }
044:
045: public DeviceBuffer() {
046: this (2000);
047: }
048:
049: public void flush() {
050: }
051:
052: public void close() {
053: }
054:
055: /**
056: * Print a String.
057: */
058: public Device print(String s) throws IOException {
059:
060: if (s == null)
061: return print("null");
062:
063: int len = s.length();
064: for (int i = 0; i < len; i++) {
065: //
066: // XXX NOTE: This is clearly incorrect for many strings,
067: // but is the only consistent approach within the current
068: // servlet framework. It must suffice until servlet output
069: // streams properly encode their output.
070: //
071: write((byte) s.charAt(i));
072: }
073: return this ;
074:
075: /*
076: correct implementation would be someting like
077: byte[] bytes = NULL_STRING;
078: if ( s!=null )
079: bytes = s.getBytes();
080:
081: return write(bytes);
082: */
083: }
084:
085: /**
086: * Print an array of chars.
087: */
088: public Device print(char[] c) throws IOException {
089: return print(c, 0, c.length - 1);
090: }
091:
092: /**
093: * Print a character array.
094: */
095: public Device print(char[] c, int start, int len)
096: throws IOException {
097: final int end = start + len;
098: for (int i = start; i < end; i++)
099: print(c[i]);
100: return this ;
101: }
102:
103: /**
104: * Print an integer.
105: */
106: public Device print(int i) throws IOException {
107: return print(String.valueOf(i));
108: }
109:
110: /**
111: * Print any Object
112: */
113: public Device print(Object o) throws IOException {
114: if (o != null)
115: return print(o.toString());
116: else
117: return print("null");
118: }
119:
120: /**
121: * Print a character.
122: */
123: public Device print(char c) throws IOException {
124: return print(String.valueOf(c));
125: }
126:
127: /**
128: * Writes the specified byte to this data output stream.
129: */
130: public Device write(int c) throws IOException {
131: return print(String.valueOf(c));
132: }
133:
134: /**
135: * Writes b.length bytes from the specified byte array to this
136: * output stream.
137: *
138: * @throws IOException
139: */
140: public Device write(byte b) throws IOException {
141: while (position + 1 > buffer.length)
142: incrementCapacity();
143: buffer[position++] = b;
144: return this ;
145: }
146:
147: /**
148: * Writes b.length bytes from the specified byte array to this
149: * output stream.
150: */
151: public Device write(byte b[]) throws IOException {
152: return write(b, 0, b.length);
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) ? (buffer.length + capacityIncrement)
163: : (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 {
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: public void writeTo(Device d) {
184: try {
185: d.write(buffer, 0, position);
186: } catch (IOException ignore) {
187: }
188: }
189: }
|