KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > io > DataOutputStream


1 package com.quadcap.sql.io;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.*;
42
43 import java.io.DataOutput JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.io.OutputStream JavaDoc;
46
47 import com.quadcap.util.Debug;
48 import com.quadcap.util.Util;
49
50 /**
51  * This class implements data output somewhat more efficiently than
52  * the JDK version. It also implements buffering and counting,
53  * to help us avoid extra layers of stream classes.
54  *
55  * 4/2/03: XX What JDK version? this hasn't been examined recently...
56  *
57  * @author Stan Bailes
58  */

59 public class DataOutputStream extends OutputStream JavaDoc implements DataOutput JavaDoc {
60     static final int MAX = 4096;
61     OutputStream JavaDoc out;
62     byte[] buf = new byte[MAX];
63     byte[] tbuf = new byte[16];
64     Object JavaDoc[] bufs = null;
65     int bufsCnt = 0;
66     int pos = 0;
67     int siz = -1;
68     int count = 0;
69
70     public DataOutputStream(OutputStream JavaDoc out) {
71         this.out = out;
72     }
73
74     public void reset(OutputStream JavaDoc out) {
75         this.out = out;
76         bufsCnt = 0;
77         pos = 0;
78         count = 0;
79         siz = -1;
80         if (bufs != null && bufs.length > 0) {
81             buf = (byte[])bufs[0];
82         }
83     }
84
85     final Object JavaDoc[] resize(Object JavaDoc[] array, int size) {
86         Object JavaDoc[] narray = new Object JavaDoc[size];
87         System.arraycopy(array, 0, narray, 0, array.length);
88         return narray;
89     }
90
91     public final void writeBuf() throws IOException JavaDoc {
92         //#ifdef DEBUG
93
if (trace) Debug.println("writeBuf(), buf = " + buf + ", bufsCnt = " +
94                                  bufsCnt);
95         //#endif
96

97         if (out != null) {
98             //#ifdef DEBUG
99
if (trace) Debug.println("-----------------------------write:\n " +
100                                      Util.strBytes(buf, 0, MAX));
101             //#endif
102
out.write(buf, 0, MAX);
103             count += MAX;
104         } else {
105             if (bufs == null) {
106                 bufs = new Object JavaDoc[8];
107             } else if (bufsCnt+1 >= bufs.length) {
108                 bufs = resize(bufs, bufsCnt * 2);
109             }
110             bufs[bufsCnt++] = buf;
111             buf = (byte[])bufs[bufsCnt];
112             if (buf == null) {
113                 buf = new byte[MAX];
114                 bufs[bufsCnt] = buf;
115             }
116         }
117         pos = 0;
118     }
119
120     public final void write(int c) throws IOException JavaDoc {
121         //#ifdef DEBUG
122
if (trace) Debug.println("[" + pos + "]: write(" + c + ")");
123         //#endif
124
if (pos >= MAX) {
125             writeBuf();
126         }
127         buf[pos++] = (byte)c;
128     }
129
130     public final void write(byte[] b) throws IOException JavaDoc {
131         write(b, 0, b.length);
132     }
133     
134     public final void write(byte[] b, int off, int len) throws IOException JavaDoc {
135         //#ifdef DEBUG
136
if (trace) {
137             Debug.println("[" + pos + "]: write(" +
138                           Util.strBytes(b, off, len) + ")");
139         }
140         //#endif
141
if (out != null && len > MAX) {
142             flush();
143             out.write(b, off, len);
144             count += len;
145             return;
146         }
147         int npos = pos + len;
148         while (npos >= MAX) {
149             final int slen = MAX - pos;
150             System.arraycopy(b, off, buf, pos, slen);
151             writeBuf();
152             len -= slen;
153             off += slen;
154             npos = pos + len;
155         }
156         if (len > 0) {
157             System.arraycopy(b, off, buf, pos, len);
158             pos = npos;
159         }
160     }
161
162     public final void writeBoolean(boolean v) throws IOException JavaDoc {
163     write(v ? 1 : 0);
164     }
165
166     public final void writeByte(int v) throws IOException JavaDoc {
167         //#ifdef DEBUG
168
if (trace) Debug.println("writeByte(" + v + ")");
169         //#endif
170
if (pos >= MAX) writeBuf();
171         buf[pos++] = (byte)v;
172     }
173     
174     //#ifdef SMALLDB
175
public void writeLong(long v) throws IOException JavaDoc {
176         while (true) {
177             if (pos >= MAX) writeBuf();
178             if ((v & 0xffffffffffffff80L) != 0) {
179                 buf[pos++] = (byte)(0x80 + (v & 0x7f));
180                 v >>>= 7;
181             } else {
182                 buf[pos++] = (byte)(v & 0x7f);
183                 break;
184             }
185         }
186     }
187     public void writeInt(int v) throws IOException JavaDoc {
188         //#ifdef DEBUG
189
if (trace) Debug.println("[" + pos + "].writeInt(" + v + ")");
190         //#endif
191
while (true) {
192             if (pos >= MAX) writeBuf();
193             if ((v & 0xffffff80) != 0) {
194                 buf[pos++] = (byte)(0x80 + (v & 0x7f));
195                 v >>>= 7;
196             } else {
197                 buf[pos++] = (byte)(v & 0x7f);
198                 break;
199             }
200         }
201     }
202
203     public void writeShort(int v) throws IOException JavaDoc {
204         writeInt(v);
205     }
206     public void writeChar(int v) throws IOException JavaDoc {
207         writeInt(v);
208     }
209     
210     //#else
211
//- public final void writeShort(int v) throws IOException {
212
//#ifdef DEBUG
213
//- if (trace) Debug.println("writeShort(" + v + ")");
214
//#endif
215
//- if (pos + 1 >= MAX) {
216
//- if (pos >= MAX) writeBuf();
217
//- buf[pos++] = (byte)((v >>> 8) & 0xFF);
218
//- if (pos >= MAX) writeBuf();
219
//- buf[pos++] = (byte)((v >>> 0) & 0xFF);
220
//- } else {
221
//- buf[pos++] = (byte)((v >>> 8) & 0xFF);
222
//- buf[pos++] = (byte)((v >>> 0) & 0xFF);
223
//- }
224
//- }
225
//-
226
//- public final void writeChar(int v) throws IOException {
227
//- writeShort(v);
228
//- }
229
//-
230
//- public final void writeInt(int v) throws IOException {
231
//#ifdef DEBUG
232
//- if (trace) Debug.println("writeInt(" + v + ") " +
233
//- Util.hexBytes(Util.bytes(v)));
234
//#endif
235
//- tbuf[0] = (byte)((v >>> 24) & 0xFF);
236
//- tbuf[1] = (byte)((v >>> 16) & 0xFF);
237
//- tbuf[2] = (byte)((v >>> 8 ) & 0xFF);
238
//- tbuf[3] = (byte)((v >>> 0 ) & 0xFF);
239
//- write(tbuf, 0, 4);
240
//- }
241
//-
242
//- public final void writeLong(long v) throws IOException {
243
//#ifdef DEBUG
244
//- if (trace) Debug.println("writeLong(" + v + ")");
245
//#endif
246
//-
247
//- tbuf[0] = (byte)((v >>> 56) & 0xFF);
248
//- tbuf[1] = (byte)((v >>> 48) & 0xFF);
249
//- tbuf[2] = (byte)((v >>> 40) & 0xFF);
250
//- tbuf[3] = (byte)((v >>> 32) & 0xFF);
251
//- tbuf[4] = (byte)((v >>> 24) & 0xFF);
252
//- tbuf[5] = (byte)((v >>> 16) & 0xFF);
253
//- tbuf[6] = (byte)((v >>> 8 ) & 0xFF);
254
//- tbuf[7] = (byte)((v >>> 0 ) & 0xFF);
255
//- write(tbuf, 0, 8);
256
//- }
257
//#endif
258

259     public final void writeFloat(float v) throws IOException JavaDoc {
260     writeInt(Float.floatToIntBits(v));
261     }
262
263     public final void writeDouble(double v) throws IOException JavaDoc {
264     writeLong(Double.doubleToLongBits(v));
265     }
266
267     public final void writeBytes(String JavaDoc s) throws IOException JavaDoc {
268         //#ifdef DEBUG
269
if (trace) Debug.println("writeBytes(" + s + ")");
270         //#endif
271
int len = s.length();
272         int off = 0;
273         while (len + pos >= MAX) {
274             int slen = MAX - pos;
275             s.getBytes(off, off + slen, buf, pos);
276             writeBuf();
277             off += slen;
278             len -= slen;
279         }
280         if (len > 0) {
281             s.getBytes(off, len, buf, pos);
282             pos += len;
283         }
284     }
285     
286     public final void writeChars(String JavaDoc s) throws IOException JavaDoc {
287         //#ifdef DEBUG
288
if (trace) Debug.println("writeChars(" + s + ")");
289         //#endif
290
final int len = s.length();
291     for (int i = 0 ; i < len ; i++) {
292         int v = s.charAt(i);
293         writeChar(v);
294     }
295     }
296
297     public final void writeUTF(String JavaDoc str) throws IOException JavaDoc {
298         throw new IOException JavaDoc("UTF write not implemented");
299     }
300
301     public final void flush() throws IOException JavaDoc {
302         if (out != null) {
303             //#ifdef DEBUG
304
if (trace) Debug.println("flush, pos = " + pos);
305             //#endif
306
if (pos > 0) {
307                 //#ifdef DEBUG
308
if (trace) Debug.println("flush(" + pos + "): " +
309                                          Util.strBytes(buf, 0, pos));
310                 //#endif
311
out.write(buf, 0, pos);
312                 count += pos;
313                 pos = 0;
314             }
315             out.flush();
316         }
317     }
318
319     public final void close() throws IOException JavaDoc {
320         flush();
321         if (out != null) out.close();
322     }
323
324     public int size() {
325         int s = bufsCnt * MAX + pos;
326         if (siz >= 0) {
327             if (s > siz) siz = -1;
328             else s = siz;
329         }
330         return s;
331     }
332
333     public final void setCount(int count) {
334         this.count = count;
335     }
336
337     public final int getCount() {
338         if (out == null) {
339             return count;
340         } else {
341             return count + size();
342         }
343     }
344
345     public final void setPosition(int p) {
346         //#ifdef DEBUG
347
if (trace) Debug.println("setPosition(" + p + ")");
348         //#endif
349
int s = size();
350         if (p < s && s > siz) siz = s;
351         bufsCnt = p / MAX;
352         pos = p % MAX;
353         if (bufs != null) {
354             buf = (byte[])bufs[bufsCnt];
355         }
356     }
357
358     public final int getPosition() {
359         return size();
360     }
361
362     public byte[] toByteArray() {
363         int len = size();
364         setPosition(len);
365         byte[] ret = new byte[len];
366         int p = 0;
367         for (int i = 0; i < bufsCnt; i++) {
368             byte[] b = (byte[])bufs[i];
369             System.arraycopy(b, 0, ret, p, Math.min(MAX, len));
370             p += MAX;
371             len -= MAX;
372         }
373         if (buf != null) {
374             System.arraycopy(buf, 0, ret, p, pos);
375         }
376         return ret;
377     }
378
379     //#ifdef DEBUG
380
public static void test1() throws IOException JavaDoc {
381         FileOutputStream fos = new FileOutputStream("dos.out");
382         ObjectOutputStream oos = new ObjectOutputStream(fos);
383         byte[] bufx = new byte[2000];
384         for (int i = 0; i < bufx.length; i++) {
385             bufx[i] = (byte)(i & 0xff);
386         }
387         int cnt = 0;
388         for (int i = 1; i < bufx.length; i++) {
389             oos.write(bufx, 0, i);
390             cnt += i;
391             if (cnt != oos.getCount()) throw new RuntimeException JavaDoc("bad count, i = " + i);
392             oos.flush();
393             
394         }
395         oos.flush();
396         fos.close();
397
398         FileInputStream fis = new FileInputStream("dos.out");
399         BufferedInputStream bis = new BufferedInputStream(fis);
400         for (int i = 1; i < bufx.length; i++) {
401             bis.read(bufx, 0, i);
402             for (int j = 0; j < i; j++) {
403                 if ((bufx[j] & 0xff)!= (j&0xff)) {
404                     throw new RuntimeException JavaDoc("bad, i = " + i + ", j = " + j);
405                 }
406             }
407         }
408         fis.close();
409     }
410             
411             
412
413     public static void main(String JavaDoc args[]) {
414         try {
415 // ObjectOutputStream out = new ObjectOutputStream();
416
// out.setPosition(6);
417
// out.writeInt(0xaabbccdd);
418
// out.setPosition(0);
419
// out.writeInt(0xffeeddcc);
420
// for (int i = 0; i < 2000; i++) {
421
// out.writeInt(i);
422
// }
423
// out.setPosition(0);
424
// out.writeInt(0x11223344);
425
// out.writeInt(0x55667788);
426
// System.out.println(Util.strBytes(out.toByteArray()));
427
test1();
428         } catch (Throwable JavaDoc t) {
429             Debug.print(t);
430         }
431     }
432
433     boolean trace = false;
434     public void setTrace(boolean t) {
435         this.trace = t;
436     }
437     //#endif
438
}
439
Popular Tags