sources/network/bytestream.h

changeset 13
09dcaeaa216b
parent 11
cffa2777d917
child 73
07dda51a7a8e
equal deleted inserted replaced
12:8d0d1b368de0 13:09dcaeaa216b
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #pragma once 31 #pragma once
32 #include <stdexcept>
32 #include "../main.h" 33 #include "../main.h"
33 34
34 class String; 35 class String;
35 36
36 enum { MAX_NETWORK_STRING = 0x800 }; 37 enum { MAX_NETWORK_STRING = 0x800 };
37 38
38 // TODO: Make able to handle big-endian too 39 // TODO: Make able to handle big-endian too
39 class Bytestream 40 class Bytestream
40 { 41 {
41 public: 42 public:
42 static bool sink; 43 class IOError : public std::exception
44 {
45 String m_message;
46
47 public:
48 IOError (String message) :
49 m_message (message) {}
50
51 inline METHOD
52 what() const throw() -> const char*
53 {
54 return m_message.chars();
55 }
56 };
43 57
44 Bytestream (unsigned long length = 0x800); 58 Bytestream (unsigned long length = 0x800);
45 Bytestream (const unsigned char* data, unsigned long length); 59 Bytestream (const unsigned char* data, unsigned long length);
46 Bytestream (const Vector<unsigned char>& bytes); 60 Bytestream (const Vector<unsigned char>& bytes);
47 Bytestream (const Bytestream& other); 61 Bytestream (const Bytestream& other);
52 METHOD clear() -> void; 66 METHOD clear() -> void;
53 inline METHOD data() -> unsigned char*; 67 inline METHOD data() -> unsigned char*;
54 inline METHOD data() const -> const unsigned char*; 68 inline METHOD data() const -> const unsigned char*;
55 METHOD grow_to_fit (unsigned long bytes) -> void; 69 METHOD grow_to_fit (unsigned long bytes) -> void;
56 inline METHOD position() const -> unsigned long; 70 inline METHOD position() const -> unsigned long;
57 METHOD read (unsigned char* buffer, unsigned long length, bool* ok = &sink) -> void; 71 METHOD read (unsigned char* buffer, unsigned long length) -> void;
58 METHOD read_byte (bool* ok = &sink) -> char; 72 METHOD read_byte() -> char;
59 METHOD read_short (bool* ok = &sink) -> short int; 73 METHOD read_short() -> short int;
60 METHOD read_long (bool* ok = &sink) -> long int; 74 METHOD read_long() -> long int;
61 METHOD read_string (bool* ok = &sink) -> String; 75 METHOD read_string() -> String;
62 METHOD read_float (bool* ok = &sink) -> float; 76 METHOD read_float() -> float;
63 METHOD resize (unsigned long length) -> void; 77 METHOD resize (unsigned long length) -> void;
64 inline METHOD rewind() -> void; 78 inline METHOD rewind() -> void;
65 inline METHOD seek (unsigned long pos) -> void; 79 inline METHOD seek (unsigned long pos) -> void;
66 inline METHOD to_vector() const -> Vector<unsigned char>; 80 inline METHOD to_vector() const -> Vector<unsigned char>;
67 METHOD write (const unsigned char* val, unsigned int length) -> void; 81 METHOD write (const unsigned char* val, unsigned int length) -> void;
85 unsigned long m_allocatedSize; 99 unsigned long m_allocatedSize;
86 unsigned long m_writtenLength; 100 unsigned long m_writtenLength;
87 101
88 METHOD init (const unsigned char* data, unsigned long length) -> void; 102 METHOD init (const unsigned char* data, unsigned long length) -> void;
89 METHOD write (unsigned char val) -> void; 103 METHOD write (unsigned char val) -> void;
104 METHOD ensure_read_space (unsigned int bytes) -> void;
90 inline METHOD space_left() const -> unsigned long; 105 inline METHOD space_left() const -> unsigned long;
91 }; 106 };
92 107
93 // ------------------------------------------------------------------------------------------------- 108 // -------------------------------------------------------------------------------------------------
94 // 109 //

mercurial