001: package com.quadcap.io;
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.InputStream;
042: import java.io.IOException;
043: import java.io.RandomAccessFile;
044:
045: import com.quadcap.util.Debug;
046:
047: /**
048: * This class implements an input stream filter which keeps track of how
049: * many bytes have been read.
050: *
051: * @author Stan Bailes
052: */
053: public class RandomAccessFileInputStream extends InputStream {
054: long mark = -1;
055: RandomAccessFile ra;
056:
057: /**
058: * Construct a new RandomAccessFileInputStream rearang from the
059: * specified file.
060: *
061: * @param ra the file
062: */
063: public RandomAccessFileInputStream(RandomAccessFile ra) {
064: this .ra = ra;
065: }
066:
067: /**
068: * Read the next byte from this input stream.
069: *
070: * @exception IOException if an I/O error occurs.
071: */
072: public int read() throws IOException {
073: // Debug.println(0, "read[" + ra.getFilePointer() + "] (" + 1 + ")");
074: return ra.read();
075: }
076:
077: /**
078: * Read a block of bytes from this input stream.
079: *
080: * @exception IOException if an I/O error occcurs.
081: */
082: public int read(byte[] buf, int offset, int count)
083: throws IOException {
084: // Debug.println(0, "read[" + ra.getFilePointer() + "] (" + count + ")");
085: return ra.read(buf, offset, count);
086: }
087:
088: /**
089: * Read a block of bytes from this input stream.
090: *
091: * @exception IOException if an I/O error occcurs.
092: */
093: public int read(byte[] buf) throws IOException {
094: // Debug.println(0, "read[" + ra.getFilePointer() + "] (" +
095: // buf.length + ")");
096: return ra.read(buf);
097: }
098:
099: /**
100: * Return the number of bytes that can be read without blocking.
101: */
102: public int available() throws IOException {
103: return (int) (ra.length() - ra.getFilePointer());
104: }
105:
106: /**
107: * Close the stream.
108: */
109: public void close() throws IOException {
110: ra.close();
111: }
112:
113: /**
114: * Skip ahead in the stream.
115: */
116: public long skip(long n) throws IOException {
117: ra.seek(ra.getFilePointer() + n);
118: return n;
119: }
120:
121: /**
122: * Mark the current position in the input stream.
123: */
124: public void mark(int readLimit) {
125: try {
126: this .mark = ra.getFilePointer();
127: } catch (IOException e) {
128: Debug.print(e);
129: throw new RuntimeException(e.toString());
130: }
131: }
132:
133: /**
134: * Return to the previously marked position in the input stream.
135: */
136: public void reset() throws IOException {
137: if (mark < 0)
138: throw new IOException("no mark");
139: ra.seek(mark);
140: }
141:
142: /**
143: * Return true if mark/reset supported (they are.)
144: */
145: public boolean markSupported() {
146: return true;
147: }
148: }
|