KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > file > RandomAccessOutputStream


1 package com.quadcap.sql.file;
2
3 /* Copyright 1997 - 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.IOException JavaDoc;
42 import java.io.OutputStream JavaDoc;
43
44 import com.quadcap.util.Debug;
45 import com.quadcap.util.Util;
46
47 /**
48  * An output stream attached to a <code>RandomAccess</code> object.
49  *
50  * @author Stan Bailes
51  */

52 public class RandomAccessOutputStream extends OutputStream JavaDoc {
53     RandomAccess ra;
54     int position;
55     byte[] buf = new byte[1];
56     
57     public RandomAccessOutputStream(RandomAccess ra) {
58     this.ra = ra;
59     this.position = 0;
60     }
61
62     public int getPosition() { return position; }
63     public void setPosition(int p) { position = p; }
64     public long size() { return ra.size(); }
65     public void resize(long size) throws IOException JavaDoc {
66     ra.resize(size);
67     }
68
69     /**
70      * Writes the specified byte to this output stream.
71      * <p>
72      * Subclasses of <code>OutputStream</code> must provide an
73      * implementation for this method.
74      *
75      * @param b the <code>byte</code>.
76      * @exception IOException if an I/O error occurs.
77      */

78     public void write(int b) throws IOException JavaDoc {
79     buf[0] = (byte)b;
80     write(buf, 0, buf.length);
81     }
82
83     /**
84      * Writes <code>b.length</code> bytes from the specified byte array
85      * to this output stream.
86      * <p>
87      * The <code>write</code> method of <code>OutputStream</code> calls
88      * the <code>write</code> method of three arguments with the three
89      * arguments <code>b</code>, <code>0</code>, and
90      * <code>b.length</code>.
91      *
92      * @param b the data.
93      * @exception IOException if an I/O error occurs.
94      */

95     public void write(byte b[]) throws IOException JavaDoc {
96     write(b, 0, b.length);
97     }
98
99     /**
100      * Writes <code>len</code> bytes from the specified byte array
101      * starting at offset <code>off</code> to this output stream.
102      * <p>
103      * The <code>write</code> method of <code>OutputStream</code> calls
104      * the write method of one argument on each of the bytes to be
105      * written out. Subclasses are encouraged to override this method and
106      * provide a more efficient implementation.
107      *
108      * @param b the data.
109      * @param off the start offset in the data.
110      * @param len the number of bytes to write.
111      * @exception IOException if an I/O error occurs.
112      */

113     public void write(byte b[], int off, int len) throws IOException JavaDoc {
114     if (position + len > ra.size()) ra.resize(position + len);
115     ra.write(position, b, off, len);
116     position += len;
117     }
118
119     /**
120      * Flushes this output stream and forces any buffered output bytes
121      * to be written out.
122      * <p>
123      * The <code>flush</code> method of <code>RandomAccessOutputStream</code>
124      * does nothing. Perhaps we could call the flush method of the underlying
125      * BlockFile?
126      *
127      * @exception IOException if an I/O error occurs.
128      */

129     public void flush() throws IOException JavaDoc {
130     }
131
132     /**
133      * Closes this output stream and releases any system resources
134      * associated with this stream.
135      * <p>
136      * The <code>close</code> method of <code>OutputStream</code> does nothing.
137      *
138      * @exception IOException if an I/O error occurs.
139      */

140     public void close() throws IOException JavaDoc {
141     ra.close();
142     }
143 }
144
Popular Tags