001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2000,2008 Oracle. All rights reserved.
005: *
006: * $Id: FastInputStream.java,v 1.18.2.2 2008/01/07 15:14:21 cwl Exp $
007: */
008:
009: package com.sleepycat.util;
010:
011: import java.io.IOException;
012: import java.io.InputStream;
013:
014: /**
015: * A replacement for ByteArrayInputStream that does not synchronize every
016: * byte read.
017: *
018: * <p>This class extends {@link InputStream} and its <code>read()</code>
019: * methods allow it to be used as a standard input stream. In addition, it
020: * provides <code>readFast()</code> methods that are not declared to throw
021: * <code>IOException</code>. <code>IOException</code> is never thrown by this
022: * class.</p>
023: *
024: * @author Mark Hayes
025: */
026: public class FastInputStream extends InputStream {
027:
028: protected int len;
029: protected int off;
030: protected int mark;
031: protected byte[] buf;
032:
033: /**
034: * Creates an input stream.
035: *
036: * @param buffer the data to read.
037: */
038: public FastInputStream(byte[] buffer) {
039:
040: buf = buffer;
041: len = buffer.length;
042: }
043:
044: /**
045: * Creates an input stream.
046: *
047: * @param buffer the data to read.
048: *
049: * @param offset the byte offset at which to begin reading.
050: *
051: * @param length the number of bytes to read.
052: */
053: public FastInputStream(byte[] buffer, int offset, int length) {
054:
055: buf = buffer;
056: off = offset;
057: len = offset + length;
058: }
059:
060: // --- begin ByteArrayInputStream compatible methods ---
061:
062: public int available() {
063:
064: return len - off;
065: }
066:
067: public boolean markSupported() {
068:
069: return true;
070: }
071:
072: public void mark(int readLimit) {
073:
074: mark = off;
075: }
076:
077: public void reset() {
078:
079: off = mark;
080: }
081:
082: public long skip(long count) {
083:
084: int myCount = (int) count;
085: if (myCount + off > len) {
086: myCount = len - off;
087: }
088: skipFast(myCount);
089: return myCount;
090: }
091:
092: public int read() throws IOException {
093:
094: return readFast();
095: }
096:
097: public int read(byte[] toBuf) throws IOException {
098:
099: return readFast(toBuf, 0, toBuf.length);
100: }
101:
102: public int read(byte[] toBuf, int offset, int length)
103: throws IOException {
104:
105: return readFast(toBuf, offset, length);
106: }
107:
108: // --- end ByteArrayInputStream compatible methods ---
109:
110: /**
111: * Equivalent to <code>skip()<code> but takes an int parameter instead of a
112: * long, and does not check whether the count given is larger than the
113: * number of remaining bytes.
114: * @see #skip(long)
115: */
116: public final void skipFast(int count) {
117: off += count;
118: }
119:
120: /**
121: * Equivalent to <code>read()<code> but does not throw
122: * <code>IOException</code>.
123: * @see #read()
124: */
125: public final int readFast() {
126:
127: return (off < len) ? (buf[off++] & 0xff) : (-1);
128: }
129:
130: /**
131: * Equivalent to <code>read(byte[])<code> but does not throw
132: * <code>IOException</code>.
133: * @see #read(byte[])
134: */
135: public final int readFast(byte[] toBuf) {
136:
137: return readFast(toBuf, 0, toBuf.length);
138: }
139:
140: /**
141: * Equivalent to <code>read(byte[],int,int)<code> but does not throw
142: * <code>IOException</code>.
143: * @see #read(byte[],int,int)
144: */
145: public final int readFast(byte[] toBuf, int offset, int length) {
146:
147: int avail = len - off;
148: if (avail <= 0) {
149: return -1;
150: }
151: if (length > avail) {
152: length = avail;
153: }
154: System.arraycopy(buf, off, toBuf, offset, length);
155: off += length;
156: return length;
157: }
158:
159: /**
160: * Returns the underlying data being read.
161: *
162: * @return the underlying data.
163: */
164: public final byte[] getBufferBytes() {
165:
166: return buf;
167: }
168:
169: /**
170: * Returns the offset at which data is being read from the buffer.
171: *
172: * @return the offset at which data is being read.
173: */
174: public final int getBufferOffset() {
175:
176: return off;
177: }
178:
179: /**
180: * Returns the end of the buffer being read.
181: *
182: * @return the end of the buffer.
183: */
184: public final int getBufferLength() {
185:
186: return len;
187: }
188: }
|