1 8 9 package com.sleepycat.util; 10 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 14 26 public class FastInputStream extends InputStream { 27 28 protected int len; 29 protected int off; 30 protected int mark; 31 protected byte[] buf; 32 33 38 public FastInputStream(byte[] buffer) { 39 40 buf = buffer; 41 len = buffer.length; 42 } 43 44 53 public FastInputStream(byte[] buffer, int offset, int length) { 54 55 buf = buffer; 56 off = offset; 57 len = offset + length; 58 } 59 60 62 public int available() { 63 64 return len - off; 65 } 66 67 public boolean markSupported() { 68 69 return true; 70 } 71 72 public void mark(int readLimit) { 73 74 mark = off; 75 } 76 77 public void reset() { 78 79 off = mark; 80 } 81 82 public long skip(long count) { 83 84 int myCount = (int) count; 85 if (myCount + off > len) { 86 myCount = len - off; 87 } 88 skipFast(myCount); 89 return myCount; 90 } 91 92 public int read() throws IOException { 93 94 return readFast(); 95 } 96 97 public int read(byte[] toBuf) throws IOException { 98 99 return readFast(toBuf, 0, toBuf.length); 100 } 101 102 public int read(byte[] toBuf, int offset, int length) throws IOException { 103 104 return readFast(toBuf, offset, length); 105 } 106 107 109 115 public final void skipFast(int count) { 116 off += count; 117 } 118 119 124 public final int readFast() { 125 126 return (off < len) ? (buf[off++] & 0xff) : (-1); 127 } 128 129 134 public final int readFast(byte[] toBuf) { 135 136 return readFast(toBuf, 0, toBuf.length); 137 } 138 139 144 public final int readFast(byte[] toBuf, int offset, int length) { 145 146 int avail = len - off; 147 if (avail <= 0) { 148 return -1; 149 } 150 if (length > avail) { 151 length = avail; 152 } 153 System.arraycopy(buf, off, toBuf, offset, length); 154 off += length; 155 return length; 156 } 157 158 163 public final byte[] getBufferBytes() { 164 165 return buf; 166 } 167 168 173 public final int getBufferOffset() { 174 175 return off; 176 } 177 178 183 public final int getBufferLength() { 184 185 return len; 186 } 187 } 188 | Popular Tags |