1 24 25 package com.mckoi.database; 26 27 import java.io.DataInputStream ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.DataInput ; 32 33 43 44 final class CellBufferInputStream extends InputStream implements CellInput { 45 46 51 private byte buf[]; 52 private int pos; 53 private int mark = 0; 54 private int count; 55 56 59 CellBufferInputStream() { 60 this.buf = null; 61 this.pos = 0; 62 this.count = 0; 63 } 64 65 68 void setArray(byte[] new_buffer) { 69 buf = new_buffer; 70 pos = 0; 71 mark = 0; 72 count = new_buffer.length; 73 } 74 75 78 void setArray(byte[] new_buffer, int offset, int length) { 79 buf = new_buffer; 80 pos = offset; 81 mark = 0; 82 count = Math.min(new_buffer.length, length + offset); 83 } 84 85 88 public int read() { 89 return buf[pos++] & 0x0FF; 90 } 92 93 public int read(byte b[], int off, int len) { 94 if (pos >= count) { 101 return -1; 102 } 103 if (pos + len > count) { 104 len = count - pos; 105 } 106 if (len <= 0) { 107 return 0; 108 } 109 System.arraycopy(buf, pos, b, off, len); 110 pos += len; 111 return len; 112 } 113 114 public long skip(long n) { 115 if (pos + n > count) { 116 n = count - pos; 117 } 118 if (n < 0) { 119 return 0; 120 } 121 pos += n; 122 return n; 123 } 124 125 public int available() { 126 return count - pos; 127 } 128 129 public void mark(int readAheadLimit) { 130 mark = pos; 131 } 132 133 public void reset() { 134 pos = mark; 135 } 136 137 public void close() throws IOException { 138 } 139 140 141 143 public void readFully(byte[] b) throws IOException { 144 read(b, 0, b.length); 145 } 146 147 public void readFully(byte b[], int off, int len) throws IOException { 148 read(b, off, len); 149 } 150 151 public int skipBytes(int n) throws IOException { 152 return (int) skip(n); 153 } 154 155 public boolean readBoolean() throws IOException { 156 return (read() != 0); 157 } 158 159 public byte readByte() throws IOException { 160 return (byte) read(); 161 } 162 163 public int readUnsignedByte() throws IOException { 164 return read(); 165 } 166 167 public short readShort() throws IOException { 168 int ch1 = read(); 169 int ch2 = read(); 170 return (short)((ch1 << 8) + (ch2 << 0)); 171 } 172 173 public int readUnsignedShort() throws IOException { 174 int ch1 = read(); 175 int ch2 = read(); 176 return (ch1 << 8) + (ch2 << 0); 177 } 178 179 public char readChar() throws IOException { 180 int ch1 = read(); 181 int ch2 = read(); 182 return (char)((ch1 << 8) + (ch2 << 0)); 183 } 184 185 private char[] char_buffer = new char[8192]; 186 187 public String readChars(int length) throws IOException { 188 if (length <= char_buffer.length) { 189 for (int i = 0; i < length; ++i) { 190 char_buffer[i] = (char) (((buf[pos++] & 0x0FF) << 8) + 191 ((buf[pos++] & 0x0FF) << 0)); 192 } 193 return new String (char_buffer, 0, length); 194 } 195 else { 196 StringBuffer chrs = new StringBuffer (length); 197 for (int i = length; i > 0; --i) { 198 chrs.append((char) (((buf[pos++] & 0x0FF) << 8) + 199 ((buf[pos++] & 0x0FF) << 0))); 200 } 201 return new String (chrs); 202 } 203 } 204 205 public int readInt() throws IOException { 206 return ((buf[pos++] & 0x0FF) << 24) + 207 ((buf[pos++] & 0x0FF) << 16) + 208 ((buf[pos++] & 0x0FF) << 8) + 209 ((buf[pos++] & 0x0FF) << 0); 210 } 211 212 public long readLong() throws IOException { 213 return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL); 214 } 215 216 public float readFloat() throws IOException { 217 return Float.intBitsToFloat(readInt()); 218 } 219 220 public double readDouble() throws IOException { 221 return Double.longBitsToDouble(readLong()); 222 } 223 224 public String readLine() throws IOException { 225 throw new Error ("Not implemented."); 226 } 227 228 public String readUTF() throws IOException { 229 throw new Error ("Not implemented."); 230 } 231 232 233 234 235 247 } 248 | Popular Tags |