Jpp  master_rocky
the software that should make you happy
JByteArrayIO.hh
Go to the documentation of this file.
1 #ifndef __JIO__JBYTEARRAYIO__
2 #define __JIO__JBYTEARRAYIO__
3 
4 #include <cstring>
5 #include <algorithm>
6 #include <vector>
7 
8 #include "JIO/JSerialisable.hh"
9 
10 
11 /**
12  * \author mdejong
13  */
14 
15 namespace JIO {}
16 namespace JPP { using namespace JIO; }
17 
18 namespace JIO {
19 
20 
21  /**
22  * Byte array binary input.
23  * This class implements the JReader interface.
24  */
26  public JReader
27  {
28  public:
29  /**
30  * Default constructor.
31  */
33  __data(NULL),
34  __size(0),
35  __pos (0)
36  {}
37 
38 
39  /**
40  * Constructor.
41  *
42  * \param buffer pointer to input data
43  * \param length number of bytes
44  */
45  JByteArrayReader(const char* buffer, const int length = 0) :
46  __data(buffer),
47  __size(length),
48  __pos (0)
49  {}
50 
51 
52  /**
53  * Get size.
54  *
55  * \return number of bytes
56  */
57  int size() const
58  {
59  return __size;
60  }
61 
62 
63  /**
64  * Get data.
65  *
66  * \return pointer to data
67  */
68  const char* data() const
69  {
70  return __data;
71  }
72 
73 
74  /**
75  * Get remaining size.
76  *
77  * \return number of bytes
78  */
79  int getRemainingSize() const
80  {
81  return __size - __pos;
82  }
83 
84 
85  /**
86  * Get remaining data.
87  *
88  * \return pointer to remaining data
89  */
90  const char* getRemainingData() const
91  {
92  return __data + __pos;
93  }
94 
95 
96  /**
97  * Get read position.
98  *
99  * \return read position
100  */
101  int tellg() const
102  {
103  return __pos;
104  }
105 
106 
107  /**
108  * Set read position.
109  *
110  * \param pos read position
111  */
112  void seekg(const int pos)
113  {
114  __pos = pos;
115  }
116 
117 
118  /**
119  * Status of reader.
120  *
121  * \return status of this reader
122  */
123  virtual bool getStatus() const override
124  {
125  return __size - __pos >= 0;
126  }
127 
128 
129  /**
130  * Read byte array.
131  *
132  * \param buffer pointer to byte array
133  * \param length number of bytes
134  * \return number of bytes
135  */
136  virtual int read(char* buffer, const int length) override
137  {
138  memcpy(buffer, __data + __pos, length);
139 
140  __pos += length;
141 
142  return length;
143  }
144 
145 
146  protected:
147  const char* __data;
148  int __size;
149  int __pos;
150  };
151 
152 
153  /**
154  * Byte array binary output.
155  * This class implements the JWriter interface.
156  */
158  public JWriter,
159  public std::vector<char>
160  {
161  public:
162  /**
163  * Constructor.
164  *
165  * \param size reserved number of bytes
166  */
167  JByteArrayWriter(const int size = 65536) :
168  std::vector<char>(),
169  __pos(0)
170  {
171  reserve(size);
172  }
173 
174 
175  /**
176  * Status of writer.
177  *
178  * \return status of this writer
179  */
180  virtual bool getStatus() const override
181  {
182  return true;
183  }
184 
185 
186  /**
187  * Clear buffer.
188  */
189  void clear()
190  {
192 
193  __pos = 0;
194  }
195 
196 
197  /**
198  * Get write position.
199  *
200  * \return write position
201  */
202  int tellp() const
203  {
204  return __pos;
205  }
206 
207 
208  /**
209  * Set write position.
210  *
211  * \param pos write position
212  */
213  void seekp(const int pos)
214  {
215  __pos = pos;
216  }
217 
218 
219  /**
220  * Write byte array.
221  *
222  * \param buffer pointer to byte array
223  * \param length number of bytes
224  * \return number of bytes
225  */
226  virtual int write(const char* buffer, const int length) override
227  {
228  if (__pos + length > (int) this->size()) {
229  this->resize(__pos + length);
230  }
231 
232  memcpy(this->data() + __pos, buffer, length);
233 
234  __pos += length;
235 
236  return length;
237  }
238 
239 
240  protected:
241  int __pos;
242  };
243 }
244 
245 #endif
Byte array binary input.
Definition: JByteArrayIO.hh:27
const char * data() const
Get data.
Definition: JByteArrayIO.hh:68
virtual int read(char *buffer, const int length) override
Read byte array.
JByteArrayReader()
Default constructor.
Definition: JByteArrayIO.hh:32
JByteArrayReader(const char *buffer, const int length=0)
Constructor.
Definition: JByteArrayIO.hh:45
int getRemainingSize() const
Get remaining size.
Definition: JByteArrayIO.hh:79
int tellg() const
Get read position.
const char * getRemainingData() const
Get remaining data.
Definition: JByteArrayIO.hh:90
int size() const
Get size.
Definition: JByteArrayIO.hh:57
virtual bool getStatus() const override
Status of reader.
void seekg(const int pos)
Set read position.
Byte array binary output.
int tellp() const
Get write position.
virtual bool getStatus() const override
Status of writer.
JByteArrayWriter(const int size=65536)
Constructor.
void clear()
Clear buffer.
void seekp(const int pos)
Set write position.
virtual int write(const char *buffer, const int length) override
Write byte array.
Interface for binary input.
Interface for binary output.
Auxiliary classes and methods for binary I/O.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JSTDTypes.hh:14