1 30 31 package com.nightlabs.util; 32 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.io.OutputStream ; 36 37 49 public class Binary 50 { 51 52 public InputStream in = null; 53 public OutputStream out = null; 54 55 60 public Binary(InputStream in) 61 { 62 this.in = in; 63 } 64 65 70 public Binary(OutputStream out) 71 { 72 this.out = out; 73 } 74 75 81 public int read() 82 throws IOException 83 { 84 if (in == null) 85 throw new IllegalStateException ("This binary is in write mode! Can't execute read()!"); 86 87 return in.read(); 88 } 89 90 96 public void close() 97 throws IOException 98 { 99 if (in != null) 100 in.close(); 101 102 if (out != null) 103 out.close(); 104 } 105 106 114 private long readReversedBytes( int byteCount ) 115 throws IOException 116 { 117 if(byteCount > 8) 118 throw new IllegalArgumentException ("nByteCount > 8!!!"); 119 120 if(byteCount < 1) 121 throw new IllegalArgumentException ("nByteCount < 1!!!"); 122 123 long bBytes[] = new long[byteCount]; 126 for( int i=0; i<byteCount; i++ ) 127 { 128 bBytes[i] = read(); 129 } 130 long nResult = 0; 131 for( int i=byteCount-1; i>0; i-- ) 132 { 133 nResult = nResult | (bBytes[i] << (8*i)); 134 } 135 nResult = nResult | bBytes[0]; 136 return( nResult ); 137 } 138 139 public byte readByte() 140 throws IOException 141 { 142 return( (byte) read() ); 143 } 144 145 151 public short readReversedShort() 152 throws IOException 153 { 154 return( (short) readReversedBytes( 2 ) ); 155 } 156 157 164 public int readReversedUnsignedShort() 165 throws IOException 166 { 167 return( (int) readReversedBytes( 2 ) ); 168 } 169 170 176 public int readReversedInt() 177 throws IOException 178 { 179 return( (int) readReversedBytes( 4 ) ); 180 } 181 182 189 public long readReversedUnsignedInt() 190 throws IOException 191 { 192 long nTest = readReversedBytes( 4 ); 193 return( nTest ); 194 } 195 196 201 public String readString() 202 throws IOException 203 { 204 int c; 205 StringBuffer sRead = new StringBuffer (); 206 c = read(); 207 while( c != 0 ) { 208 sRead.append( (char) c ); 209 c = read(); 210 } 211 return( sRead.toString() ); 212 } 213 214 219 public String readString( int _length ) 220 throws IOException 221 { 222 int c; 223 StringBuffer sRead = new StringBuffer (); 224 for( int i=0; i<_length; i++ ) { 225 c = read(); 226 sRead.append( (char) c ); 227 } 228 return( sRead.toString() ); 229 } 230 231 238 public int readNumber() 239 throws NumberFormatException , IOException 240 { 241 StringBuffer sRead = new StringBuffer (); 242 if (in.markSupported()) 243 in.mark(256); 244 245 boolean doReset = true; 246 try { 247 int c = read(); 248 while( c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || 249 c == '5' || c == '6' || c == '7' || c == '8' || c == '9' ) { 250 sRead.append( (char) c ); 251 c = read(); 252 } 253 254 doReset = false; 255 } finally { 256 if (doReset && in.markSupported()) 257 in.reset(); 258 } 259 return( Integer.parseInt( sRead.toString() ) ); 260 } 261 262 268 public long readReversedLong() 269 throws IOException 270 { 271 long nTest = readReversedBytes( 8 ); 272 return( nTest ); 273 } 274 275 private void writeReversedBytes(byte[] bytes) 276 throws IOException 277 { 278 byte[] rBytes = new byte[bytes.length]; 279 int m = 0; 280 for (int i = bytes.length - 1; i >= 0; --i) 281 rBytes[m++] = bytes[i]; 282 283 out.write(rBytes); 284 } 285 286 public void write(byte[] bytes) 287 throws IOException 288 { 289 out.write(bytes); 290 } 291 292 public void write(byte b) 293 throws IOException 294 { 295 out.write(b); 296 } 297 298 public byte[] getByteArray(long v, int length) 299 { 300 byte b[] = new byte[length]; 301 int i, shift; 302 for(i = 0, shift = (length - 1) * 8; i < length; i++, shift -= 8) 303 b[i] = (byte)(0xFF & (v >> shift)); 304 305 return b; 306 } 307 308 public void writeReversedUnsignedByte(short val) 309 throws IOException 310 { 311 writeReversedBytes(getByteArray(val, 1)); 312 } 313 314 public void writeReversedUnsignedShort(int val) 315 throws IOException 316 { 317 writeReversedBytes(getByteArray(val, 2)); 318 } 319 320 public void writeReversedShort(short val) 321 throws IOException 322 { 323 writeReversedBytes(getByteArray(val, 2)); 324 } 325 326 public void writeReversedUnsignedInt(long val) 327 throws IOException 328 { 329 writeReversedBytes(getByteArray(val, 4)); 330 } 331 332 public void writeReversedInt(int val) 333 throws IOException 334 { 335 writeReversedBytes(getByteArray(val, 4)); 336 } 337 338 343 public void writeString(String s) 344 throws IOException 345 { 346 write(s.getBytes(CHARSET)); 347 write((byte)0); } 349 350 public static String CHARSET = "ISO-8859-1"; 351 352 359 public void writeString(String s, int length) 360 throws IOException 361 { 362 if (s.length() != length) 363 throw new IllegalArgumentException ("Param length does not match String.length()! s=\""+s+"\" length=\""+length+"\""); 364 365 write(s.getBytes(CHARSET)); 366 } 367 } 368 | Popular Tags |