001: /*
002: * $Id: BufferedDataInputStream.java,v 1.1 2005/04/07 00:27:53 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.io;
042:
043: import java.io.BufferedInputStream;
044: import java.io.DataInputStream;
045: import java.io.FilterInputStream;
046: import java.io.IOException;
047:
048: /**
049: * Utility that wraps a {@link AxionInputStream}in a {@link DataInputStream}and buffers
050: * input through a {@link BufferedInputStream}.
051: *
052: * @version $Revision: 1.1 $ $Date: 2005/04/07 00:27:53 $
053: * @author Ahimanikya Satapathy
054: */
055: public class BufferedDataInputStream extends DataInputStream {
056:
057: /** Buffer input. This improves performance significantly. */
058: private static class Buffer extends BufferedInputStream {
059: public Buffer(PositionCache in, int bufferSize)
060: throws IOException {
061: super (in, bufferSize);
062: }
063:
064: public long getPos() throws IOException { // adjust for buffer
065: return ((PositionCache) in).getPos()
066: - (this .count - this .pos);
067: }
068:
069: // optimized version of read()
070: public int read() throws IOException {
071: if (pos >= count)
072: return super .read();
073: return buf[pos++] & 0xff;
074: }
075:
076: public synchronized void reset() throws IOException {
077: // invalidate buffer
078: this .count = 0;
079: this .pos = 0;
080: }
081:
082: public void seek(long desired) throws IOException {
083: long current = getPos();
084: long start = (current - this .pos);
085: if (desired >= start && desired < start + this .count) {
086: this .pos += (desired - current); // can position within buffer
087: } else {
088: this .count = 0; // invalidate buffer
089: this .pos = 0;
090: ((PositionCache) in).seek(desired); // seek underlying stream
091: }
092: }
093: }
094:
095: /** Cache the file position. This improves performance significantly. */
096: private static class PositionCache extends FilterInputStream {
097: long position;
098:
099: public PositionCache(AxionInputStream in) throws IOException {
100: super (in);
101: this .position = in.getPos();
102: }
103:
104: public long getPos() throws IOException {
105: return position; // return cached position
106: }
107:
108: // This is the only read() method called by BufferedInputStream, so we trap
109: // calls to it in order to cache the position.
110: public int read(byte b[], int off, int len) throws IOException {
111: int result = in.read(b, off, len);
112: position += result;
113: return result;
114: }
115:
116: public void seek(long desired) throws IOException {
117: ((AxionInputStream) in).seek(desired); // seek underlying stream
118: position = desired; // update position
119: }
120: }
121:
122: public BufferedDataInputStream(AxionInputStream in)
123: throws IOException {
124: this (in, 4096);
125: }
126:
127: public BufferedDataInputStream(AxionInputStream in, int bufferSize)
128: throws IOException {
129: super (new Buffer(new PositionCache(in), bufferSize));
130: }
131:
132: public long getPos() throws IOException {
133: return ((Buffer) in).getPos();
134: }
135:
136: public void seek(long desired) throws IOException {
137: ((Buffer) in).seek(desired);
138: }
139: }
|