KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > lib > HsqlByteArrayOutputStream


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.lib;
33
34 import java.io.DataOutput JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.OutputStream JavaDoc;
37 import java.io.UTFDataFormatException JavaDoc;
38 import java.io.UnsupportedEncodingException JavaDoc;
39
40 /**
41  * This class is a replacement for both java.io.ByteArrayOuputStream
42  * (without synchronization) and java.io.DataOutputStream
43  *
44  * @author fredt@users
45  * @version 1.7.2
46  * @since 1.7.0
47  */

48 public class HsqlByteArrayOutputStream extends java.io.OutputStream JavaDoc
49 implements DataOutput JavaDoc {
50
51     protected byte[] buf;
52     protected int count;
53
54     public HsqlByteArrayOutputStream() {
55         this(128);
56     }
57
58     public HsqlByteArrayOutputStream(int size) {
59
60         if (size < 128) {
61             size = 128;
62         }
63
64         buf = new byte[size];
65     }
66
67     public HsqlByteArrayOutputStream(byte[] buffer) {
68         buf = buffer;
69     }
70
71     // methods that implement dataOutput
72
public final void writeShort(int v) {
73
74         ensureRoom(2);
75
76         buf[count++] = (byte) (v >>> 8);
77         buf[count++] = (byte) v;
78     }
79
80     public final void writeInt(int v) {
81
82         if (count + 4 > buf.length) {
83             ensureRoom(4);
84         }
85
86         buf[count++] = (byte) (v >>> 24);
87         buf[count++] = (byte) (v >>> 16);
88         buf[count++] = (byte) (v >>> 8);
89         buf[count++] = (byte) v;
90     }
91
92     public final void writeLong(long v) {
93         writeInt((int) (v >>> 32));
94         writeInt((int) v);
95     }
96
97     public final void writeBytes(String JavaDoc s) {
98
99         int len = s.length();
100
101         ensureRoom(len);
102
103         for (int i = 0; i < len; i++) {
104             buf[count++] = (byte) s.charAt(i);
105         }
106     }
107
108     public final void writeFloat(float v) {
109         writeInt(Float.floatToIntBits(v));
110     }
111
112     public final void writeDouble(double v) {
113         writeLong(Double.doubleToLongBits(v));
114     }
115
116     public void writeBoolean(boolean v) throws IOException JavaDoc {
117
118         ensureRoom(1);
119
120         buf[count++] = (byte) (v ? 1
121                                  : 0);
122     }
123
124     public void writeByte(int v) throws IOException JavaDoc {
125
126         ensureRoom(1);
127
128         buf[count++] = (byte) (v);
129     }
130
131     public void writeChar(int v) throws IOException JavaDoc {
132
133         ensureRoom(2);
134
135         buf[count++] = (byte) (v >>> 8);
136         buf[count++] = (byte) v;
137     }
138
139     public void writeChars(String JavaDoc s) throws IOException JavaDoc {
140
141         int len = s.length();
142
143         ensureRoom(len * 2);
144
145         for (int i = 0; i < len; i++) {
146             int v = s.charAt(i);
147
148             buf[count++] = (byte) (v >>> 8);
149             buf[count++] = (byte) v;
150         }
151     }
152
153     public void writeUTF(String JavaDoc str) throws IOException JavaDoc {
154
155         int len = str.length();
156
157         if (len > 0xffff) {
158             throw new UTFDataFormatException JavaDoc();
159         }
160
161         ensureRoom(len * 3 + 2);
162
163         //
164
int initpos = count;
165
166         count += 2;
167
168         StringConverter.writeUTF(str, this);
169
170         int bytecount = count - initpos - 2;
171
172         if (bytecount > 0xffff) {
173             count = initpos;
174
175             throw new UTFDataFormatException JavaDoc();
176         }
177
178         buf[initpos++] = (byte) (bytecount >>> 8);
179         buf[initpos] = (byte) bytecount;
180     }
181
182     /**
183      * does nothing
184      */

185     public void flush() throws java.io.IOException JavaDoc {
186         super.flush();
187     }
188
189     // methods that extend java.io.OutputStream
190
public void write(int b) {
191
192         ensureRoom(1);
193
194         buf[count++] = (byte) b;
195     }
196
197     public void write(byte[] b) {
198         write(b, 0, b.length);
199     }
200
201     public void write(byte[] b, int off, int len) {
202
203         ensureRoom(len);
204         System.arraycopy(b, off, buf, count, len);
205
206         count += len;
207     }
208
209     public void writeTo(OutputStream JavaDoc out) throws IOException JavaDoc {
210         out.write(buf, 0, count);
211     }
212
213     public void reset() {
214         count = 0;
215     }
216
217     public byte[] toByteArray() {
218
219         byte[] newbuf = new byte[count];
220
221         System.arraycopy(buf, 0, newbuf, 0, count);
222
223         return newbuf;
224     }
225
226     public int size() {
227         return count;
228     }
229
230     public String JavaDoc toString() {
231         return new String JavaDoc(buf, 0, count);
232     }
233
234     public String JavaDoc toString(String JavaDoc enc) throws UnsupportedEncodingException JavaDoc {
235         return new String JavaDoc(buf, 0, count, enc);
236     }
237
238     public void close() throws IOException JavaDoc {}
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 }
281
Popular Tags