AbZip  1.0.0
iobuffer.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 Andy Bray
4 **
5 ** This file is part of AbZip.
6 **
7 ** AbZip is free software: you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License as published by
9 ** the Free Software Foundation, either version 3 of the License, or
10 ** (at your option) any later version.
11 **
12 ** AbZip is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ** GNU General Public License for more details.
16 **
17 ** You should have received a copy of the GNU General Public License
18 ** along with AbZip. If not, see <http://www.gnu.org/licenses/>.
19 **
20 ****************************************************************************/
21 
22 #ifndef IOBUFFER_H
23 #define IOBUFFER_H
24 
25 #include <QObject>
26 #include <QIODevice>
27 
29 class IOBuffer
30 {
31 public:
33  IOBuffer();
34 
36  IOBuffer( int size );
37 
39  IOBuffer(const QByteArray &baSeed);
40 
42  void clear();
43 
44  int read( QIODevice* ioDevice, qint64 size );
45 
47  bool hasData();
48 
50  quint32 size() const;
51 
52  quint32 currentPosition();
53 
54  void seek(quint32 nPos);
55 
57  const QByteArray& getByteArray() const;
58 
60  quint8 readByte();
62  void writeByte( quint8 n);
63 
65  quint32 readUInt32();
66 
68  void writeUInt32(quint32 n);
69 
71  quint64 readUInt64();
72 
73 
75  void writeUInt64(quint64 n);
76 
78  QByteArray readByteArray(quint32 size);
80  void writeByteArray(const QByteArray &ba);
81 
82 
85  void writeRawBytes(const QByteArray &ba);
86 
88  void writeRawData(const void* data, int nLen);
89 
91  quint16 readUInt16();
93  void writeUInt16(quint16 n);
94 
96  void pad(quint8 nNumBytes);
98  void skip(quint8 nNumBytes);
99 
100 private:
101  QByteArray m_ba;
102  quint32 m_nCurrentPos;
103 
104  bool ensureSpace(quint32 nSize);
105 
106 };
107 
108 #endif // IOBUFFER_H
quint32 readUInt32()
reads a four byte unsigned integer from the message, and moves the current position one by 4 bytes...
Definition: iobuffer.cpp:103
void writeUInt64(quint64 n)
writes 8 bytes at the current position and moves the position forwards by 8 bytes.
Definition: iobuffer.cpp:136
const QByteArray & getByteArray() const
get a copy of the message as a byte array:
Definition: iobuffer.cpp:82
void writeRawBytes(const QByteArray &ba)
Definition: iobuffer.cpp:183
quint16 readUInt16()
Read an unsigned short:
Definition: iobuffer.cpp:143
Provides a message buffer that can be used to read & write data across the network.
Definition: iobuffer.h:29
void writeUInt32(quint32 n)
writes four bytes at the current position and moves the position forwards by 4 bytes.
Definition: iobuffer.cpp:128
void pad(quint8 nNumBytes)
Pad a certain number of empty (0) bytes:
Definition: iobuffer.cpp:196
void skip(quint8 nNumBytes)
Skip a number of bytes - to be used with the pad() method above:
Definition: iobuffer.cpp:204
void writeByteArray(const QByteArray &ba)
writes data size, then data (without nulls) to message. Modifies position.
Definition: iobuffer.cpp:172
quint32 size() const
get message size:
Definition: iobuffer.cpp:51
quint8 readByte()
read a single byte. Modifies internal position:
Definition: iobuffer.cpp:87
void writeByte(quint8 n)
Write a single byte.
Definition: iobuffer.cpp:98
bool hasData()
returns true if there is still data in the buffer to read
Definition: iobuffer.cpp:62
void clear()
Clear the contents of the message:
Definition: iobuffer.cpp:45
IOBuffer()
construct a new, empty buffer:
Definition: iobuffer.cpp:27
void writeRawData(const void *data, int nLen)
Write raw byte data to buffer.
Definition: iobuffer.cpp:189
quint64 readUInt64()
reads a 8 byte unsigned long from the message, and moves the current position one by 4 bytes...
Definition: iobuffer.cpp:115
void writeUInt16(quint16 n)
Write an unsigned short:
Definition: iobuffer.cpp:155
QByteArray readByteArray(quint32 size)
reads a string of size length. Modifies position.
Definition: iobuffer.cpp:161