1 19 20 package com.maverick.util; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.EOFException ; 24 import java.io.IOException ; 25 import java.io.UnsupportedEncodingException ; 26 27 import java.math.BigInteger ; 28 29 35 public class ByteArrayReader 36 extends ByteArrayInputStream { 37 38 private static String CHARSET_ENCODING = "UTF8"; 39 40 public static boolean encode; 41 42 static { 43 setCharsetEncoding(CHARSET_ENCODING); 44 } 45 51 public ByteArrayReader(byte[] buffer, int start, int len) { 52 super(buffer, start, len); 53 } 54 55 public ByteArrayReader(byte[] buffer) { 56 super(buffer, 0, buffer.length); 57 } 58 59 63 public byte[] array() { 64 return this.buf; 65 } 66 67 74 public static void setCharsetEncoding(String charset) { 75 try { 76 77 String test = "123456890"; 78 test.getBytes(charset); 79 CHARSET_ENCODING = charset; 80 encode = true; 81 } 82 catch (UnsupportedEncodingException ex) { 83 CHARSET_ENCODING = ""; 85 encode = false; 86 } 87 } 88 89 93 public static String getCharsetEncoding() { 94 return CHARSET_ENCODING; 95 } 96 97 104 public void readFully(byte b[], int off, int len) throws IOException { 105 if (len < 0) 106 throw new IndexOutOfBoundsException (); 107 int n = 0; 108 while (n < len) { 109 int count = read(b, off + n, len - n); 110 if (count < 0) 111 throw new EOFException (); 112 n += count; 113 } 114 } 115 116 121 public boolean readBoolean() throws IOException { 122 return read()==1; 123 } 124 125 public void readFully(byte[] b) throws IOException { 126 readFully(b,0,b.length); 127 } 128 129 134 public BigInteger readBigInteger() throws IOException { 135 int len = (int) readInt(); 136 byte[] raw = new byte[len]; 137 readFully(raw); 138 return new BigInteger (raw); 139 } 140 141 public UnsignedInteger64 readUINT64() throws IOException { 142 byte[] raw = new byte[8]; 143 readFully(raw); 144 return new UnsignedInteger64(raw); 145 } 146 147 public UnsignedInteger32 readUINT32() throws IOException { 148 return new UnsignedInteger32(readInt()); 149 } 150 151 159 public static long readInt(byte[] data, int start) { 160 long ret = ( ( (long) (data[start] & 0xFF) << 24) & 0xFFFFFFFFL) 161 | ( (data[start + 1] & 0xFF) << 16) 162 | ( (data[start + 2] & 0xFF) << 8) | ( (data[start + 3] & 0xFF) << 0); 163 164 return ret; 165 } 166 167 168 public static short readShort(byte[] data, int start) { 169 short ret = (short)((data[start+1] & 0xFF) << 8 | (data[start] & 0xFF) << 0); 170 171 return ret; 172 } 173 182 183 195 196 201 public byte[] readBinaryString() throws IOException { 202 int len = (int) readInt(); 203 byte[] buf = new byte[len]; 204 readFully(buf); 205 return buf; 206 } 207 208 215 public long readInt() throws IOException { 216 int ch1 = read(); 217 int ch2 = read(); 218 int ch3 = read(); 219 int ch4 = read(); 220 if ((ch1 | ch2 | ch3 | ch4) < 0) 221 throw new EOFException (); 222 return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)) & 0xFFFFFFFFL; 223 224 } 225 226 231 public String readString() throws IOException { 232 return readString(CHARSET_ENCODING); 233 } 234 235 241 public String readString(String charset) throws IOException { 242 long len = readInt(); 243 244 if(len > available()) 245 throw new IOException ("Cannot read string of length " + len 246 + " bytes when only " + available() 247 + " bytes are available"); 248 249 byte[] raw = new byte[ (int) len]; 250 readFully(raw); 251 if (encode) { 252 return new String (raw, charset); 253 } 254 else { 255 return new String (raw); 256 } 257 258 } 259 260 261 262 263 public short readShort() throws IOException { 264 int ch1 = read(); 265 int ch2 = read(); 266 267 if ( (ch1 | ch2) < 0) { 268 throw new EOFException (); 269 } 270 271 return (short) ( (ch1 << 8) + (ch2 << 0)); 272 } 273 274 279 public BigInteger readMPINT32() throws IOException { 280 int bits = (int)readInt(); 281 282 byte[] raw = new byte[ (bits + 7) / 8 + 1]; 283 284 raw[0] = 0; 285 readFully(raw, 1, raw.length - 1); 286 287 return new BigInteger (raw); 288 289 } 290 291 296 public BigInteger readMPINT() throws IOException { 297 short bits = readShort(); 298 299 byte[] raw = new byte[ (bits + 7) / 8 + 1]; 300 301 raw[0] = 0; 302 readFully(raw, 1, raw.length - 1); 303 304 return new BigInteger (raw); 305 } 306 307 311 public int getPosition() { 312 return this.pos; 313 } 314 } 315 | Popular Tags |