001: package com.quadcap.sql.file;
002:
003: /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.IOException;
042: import java.io.OutputStream;
043:
044: import com.quadcap.util.Debug;
045: import com.quadcap.util.Util;
046:
047: /**
048: * An output stream attached to a <code>RandomAccess</code> object.
049: *
050: * @author Stan Bailes
051: */
052: public class RandomAccessOutputStream extends OutputStream {
053: RandomAccess ra;
054: int position;
055: byte[] buf = new byte[1];
056:
057: public RandomAccessOutputStream(RandomAccess ra) {
058: this .ra = ra;
059: this .position = 0;
060: }
061:
062: public int getPosition() {
063: return position;
064: }
065:
066: public void setPosition(int p) {
067: position = p;
068: }
069:
070: public long size() {
071: return ra.size();
072: }
073:
074: public void resize(long size) throws IOException {
075: ra.resize(size);
076: }
077:
078: /**
079: * Writes the specified byte to this output stream.
080: * <p>
081: * Subclasses of <code>OutputStream</code> must provide an
082: * implementation for this method.
083: *
084: * @param b the <code>byte</code>.
085: * @exception IOException if an I/O error occurs.
086: */
087: public void write(int b) throws IOException {
088: buf[0] = (byte) b;
089: write(buf, 0, buf.length);
090: }
091:
092: /**
093: * Writes <code>b.length</code> bytes from the specified byte array
094: * to this output stream.
095: * <p>
096: * The <code>write</code> method of <code>OutputStream</code> calls
097: * the <code>write</code> method of three arguments with the three
098: * arguments <code>b</code>, <code>0</code>, and
099: * <code>b.length</code>.
100: *
101: * @param b the data.
102: * @exception IOException if an I/O error occurs.
103: */
104: public void write(byte b[]) throws IOException {
105: write(b, 0, b.length);
106: }
107:
108: /**
109: * Writes <code>len</code> bytes from the specified byte array
110: * starting at offset <code>off</code> to this output stream.
111: * <p>
112: * The <code>write</code> method of <code>OutputStream</code> calls
113: * the write method of one argument on each of the bytes to be
114: * written out. Subclasses are encouraged to override this method and
115: * provide a more efficient implementation.
116: *
117: * @param b the data.
118: * @param off the start offset in the data.
119: * @param len the number of bytes to write.
120: * @exception IOException if an I/O error occurs.
121: */
122: public void write(byte b[], int off, int len) throws IOException {
123: if (position + len > ra.size())
124: ra.resize(position + len);
125: ra.write(position, b, off, len);
126: position += len;
127: }
128:
129: /**
130: * Flushes this output stream and forces any buffered output bytes
131: * to be written out.
132: * <p>
133: * The <code>flush</code> method of <code>RandomAccessOutputStream</code>
134: * does nothing. Perhaps we could call the flush method of the underlying
135: * BlockFile?
136: *
137: * @exception IOException if an I/O error occurs.
138: */
139: public void flush() throws IOException {
140: }
141:
142: /**
143: * Closes this output stream and releases any system resources
144: * associated with this stream.
145: * <p>
146: * The <code>close</code> method of <code>OutputStream</code> does nothing.
147: *
148: * @exception IOException if an I/O error occurs.
149: */
150: public void close() throws IOException {
151: ra.close();
152: }
153: }
|