Quadcap Embeddable Server

com/quadcap/io/RandomAccessFileInputStream.java

Go to the documentation of this file.
00001 package com.quadcap.io; 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 import java.io.RandomAccessFile; 00044 00045 import com.quadcap.util.Debug; 00046 00047 /** 00048 * This class implements an input stream filter which keeps track of how 00049 * many bytes have been read. 00050 * 00051 * @author Stan Bailes 00052 */ 00053 public class RandomAccessFileInputStream extends InputStream { 00054 long mark = -1; 00055 RandomAccessFile ra; 00056 00057 /** 00058 * Construct a new RandomAccessFileInputStream rearang from the 00059 * specified file. 00060 * 00061 * @param ra the file 00062 */ 00063 public RandomAccessFileInputStream(RandomAccessFile ra) { 00064 this.ra = ra; 00065 } 00066 00067 /** 00068 * Read the next byte from this input stream. 00069 * 00070 * @exception IOException if an I/O error occurs. 00071 */ 00072 public int read() throws IOException { 00073 // Debug.println(0, "read[" + ra.getFilePointer() + "] (" + 1 + ")"); 00074 return ra.read(); 00075 } 00076 00077 /** 00078 * Read a block of bytes from this input stream. 00079 * 00080 * @exception IOException if an I/O error occcurs. 00081 */ 00082 public int read(byte[] buf, int offset, int count) throws IOException { 00083 // Debug.println(0, "read[" + ra.getFilePointer() + "] (" + count + ")"); 00084 return ra.read(buf, offset, count); 00085 } 00086 00087 /** 00088 * Read a block of bytes from this input stream. 00089 * 00090 * @exception IOException if an I/O error occcurs. 00091 */ 00092 public int read(byte[] buf) throws IOException { 00093 // Debug.println(0, "read[" + ra.getFilePointer() + "] (" + 00094 // buf.length + ")"); 00095 return ra.read(buf); 00096 } 00097 00098 /** 00099 * Return the number of bytes that can be read without blocking. 00100 */ 00101 public int available() throws IOException { 00102 return (int)(ra.length() - ra.getFilePointer()); 00103 } 00104 00105 /** 00106 * Close the stream. 00107 */ 00108 public void close() throws IOException { 00109 ra.close(); 00110 } 00111 00112 /** 00113 * Skip ahead in the stream. 00114 */ 00115 public long skip(long n) throws IOException { 00116 ra.seek(ra.getFilePointer() + n); 00117 return n; 00118 } 00119 00120 /** 00121 * Mark the current position in the input stream. 00122 */ 00123 public void mark(int readLimit) { 00124 try { 00125 this.mark = ra.getFilePointer(); 00126 } catch (IOException e) { 00127 Debug.print(e); 00128 throw new RuntimeException(e.toString()); 00129 } 00130 } 00131 00132 /** 00133 * Return to the previously marked position in the input stream. 00134 */ 00135 public void reset() throws IOException { 00136 if (mark < 0) throw new IOException("no mark"); 00137 ra.seek(mark); 00138 } 00139 00140 /** 00141 * Return true if mark/reset supported (they are.) 00142 */ 00143 public boolean markSupported() { 00144 return true; 00145 } 00146 }