Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

FastInputStream.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2000-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: FastInputStream.java,v 12.2 2005/08/01 20:25:22 mark Exp $
00008  */
00009 
00010 package com.sleepycat.util;
00011 
00012 import java.io.IOException;
00013 import java.io.InputStream;
00014 
00027 public class FastInputStream extends InputStream {
00028 
00029     protected int len;
00030     protected int off;
00031     protected int mark;
00032     protected byte[] buf;
00033 
00039     public FastInputStream(byte[] buffer) {
00040 
00041         buf = buffer;
00042         len = buffer.length;
00043     }
00044 
00054     public FastInputStream(byte[] buffer, int offset, int length) {
00055 
00056         buf = buffer;
00057         off = offset;
00058         len = length;
00059     }
00060 
00061     // --- begin ByteArrayInputStream compatible methods ---
00062 
00063     public int available() {
00064 
00065         return len - off;
00066     }
00067 
00068     public boolean markSupported() {
00069 
00070         return true;
00071     }
00072 
00073     public void mark(int pos) {
00074 
00075         mark = pos;
00076     }
00077 
00078     public void reset() {
00079 
00080         off = mark;
00081     }
00082 
00083     public long skip(long count) {
00084 
00085         int myCount = (int) count;
00086         if (myCount + off > len) {
00087             myCount = len - off;
00088         }
00089         off += myCount;
00090         return myCount;
00091     }
00092 
00093     public int read() throws IOException {
00094 
00095         return readFast();
00096     }
00097 
00098     public int read(byte[] toBuf) throws IOException {
00099 
00100         return readFast(toBuf, 0, toBuf.length);
00101     }
00102 
00103     public int read(byte[] toBuf, int offset, int length) throws IOException {
00104 
00105         return readFast(toBuf, offset, length);
00106     }
00107 
00108     // --- end ByteArrayInputStream compatible methods ---
00109 
00115     public final int readFast() {
00116 
00117         return (off < len) ? (buf[off++] & 0xff) : (-1);
00118     }
00119 
00125     public final int readFast(byte[] toBuf) {
00126 
00127         return readFast(toBuf, 0, toBuf.length);
00128     }
00129 
00135     public final int readFast(byte[] toBuf, int offset, int length) {
00136 
00137         int avail = len - off;
00138         if (avail <= 0) {
00139             return -1;
00140         }
00141         if (length > avail) {
00142             length = avail;
00143         }
00144         System.arraycopy(buf, off, toBuf, offset, length);
00145         off += length;
00146         return length;
00147     }
00148 
00154     public final byte[] getBufferBytes() {
00155 
00156         return buf;
00157     }
00158 
00164     public final int getBufferOffset() {
00165 
00166         return off;
00167     }
00168 
00174     public final int getBufferLength() {
00175 
00176         return len;
00177     }
00178 }

Generated on Sun Dec 25 12:14:33 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2