/*- * See the file LICENSE for redistribution information. * * Copyright (c) 2000-2004 * Sleepycat Software. All rights reserved. * * $Id: FastInputStream.java,v 1.2 2004/06/04 18:24:50 mark Exp $ */ package com.sleepycat.util; import java.io.IOException; import java.io.InputStream; /** * A replacement for ByteArrayInputStream that does not synchronize every * byte read. * *
This class extends {@link InputStream} and its read()
* methods allow it to be used as a standard input stream. In addition, it
* provides readFast() methods that are not declared to throw
* IOException. IOException is never thrown by this
* class.
read() but does not throw
* IOException.
* @see #read()
*/
public final int readFast() {
return (off < len) ? (buf[off++] & 0xff) : (-1);
}
/**
* Equivalent to read(byte[]) but does not throw
* IOException.
* @see #read(byte[])
*/
public final int readFast(byte[] toBuf) {
return readFast(toBuf, 0, toBuf.length);
}
/**
* Equivalent to read(byte[],int,int) but does not throw
* IOException.
* @see #read(byte[],int,int)
*/
public final int readFast(byte[] toBuf, int offset, int length) {
int avail = len - off;
if (avail <= 0) {
return -1;
}
if (length > avail) {
length = avail;
}
for (int i = 0; i < length; i++) {
toBuf[offset++] = buf[off++];
}
return length;
}
/**
* Returns the underlying data being read.
*
* @return the underlying data.
*/
public final byte[] getBufferBytes() {
return buf;
}
/**
* Returns the offset at which data is being read from the buffer.
*
* @return the offset at which data is being read.
*/
public final int getBufferOffset() {
return off;
}
/**
* Returns the end of the buffer being read.
*
* @return the end of the buffer.
*/
public final int getBufferLength() {
return len;
}
}