![]() |
Quadcap Embeddable Database |
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.InputStream; 00042 import java.io.IOException; 00043 00044 import com.quadcap.util.Debug; 00045 00046 /** 00047 * An input stream attached to a <code>RandomAccess</code> object. 00048 * 00049 * @author Stan Bailes 00050 */ 00051 public class RandomAccessInputStream extends InputStream { 00052 RandomAccess ra; 00053 int position; 00054 byte[] buf1 = new byte[1]; 00055 00056 public RandomAccessInputStream(RandomAccess ra) { 00057 this.ra = ra; 00058 this.position = 0; 00059 } 00060 00061 public int getPosition() { return position; } 00062 public void setPosition(int p) { position = p; } 00063 00064 /** 00065 * Reads the next byte of data from this input stream. The value 00066 * byte is returned as an <code>int</code> in the range 00067 * <code>0</code> to <code>255</code>. If no byte is available 00068 * because the end of the stream has been reached, the value 00069 * <code>-1</code> is returned. 00070 * 00071 * @return the next byte of data, or <code>-1</code> if the end of the 00072 * stream is reached. 00073 * @exception IOException if an I/O error occurs. 00074 */ 00075 public int read() throws IOException { 00076 if (position >= ra.size()) return -1; 00077 00078 ra.read(position, buf1, 0, 1); 00079 position++; 00080 return buf1[0] & 0xff; 00081 } 00082 00083 /** 00084 * Reads up to <code>len</code> bytes of data from this input stream 00085 * into an array of bytes. This method blocks until some input is 00086 * available. If the first argument is <code>null,</code> up to 00087 * <code>len</code> bytes are read and discarded. 00088 * 00089 * @param b the buffer into which the data is read. 00090 * @param off the start offset of the data. 00091 * @param len the maximum number of bytes read. 00092 * @return the total number of bytes read into the buffer, or 00093 * <code>-1</code> if there is no more data because the end of 00094 * the stream has been reached. 00095 * @exception IOException if an I/O error occurs. 00096 */ 00097 public int read(byte b[], int off, int len) throws IOException { 00098 if (position >= ra.size()) return -1; 00099 if (ra.size() - position < len) len = (int)(ra.size() - position); 00100 00101 if (b != null) ra.read(position, b, off, len); 00102 position += len; 00103 return len; 00104 } 00105 00106 /** 00107 * Skips over and discards <code>n</code> bytes of data from this 00108 * input stream. The <code>skip</code> method may, for a variety of 00109 * reasons, end up skipping over some smaller number of bytes, 00110 * possibly <code>0</code>. The actual number of bytes skipped is 00111 * returned. 00112 * <p> 00113 * The <code>skip</code> method of <code>InputStream</code> creates 00114 * a byte array of length <code>n</code> and then reads into it until 00115 * <code>n</code> bytes have been read or the end of the stream has 00116 * been reached. Subclasses are encouraged to provide a more 00117 * efficient implementation of this method. 00118 * 00119 * @param n the number of bytes to be skipped. 00120 * @return the actual number of bytes skipped. 00121 * @exception IOException if an I/O error occurs. 00122 * @since JDK1.0 00123 */ 00124 public long skip(long n) throws IOException { 00125 return read(null, 0, (int)n); 00126 } 00127 00128 /** 00129 * Returns the number of bytes that can be read from this input 00130 * stream without blocking. The available method of 00131 * <code>InputStream</code> returns <code>0</code>. This method 00132 * <B>should</B> be overridden by subclasses. 00133 * 00134 * @return the number of bytes that can be read from this input stream 00135 * without blocking. 00136 * @exception IOException if an I/O error occurs. 00137 */ 00138 public int available() throws IOException { 00139 return (int)(ra.size() - position); 00140 } 00141 00142 /** 00143 * Closes this input stream and releases any system resources 00144 * associated with the stream. 00145 * 00146 * @exception IOException if an I/O error occurs. 00147 */ 00148 public void close() throws IOException { 00149 ra.close(); 00150 } 00151 00152 }