the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 118 lines 4.6 kB view raw
1#include "stdafx.h" 2 3#include "InputOutputStream.h" 4 5//Creates ByteArrayInputStream that uses buf as its buffer array. The initial value of pos is offset and 6//the initial value of count is the minimum of offset+length and buf.length. The buffer array is not copied. 7//The buffer's mark is set to the specified offset. 8//Parameters: 9//buf - the input buffer. 10//offset - the offset in the buffer of the first byte to read. 11//length - the maximum number of bytes to read from the buffer. 12ByteArrayInputStream::ByteArrayInputStream(byteArray buf, unsigned int offset, unsigned int length) 13 : pos( offset ), count( min( offset+length, buf.length ) ), mark( offset ) 14{ 15 this->buf = buf; 16} 17 18//Creates a ByteArrayInputStream so that it uses buf as its buffer array. The buffer array is not copied. 19//The initial value of pos is 0 and the initial value of count is the length of buf. 20//Parameters: 21//buf - the input buffer. 22ByteArrayInputStream::ByteArrayInputStream(byteArray buf) 23 : pos( 0 ), count( buf.length ), mark( 0 ) 24{ 25 this->buf = buf; 26} 27 28//Reads the next byte of data from this input stream. The value byte is returned as an int in the range 0 to 255. 29//If no byte is available because the end of the stream has been reached, the value -1 is returned. 30//This read method cannot block. 31//Returns: 32//the next byte of data, or -1 if the end of the stream has been reached. 33int ByteArrayInputStream::read() 34{ 35 if( pos >= count ) 36 return -1; 37 else 38 return buf[pos++]; 39} 40 41//Reads some number of bytes from the input stream and stores them into the buffer array b. 42//The number of bytes actually read is returned as an integer. This method blocks until input data is available, 43//end of file is detected, or an exception is thrown. 44//If the length of b is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. 45//If no byte is available because the stream is at the end of the file, the value -1 is returned; otherwise, 46//at least one byte is read and stored into b. 47// 48//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, 49//at most, 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], 50//leaving elements b[k] through b[b.length-1] unaffected. 51// 52//The read(b) method for class InputStream has the same effect as: 53// 54// read(b, 0, b.length) 55//Parameters: 56//b - the buffer into which the data is read. 57//Returns: 58//the total number of bytes read into the buffer, or -1 is there is no more data because the end of the stream has been reached. 59int ByteArrayInputStream::read(byteArray b) 60{ 61 return read( b, 0, b.length ); 62} 63 64//Reads up to len bytes of data into an array of bytes from this input stream. If pos equals count, 65//then -1 is returned to indicate end of file. Otherwise, the number k of bytes read is equal to the smaller of len and count-pos. 66//If k is positive, then bytes buf[pos] through buf[pos+k-1] are copied into b[off] through b[off+k-1] in the manner 67//performed by System.arraycopy. The value k is added into pos and k is returned. 68//This read method cannot block. 69//Parameters: 70//b - the buffer into which the data is read. 71//off - the start offset in the destination array b 72//len - the maximum number of bytes read. 73//Returns: 74//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. 75int ByteArrayInputStream::read(byteArray b, unsigned int offset, unsigned int length) 76{ 77 if( pos == count ) 78 return -1; 79 80 int k = min( length, count-pos ); 81 XMemCpy( &b[offset], &buf[pos], k ); 82 //std::copy( buf->data+pos, buf->data+pos+k, b->data + offset ); // Or this instead? 83 84 pos += k; 85 86 return k; 87} 88 89//Closing a ByteArrayInputStream has no effect. 90//The methods in this class can be called after the stream has been closed without generating an IOException. 91void ByteArrayInputStream::close() 92{ 93 return; 94} 95 96//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. 97//Overrides: 98//skip in class InputStream 99//Parameters: 100//n - the number of bytes to be skipped. 101//Returns: 102//the actual number of bytes skipped. 103__int64 ByteArrayInputStream::skip(__int64 n) 104{ 105 int newPos = pos + n; 106 107 if(newPos > count) newPos = count; 108 109 int k = newPos - pos; 110 pos = newPos; 111 112 return k; 113} 114 115ByteArrayInputStream::~ByteArrayInputStream() 116{ 117 if(buf.data != NULL) delete [] buf.data; 118}