--- a/sources/network/bytestream.h Wed Jul 20 18:31:19 2016 +0300 +++ b/sources/network/bytestream.h Fri Jul 22 17:59:55 2016 +0300 @@ -40,119 +40,51 @@ MAX_NETWORK_STRING = 0x800 }; -// TODO: Make able to handle big-endian too +class IOError : public std::exception +{ + String m_message; + +public: + IOError(String message) : + m_message(message) {} + + const char* what() const throw() + { + return m_message; + } +}; + class Bytestream { public: - class IOError : public std::exception - { - String m_message; - - public: - IOError (String message) : - m_message (message) {} - - const char* what() const throw() - { - return m_message; - } - }; - - Bytestream (unsigned long length = 0x800); - Bytestream (const unsigned char* data, unsigned long length); - Bytestream (const Vector<unsigned char>& bytes); - Bytestream (const Bytestream& other); - ~Bytestream(); - - void clear(); - void grow_to_fit (unsigned long bytes); - void read (unsigned char* buffer, unsigned long length); - int8_t read_byte(); - int32_t read_long(); - int16_t read_short(); - String read_string(); - float read_float(); - void resize (unsigned long length); - void write (const unsigned char* val, unsigned int length); - void write_buffer (const Bytestream& other); - void write_buffer (const Vector<unsigned char>& other); - void write_byte (int8_t val); - void write_double (double val); - void write_float (float val); - void write_long (int32_t val); - void write_short (int16_t val); - void write_string (const String& val); - - Bytestream& operator= (const Bytestream& other); - - unsigned long allocated_size() const - { - return m_allocatedSize; - } + Bytestream(ByteArray& data); - unsigned long bytes_left() const - { - return m_writtenLength - (m_cursor - &m_data[0]); - } - - inline unsigned char* data() - { - return m_data; - } - inline const unsigned char* data() const - { - return m_data; - } - - unsigned long position() const - { - return m_cursor - m_data; - } - - void rewind() - { - m_cursor = m_data; - } - - void seek (unsigned long pos) - { - m_cursor = m_data + pos; - } - - Vector<unsigned char> to_vector() const - { - return Vector<unsigned char> (m_data, m_writtenLength); - } - - unsigned long written_length() const - { - return m_writtenLength; - } - - unsigned char& operator[] (unsigned long idx) - { - return m_data[idx]; - } - - unsigned char operator[] (unsigned long idx) const - { - return m_data[idx]; - } + int bytesLeft() const; + ByteArray::Iterator getCurrentIterator(); + int position() const; + ByteArray readBuffer(int length); + int8_t readByte(); + int32_t readLong(); + int16_t readShort(); + String readString(); + float readFloat(); + void rewind(); + void seek(int position); + void write(const unsigned char* val, unsigned int length); + void writeBuffer(const ByteArray& other); + void writeByte(int8_t value); + void writeDouble(double val); + void writeFloat(float value); + void writeLong(int32_t value); + void writeShort(int16_t value); + void writeString(const String& string); private: - unsigned char* m_data; - unsigned char* m_cursor; - unsigned long m_allocatedSize; - unsigned long m_writtenLength; + ByteArray& m_data; + int m_position; - void init (const unsigned char* data, unsigned long length); - void write (unsigned char val); - void ensure_read_space (unsigned int bytes); - - unsigned long space_left() const - { - return m_allocatedSize - m_writtenLength; - } + int8_t read(); + void ensureReadSpace(int bytes); }; END_ZFC_NAMESPACE