Quadcap Embeddable Database

com/quadcap/sql/file/RandomAccessOutputStream.java

Go to the documentation of this file.
00001 package com.quadcap.sql.file; 00002 00003 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.IOException; 00042 import java.io.OutputStream; 00043 00044 import com.quadcap.util.Debug; 00045 import com.quadcap.util.Util; 00046 00047 /** 00048 * An output stream attached to a <code>RandomAccess</code> object. 00049 * 00050 * @author Stan Bailes 00051 */ 00052 public class RandomAccessOutputStream extends OutputStream { 00053 RandomAccess ra; 00054 int position; 00055 byte[] buf = new byte[1]; 00056 00057 public RandomAccessOutputStream(RandomAccess ra) { 00058 this.ra = ra; 00059 this.position = 0; 00060 } 00061 00062 public int getPosition() { return position; } 00063 public void setPosition(int p) { position = p; } 00064 public long size() { return ra.size(); } 00065 public void resize(long size) throws IOException { 00066 ra.resize(size); 00067 } 00068 00069 /** 00070 * Writes the specified byte to this output stream. 00071 * <p> 00072 * Subclasses of <code>OutputStream</code> must provide an 00073 * implementation for this method. 00074 * 00075 * @param b the <code>byte</code>. 00076 * @exception IOException if an I/O error occurs. 00077 */ 00078 public void write(int b) throws IOException { 00079 buf[0] = (byte)b; 00080 write(buf, 0, buf.length); 00081 } 00082 00083 /** 00084 * Writes <code>b.length</code> bytes from the specified byte array 00085 * to this output stream. 00086 * <p> 00087 * The <code>write</code> method of <code>OutputStream</code> calls 00088 * the <code>write</code> method of three arguments with the three 00089 * arguments <code>b</code>, <code>0</code>, and 00090 * <code>b.length</code>. 00091 * 00092 * @param b the data. 00093 * @exception IOException if an I/O error occurs. 00094 */ 00095 public void write(byte b[]) throws IOException { 00096 write(b, 0, b.length); 00097 } 00098 00099 /** 00100 * Writes <code>len</code> bytes from the specified byte array 00101 * starting at offset <code>off</code> to this output stream. 00102 * <p> 00103 * The <code>write</code> method of <code>OutputStream</code> calls 00104 * the write method of one argument on each of the bytes to be 00105 * written out. Subclasses are encouraged to override this method and 00106 * provide a more efficient implementation. 00107 * 00108 * @param b the data. 00109 * @param off the start offset in the data. 00110 * @param len the number of bytes to write. 00111 * @exception IOException if an I/O error occurs. 00112 */ 00113 public void write(byte b[], int off, int len) throws IOException { 00114 if (position + len > ra.size()) ra.resize(position + len); 00115 ra.write(position, b, off, len); 00116 position += len; 00117 } 00118 00119 /** 00120 * Flushes this output stream and forces any buffered output bytes 00121 * to be written out. 00122 * <p> 00123 * The <code>flush</code> method of <code>RandomAccessOutputStream</code> 00124 * does nothing. Perhaps we could call the flush method of the underlying 00125 * BlockFile? 00126 * 00127 * @exception IOException if an I/O error occurs. 00128 */ 00129 public void flush() throws IOException { 00130 } 00131 00132 /** 00133 * Closes this output stream and releases any system resources 00134 * associated with this stream. 00135 * <p> 00136 * The <code>close</code> method of <code>OutputStream</code> does nothing. 00137 * 00138 * @exception IOException if an I/O error occurs. 00139 */ 00140 public void close() throws IOException { 00141 ra.close(); 00142 } 00143 }