001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.lib;
032:
033: import java.io.DataOutput;
034: import java.io.IOException;
035: import java.io.OutputStream;
036: import java.io.UTFDataFormatException;
037: import java.io.UnsupportedEncodingException;
038:
039: /**
040: * This class is a replacement for both java.io.ByteArrayOuputStream
041: * (without synchronization) and java.io.DataOutputStream
042: *
043: * @author fredt@users
044: * @version 1.7.2
045: * @since 1.7.0
046: */
047: public class HsqlByteArrayOutputStream extends java.io.OutputStream
048: implements DataOutput {
049:
050: protected byte[] buf;
051: protected int count;
052:
053: public HsqlByteArrayOutputStream() {
054: this (128);
055: }
056:
057: public HsqlByteArrayOutputStream(int size) {
058:
059: if (size < 128) {
060: size = 128;
061: }
062:
063: buf = new byte[size];
064: }
065:
066: public HsqlByteArrayOutputStream(byte[] buffer) {
067: buf = buffer;
068: }
069:
070: // methods that implement dataOutput
071: public final void writeShort(int v) {
072:
073: ensureRoom(2);
074:
075: buf[count++] = (byte) (v >>> 8);
076: buf[count++] = (byte) v;
077: }
078:
079: public final void writeInt(int v) {
080:
081: if (count + 4 > buf.length) {
082: ensureRoom(4);
083: }
084:
085: buf[count++] = (byte) (v >>> 24);
086: buf[count++] = (byte) (v >>> 16);
087: buf[count++] = (byte) (v >>> 8);
088: buf[count++] = (byte) v;
089: }
090:
091: public final void writeLong(long v) {
092: writeInt((int) (v >>> 32));
093: writeInt((int) v);
094: }
095:
096: public final void writeBytes(String s) {
097:
098: int len = s.length();
099:
100: ensureRoom(len);
101:
102: for (int i = 0; i < len; i++) {
103: buf[count++] = (byte) s.charAt(i);
104: }
105: }
106:
107: public final void writeFloat(float v) {
108: writeInt(Float.floatToIntBits(v));
109: }
110:
111: public final void writeDouble(double v) {
112: writeLong(Double.doubleToLongBits(v));
113: }
114:
115: public void writeBoolean(boolean v) throws IOException {
116:
117: ensureRoom(1);
118:
119: buf[count++] = (byte) (v ? 1 : 0);
120: }
121:
122: public void writeByte(int v) throws IOException {
123:
124: ensureRoom(1);
125:
126: buf[count++] = (byte) (v);
127: }
128:
129: public void writeChar(int v) throws IOException {
130:
131: ensureRoom(2);
132:
133: buf[count++] = (byte) (v >>> 8);
134: buf[count++] = (byte) v;
135: }
136:
137: public void writeChars(String s) throws IOException {
138:
139: int len = s.length();
140:
141: ensureRoom(len * 2);
142:
143: for (int i = 0; i < len; i++) {
144: int v = s.charAt(i);
145:
146: buf[count++] = (byte) (v >>> 8);
147: buf[count++] = (byte) v;
148: }
149: }
150:
151: public void writeUTF(String str) throws IOException {
152:
153: int len = str.length();
154:
155: if (len > 0xffff) {
156: throw new UTFDataFormatException();
157: }
158:
159: ensureRoom(len * 3 + 2);
160:
161: //
162: int initpos = count;
163:
164: count += 2;
165:
166: StringConverter.writeUTF(str, this );
167:
168: int bytecount = count - initpos - 2;
169:
170: if (bytecount > 0xffff) {
171: count = initpos;
172:
173: throw new UTFDataFormatException();
174: }
175:
176: buf[initpos++] = (byte) (bytecount >>> 8);
177: buf[initpos] = (byte) bytecount;
178: }
179:
180: /**
181: * does nothing
182: */
183: public void flush() throws java.io.IOException {
184: super .flush();
185: }
186:
187: // methods that extend java.io.OutputStream
188: public void write(int b) {
189:
190: ensureRoom(1);
191:
192: buf[count++] = (byte) b;
193: }
194:
195: public void write(byte[] b) {
196: write(b, 0, b.length);
197: }
198:
199: public void write(byte[] b, int off, int len) {
200:
201: ensureRoom(len);
202: System.arraycopy(b, off, buf, count, len);
203:
204: count += len;
205: }
206:
207: public void writeTo(OutputStream out) throws IOException {
208: out.write(buf, 0, count);
209: }
210:
211: public void reset() {
212: count = 0;
213: }
214:
215: public byte[] toByteArray() {
216:
217: byte[] newbuf = new byte[count];
218:
219: System.arraycopy(buf, 0, newbuf, 0, count);
220:
221: return newbuf;
222: }
223:
224: public int size() {
225: return count;
226: }
227:
228: public String toString() {
229: return new String(buf, 0, count);
230: }
231:
232: public String toString(String enc)
233: throws UnsupportedEncodingException {
234: return new String(buf, 0, count, enc);
235: }
236:
237: public void close() throws IOException {
238: }
239:
240: // additional public methods not in similar java.util classes
241: public void fill(int b, int len) {
242:
243: ensureRoom(len);
244:
245: for (int i = 0; i < len; i++) {
246: buf[count++] = (byte) b;
247: }
248: }
249:
250: public byte[] getBuffer() {
251: return this .buf;
252: }
253:
254: protected void ensureRoom(int extra) {
255:
256: int newcount = count + extra;
257: int newsize = buf.length;
258:
259: if (newcount > newsize) {
260: while (newcount > newsize) {
261: newsize *= 2;
262: }
263:
264: byte[] newbuf = new byte[newsize];
265:
266: System.arraycopy(buf, 0, newbuf, 0, count);
267:
268: buf = newbuf;
269: }
270: }
271:
272: protected void reset(int newSize) {
273:
274: count = 0;
275:
276: if (newSize > buf.length) {
277: buf = new byte[newSize];
278: }
279: }
280: }
|