the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 553 lines 18 kB view raw
1#include "stdafx.h" 2#include "BasicTypeContainers.h" 3 4#include "DataInputStream.h" 5 6//Creates a DataInputStream that uses the specified underlying InputStream. 7//Parameters: 8//in - the specified input stream 9DataInputStream::DataInputStream(InputStream *in) : stream( in ) 10{ 11} 12 13//Reads the next byte of data from this input stream. The value byte is returned as an int in the range 0 to 255. 14//If no byte is available because the end of the stream has been reached, the value -1 is returned. 15//This method blocks until input data is available, the end of the stream is detected, or an exception is thrown. 16//This method simply performs in.read() and returns the result. 17int DataInputStream::read() 18{ 19 return stream->read(); 20} 21 22//Reads some number of bytes from the contained input stream and stores them into the buffer array b. 23//The number of bytes actually read is returned as an integer. This method blocks until input data is available, 24//end of file is detected, or an exception is thrown. 25//If b is null, a NullPointerException is thrown. If the length of b is zero, then no bytes are read and 0 is returned; 26//otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, 27//the value -1 is returned; otherwise, at least one byte is read and stored into b. 28// 29//The first byte read is stored into element b[0], the next one into b[1], and so on. The number of bytes read is, at most, 30//equal to the length of b. Let k be the number of bytes actually read; these bytes will be stored in elements b[0] through b[k-1], 31//leaving elements b[k] through b[b.length-1] unaffected. 32// 33//The read(b) method has the same effect as: 34// 35// read(b, 0, b.length) 36// 37//Overrides: 38//read in class FilterInputStream 39//Parameters: 40//b - the buffer into which the data is read. 41//Returns: 42//the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached. 43int DataInputStream::read(byteArray b) 44{ 45 return read( b, 0, b.length ); 46} 47 48//Reads up to len bytes of data from the contained input stream into an array of bytes. An attempt is made to read as many as len bytes, 49//but a smaller number may be read, possibly zero. The number of bytes actually read is returned as an integer. 50//This method blocks until input data is available, end of file is detected, or an exception is thrown. 51// 52//If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. 53//If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b. 54// 55//The first byte read is stored into element b[off], the next one into b[off+1], and so on. The number of bytes read is, 56//at most, equal to len. Let k be the number of bytes actually read; these bytes will be stored in elements b[off] through b[off+k-1], 57//leaving elements b[off+k] through b[off+len-1] unaffected. 58// 59//In every case, elements b[0] through b[off] and elements b[off+len] through b[b.length-1] are unaffected. 60// 61//Overrides: 62//read in class FilterInputStream 63//Parameters: 64//b - the buffer into which the data is read. 65//off - the start offset in the destination array b 66//len - the maximum number of bytes read. 67//Returns: 68//the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached. 69int DataInputStream::read(byteArray b, unsigned int offset, unsigned int length) 70{ 71 return stream->read( b, offset, length ); 72} 73 74//Closes this input stream and releases any system resources associated with the stream. This method simply performs in.close() 75void DataInputStream::close() 76{ 77 stream->close(); 78} 79 80//Reads one input byte and returns true if that byte is nonzero, false if that byte is zero. This method is suitable for reading 81//the byte written by the writeBoolean method of interface DataOutput. 82//Returns: 83//the boolean value read. 84bool DataInputStream::readBoolean() 85{ 86 return stream->read() != 0; 87} 88 89//Reads and returns one input byte. The byte is treated as a signed value in the range -128 through 127, inclusive. 90//This method is suitable for reading the byte written by the writeByte method of interface DataOutput. 91//Returns: 92//the 8-bit value read. 93byte DataInputStream::readByte() 94{ 95 return (byte) stream->read(); 96} 97 98unsigned char DataInputStream::readUnsignedByte() 99{ 100 return (unsigned char) stream->read(); 101} 102 103//Reads two input bytes and returns a char value. Let a be the first byte read and b be the second byte. The value returned is: 104//(char)((a << 8) | (b & 0xff)) 105// 106//This method is suitable for reading bytes written by the writeChar method of interface DataOutput. 107//Returns: 108//the char value read. 109wchar_t DataInputStream::readChar() 110{ 111 int a = stream->read(); 112 int b = stream->read(); 113 return (wchar_t)((a << 8) | (b & 0xff)); 114} 115 116//Reads some bytes from an input stream and stores them into the buffer array b. The number of bytes read is equal to the length of b. 117//This method blocks until one of the following conditions occurs: 118// 119//b.length bytes of input data are available, in which case a normal return is made. 120//End of file is detected, in which case an EOFException is thrown. 121//An I/O error occurs, in which case an IOException other than EOFException is thrown. 122//If b is null, a NullPointerException is thrown. If b.length is zero, then no bytes are read. Otherwise, the first byte read is 123//stored into element b[0], the next one into b[1], and so on. If an exception is thrown from this method, then it may be that some but 124//not all bytes of b have been updated with data from the input stream. 125// 126//Parameters: 127//b - the buffer into which the data is read. 128bool DataInputStream::readFully(byteArray b) 129{ 130 // TODO 4J Stu - I am not entirely sure if this matches the implementation of the Java library 131 // TODO 4J Stu - Need to handle exceptions here is we throw them in other InputStreams 132 for(unsigned int i = 0; i < b.length ;i++) 133 { 134 int byteRead = stream->read(); 135 if( byteRead == -1 ) 136 { 137 return false; 138 } 139 else 140 { 141 b[i] = byteRead; 142 } 143 } 144 return true; 145} 146 147bool DataInputStream::readFully(charArray b) 148{ 149 // TODO 4J Stu - I am not entirely sure if this matches the implementation of the Java library 150 // TODO 4J Stu - Need to handle exceptions here is we throw them in other InputStreams 151 for(unsigned int i = 0; i < b.length ;i++) 152 { 153 int byteRead = stream->read(); 154 if( byteRead == -1 ) 155 { 156 return false; 157 } 158 else 159 { 160 b[i] = byteRead; 161 } 162 } 163 return true; 164} 165 166//Reads eight input bytes and returns a double value. It does this by first constructing a long value in exactly the manner 167//of the readlong method, then converting this long value to a double in exactly the manner of the method Double.longBitsToDouble. 168//This method is suitable for reading bytes written by the writeDouble method of interface DataOutput. 169//Returns: 170//the double value read. 171double DataInputStream::readDouble() 172{ 173 __int64 bits = readLong(); 174 175 return Double::longBitsToDouble( bits ); 176} 177 178//Reads four input bytes and returns a float value. It does this by first constructing an int value in exactly the manner 179//of the readInt method, then converting this int value to a float in exactly the manner of the method Float.intBitsToFloat. 180//This method is suitable for reading bytes written by the writeFloat method of interface DataOutput. 181//Returns: 182//the float value read. 183float DataInputStream::readFloat() 184{ 185 int bits = readInt(); 186 187 return Float::intBitsToFloat( bits ); 188} 189 190//Reads four input bytes and returns an int value. Let a-d be the first through fourth bytes read. The value returned is: 191// 192// (((a & 0xff) << 24) | ((b & 0xff) << 16) | 193// ((c & 0xff) << 8) | (d & 0xff)) 194// 195//This method is suitable for reading bytes written by the writeInt method of interface DataOutput. 196//Returns: 197//the int value read. 198int DataInputStream::readInt() 199{ 200 int a = stream->read(); 201 int b = stream->read(); 202 int c = stream->read(); 203 int d = stream->read(); 204 int bits = (((a & 0xff) << 24) | ((b & 0xff) << 16) | 205 ((c & 0xff) << 8) | (d & 0xff)); 206 return bits; 207} 208 209//Reads eight input bytes and returns a long value. Let a-h be the first through eighth bytes read. The value returned is: 210// 211// (((long)(a & 0xff) << 56) | 212// ((long)(b & 0xff) << 48) | 213// ((long)(c & 0xff) << 40) | 214// ((long)(d & 0xff) << 32) | 215// ((long)(e & 0xff) << 24) | 216// ((long)(f & 0xff) << 16) | 217// ((long)(g & 0xff) << 8) | 218// ((long)(h & 0xff))) 219// 220//This method is suitable for reading bytes written by the writeLong method of interface DataOutput. 221// 222//Returns: 223//the long value read. 224__int64 DataInputStream::readLong() 225{ 226 __int64 a = stream->read(); 227 __int64 b = stream->read(); 228 __int64 c = stream->read(); 229 __int64 d = stream->read(); 230 __int64 e = stream->read(); 231 __int64 f = stream->read(); 232 __int64 g = stream->read(); 233 __int64 h = stream->read(); 234 235 __int64 bits = (((a & 0xff) << 56) | 236 ((b & 0xff) << 48) | 237 ((c & 0xff) << 40) | 238 ((d & 0xff) << 32) | 239 ((e & 0xff) << 24) | 240 ((f & 0xff) << 16) | 241 ((g & 0xff) << 8) | 242 ((h & 0xff))); 243 244 return bits; 245} 246 247//Reads two input bytes and returns a short value. Let a be the first byte read and b be the second byte. The value returned is: 248//(short)((a << 8) | (b & 0xff)) 249// 250//This method is suitable for reading the bytes written by the writeShort method of interface DataOutput. 251//Returns: 252//the 16-bit value read. 253short DataInputStream::readShort() 254{ 255 int a = stream->read(); 256 int b = stream->read(); 257 return (short)((a << 8) | (b & 0xff)); 258} 259 260unsigned short DataInputStream::readUnsignedShort() 261{ 262 int a = stream->read(); 263 int b = stream->read(); 264 return (unsigned short)((a << 8) | (b & 0xff)); 265} 266 267//Reads in a string that has been encoded using a modified UTF-8 format. The general contract of readUTF is that it reads a representation 268//of a Unicode character string encoded in modified UTF-8 format; this string of characters is then returned as a String. 269//First, two bytes are read and used to construct an unsigned 16-bit integer in exactly the manner of the readUnsignedShort method . 270//This integer value is called the UTF length and specifies the number of additional bytes to be read. These bytes are then converted 271//to characters by considering them in groups. The length of each group is computed from the value of the first byte of the group. 272//The byte following a group, if any, is the first byte of the next group. 273// 274//If the first byte of a group matches the bit pattern 0xxxxxxx (where x means "may be 0 or 1"), then the group consists of just that byte. 275//The byte is zero-extended to form a character. 276// 277//If the first byte of a group matches the bit pattern 110xxxxx, then the group consists of that byte a and a second byte b. 278//If there is no byte b (because byte a was the last of the bytes to be read), or if byte b does not match the bit pattern 10xxxxxx, 279//then a UTFDataFormatException is thrown. Otherwise, the group is converted to the character: 280// 281//(char)(((a& 0x1F) << 6) | (b & 0x3F)) 282// 283//If the first byte of a group matches the bit pattern 1110xxxx, then the group consists of that byte a and two more bytes b and c. 284//If there is no byte c (because byte a was one of the last two of the bytes to be read), or either byte b or byte c does not match the bit 285//pattern 10xxxxxx, then a UTFDataFormatException is thrown. Otherwise, the group is converted to the character: 286// 287// (char)(((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F)) 288// 289//If the first byte of a group matches the pattern 1111xxxx or the pattern 10xxxxxx, then a UTFDataFormatException is thrown. 290//If end of file is encountered at any time during this entire process, then an EOFException is thrown. 291// 292//After every group has been converted to a character by this process, the characters are gathered, in the same order in which their 293//corresponding groups were read from the input stream, to form a String, which is returned. 294// 295//The writeUTF method of interface DataOutput may be used to write data that is suitable for reading by this method. 296// 297//Returns: 298//a Unicode string. 299wstring DataInputStream::readUTF() 300{ 301 wstring outputString; 302 int a = stream->read(); 303 int b = stream->read(); 304 unsigned short UTFLength = (unsigned short) (((a & 0xff) << 8) | (b & 0xff)); 305 306 //// 4J Stu - I decided while writing DataOutputStream that we didn't need to bother using the UTF8 format 307 //// used in the java libs, and just write in/out as wchar_t all the time 308 309 /*for( unsigned short i = 0; i < UTFLength; i++) 310 { 311 wchar_t theChar = readChar(); 312 outputString.push_back(theChar); 313 }*/ 314 315 316 unsigned short currentByteIndex = 0; 317 while( currentByteIndex < UTFLength ) 318 { 319 int firstByte = stream->read(); 320 currentByteIndex++; 321 322 if( firstByte == -1 ) 323 // TODO 4J Stu - EOFException 324 break; 325 326 // Masking patterns: 327 // 10000000 = 0x80 // Match only highest bit 328 // 11000000 = 0xC0 // Match only highest two bits 329 // 11100000 = 0xE0 // Match only highest three bits 330 // 11110000 = 0xF0 // Match only highest four bits 331 332 // Matching patterns: 333 // 10xxxxxx = 0x80 // ERROR, or second/third byte 334 // 1111xxxx = 0xF0 //ERROR 335 // 0xxxxxxx = 0x00 // One byte UTF 336 // 110xxxxx = 0xC0 // Two byte UTF 337 // 1110xxxx = 0xE0 // Three byte UTF 338 if( ( (firstByte & 0xC0 ) == 0x80 ) || ( (firstByte & 0xF0) == 0xF0) ) 339 { 340 // TODO 4J Stu - UTFDataFormatException 341 break; 342 } 343 else if( (firstByte & 0x80) == 0x00 ) 344 { 345 // One byte UTF 346 wchar_t readChar = (wchar_t)firstByte; 347 outputString.push_back( readChar ); 348 continue; 349 } 350 else if( (firstByte & 0xE0) == 0xC0 ) 351 { 352 // Two byte UTF 353 354 // No more bytes to read 355 if( !(currentByteIndex < UTFLength) ) 356 { 357 // TODO 4J Stu - UTFDataFormatException 358 break; 359 } 360 361 int secondByte = stream->read(); 362 currentByteIndex++; 363 364 // No second byte 365 if( secondByte == -1 ) 366 { 367 // TODO 4J Stu - EOFException 368 break; 369 } 370 // Incorrect second byte pattern 371 else if( (secondByte & 0xC0 ) != 0x80 ) 372 { 373 // TODO 4J Stu - UTFDataFormatException 374 break; 375 } 376 377 wchar_t readChar = (wchar_t)( ((firstByte& 0x1F) << 6) | (secondByte & 0x3F) ); 378 outputString.push_back( readChar ); 379 continue; 380 } 381 else if( (firstByte & 0xF0) == 0xE0 ) 382 { 383 // Three byte UTF 384 385 // No more bytes to read 386 if( !(currentByteIndex < UTFLength) ) 387 { 388 // TODO 4J Stu - UTFDataFormatException 389 break; 390 } 391 392 int secondByte = stream->read(); 393 currentByteIndex++; 394 395 // No second byte 396 if( secondByte == -1 ) 397 { 398 // TODO 4J Stu - EOFException 399 break; 400 } 401 402 // No more bytes to read 403 if( !(currentByteIndex < UTFLength) ) 404 { 405 // TODO 4J Stu - UTFDataFormatException 406 break; 407 } 408 409 int thirdByte = stream->read(); 410 currentByteIndex++; 411 412 // No third byte 413 if( thirdByte == -1 ) 414 { 415 // TODO 4J Stu - EOFException 416 break; 417 } 418 // Incorrect second or third byte pattern 419 else if( ( (secondByte & 0xC0 ) != 0x80 ) || ( (thirdByte & 0xC0 ) != 0x80 ) ) 420 { 421 // TODO 4J Stu - UTFDataFormatException 422 break; 423 } 424 425 wchar_t readChar = (wchar_t)(((firstByte & 0x0F) << 12) | ((secondByte & 0x3F) << 6) | (thirdByte & 0x3F)); 426 outputString.push_back( readChar ); 427 continue; 428 } 429 } 430 431 return outputString; 432} 433 434int DataInputStream::readUTFChar() 435{ 436 int returnValue = -1; 437 int firstByte = stream->read(); 438 439 if( firstByte == -1 ) 440 // TODO 4J Stu - EOFException 441 return returnValue; 442 443 // Masking patterns: 444 // 10000000 = 0x80 // Match only highest bit 445 // 11000000 = 0xC0 // Match only highest two bits 446 // 11100000 = 0xE0 // Match only highest three bits 447 // 11110000 = 0xF0 // Match only highest four bits 448 449 // Matching patterns: 450 // 10xxxxxx = 0x80 // ERROR, or second/third byte 451 // 1111xxxx = 0xF0 //ERROR 452 // 0xxxxxxx = 0x00 // One byte UTF 453 // 110xxxxx = 0xC0 // Two byte UTF 454 // 1110xxxx = 0xE0 // Three byte UTF 455 if( ( (firstByte & 0xC0 ) == 0x80 ) || ( (firstByte & 0xF0) == 0xF0) ) 456 { 457 // TODO 4J Stu - UTFDataFormatException 458 return returnValue; 459 } 460 else if( (firstByte & 0x80) == 0x00 ) 461 { 462 // One byte UTF 463 returnValue = firstByte; 464 } 465 else if( (firstByte & 0xE0) == 0xC0 ) 466 { 467 // Two byte UTF 468 int secondByte = stream->read(); 469 470 // No second byte 471 if( secondByte == -1 ) 472 { 473 // TODO 4J Stu - EOFException 474 return returnValue; 475 } 476 // Incorrect second byte pattern 477 else if( (secondByte & 0xC0 ) != 0x80 ) 478 { 479 // TODO 4J Stu - UTFDataFormatException 480 return returnValue; 481 } 482 483 returnValue = ((firstByte& 0x1F) << 6) | (secondByte & 0x3F); 484 } 485 else if( (firstByte & 0xF0) == 0xE0 ) 486 { 487 // Three byte UTF 488 489 int secondByte = stream->read(); 490 491 // No second byte 492 if( secondByte == -1 ) 493 { 494 // TODO 4J Stu - EOFException 495 return returnValue; 496 } 497 498 int thirdByte = stream->read(); 499 500 // No third byte 501 if( thirdByte == -1 ) 502 { 503 // TODO 4J Stu - EOFException 504 return returnValue; 505 } 506 // Incorrect second or third byte pattern 507 else if( ( (secondByte & 0xC0 ) != 0x80 ) || ( (thirdByte & 0xC0 ) != 0x80 ) ) 508 { 509 // TODO 4J Stu - UTFDataFormatException 510 return returnValue; 511 } 512 513 returnValue = (((firstByte & 0x0F) << 12) | ((secondByte & 0x3F) << 6) | (thirdByte & 0x3F)); 514 } 515 return returnValue; 516} 517 518// 4J Added 519PlayerUID DataInputStream::readPlayerUID() 520{ 521 PlayerUID returnValue; 522#if defined(__PS3__) || defined(__ORBIS__) || defined(__PSVITA__) 523 for(int idPos=0;idPos<sizeof(PlayerUID); idPos++) 524 ((char*)&returnValue)[idPos] = readByte(); 525#elif defined(_DURANGO) 526 returnValue = readUTF(); 527#else 528 returnValue = readLong(); 529#endif // PS3 530 return returnValue; 531} 532 533void DataInputStream::deleteChildStream() 534{ 535 delete stream; 536} 537 538//Skips n bytes of input from this input stream. Fewer bytes might be skipped if the end of the input stream is reached. The actual number k of bytes to be skipped is equal to the smaller of n and count-pos. The value k is added into pos and k is returned. 539//Overrides: 540//skip in class InputStream 541//Parameters: 542//n - the number of bytes to be skipped. 543//Returns: 544//the actual number of bytes skipped. 545__int64 DataInputStream::skip(__int64 n) 546{ 547 return stream->skip(n); 548} 549 550int DataInputStream::skipBytes(int n) 551{ 552 return skip(n); 553}