ESA JPIP server  0.1
databin_writer.h
Go to the documentation of this file.
1 #ifndef _JPIP_DATABIN_WRITER_H_
2 #define _JPIP_DATABIN_WRITER_H_
3 
4 
5 #include <stdint.h>
6 #include "jpip.h"
7 #include "data/file.h"
8 #include "data/file_segment.h"
10 
11 
12 namespace jpip
13 {
14 
15  using namespace std;
16  using namespace data;
17  using namespace jpeg2000;
18 
19 
29  {
30  private:
35  bool eof;
36  char *ini;
37  char *ptr;
38  char *end;
39 
44 
50  template<typename T> DataBinWriter& WriteValue(T value)
51  {
52  if(!eof) {
53  if((ptr + sizeof(T)) >= end) eof = true;
54  else {
55  for (int i = sizeof(T) - 1; i >= 0; i--)
56  *ptr++ = (value >> (8 * i)) & 0xFF;
57  }
58  }
59 
60  return *this;
61  }
62 
68  DataBinWriter& WriteVBAS(uint64_t value);
69 
79  DataBinWriter& WriteHeader(uint64_t bin_id, uint64_t bin_offset,
80  uint64_t bin_length, bool last_byte = false);
81 
82  public:
87  {
88  eof = true;
89  databin_class = -1;
90  codestream_idx = -1;
91  prev_databin_class = -1;
92  prev_codestream_idx = -1;
93  ini = ptr = end = NULL;
94  }
95 
102  DataBinWriter& SetBuffer(char *buf, int buf_len)
103  {
104  eof = false;
105  ini = ptr = buf;
106  end = ini + buf_len;
107 
108  return *this;
109  }
110 
117  {
118  databin_class = -1;
119  codestream_idx = -1;
120  prev_databin_class = -1;
121  prev_codestream_idx = -1;
122 
123  return *this;
124  }
125 
132  {
133  if(value < 0) value = 0;
134  codestream_idx = value;
135  return *this;
136  }
137 
143  DataBinWriter& SetDataBinClass(int databin_class)
144  {
145  this->databin_class = databin_class;
146  return *this;
147  }
148 
159  DataBinWriter& Write(uint64_t bin_id, uint64_t bin_offset,
160  const File& file, const FileSegment& segment,
161  bool last_byte = false);
162 
173  DataBinWriter& WritePlaceHolder(uint64_t bin_id, uint64_t bin_offset,
174  const File& file, const PlaceHolder& place_holder,
175  bool last_byte = false);
176 
182  DataBinWriter& WriteEmpty(uint64_t bin_id = 0);
183 
187  int GetCount() const
188  {
189  return (ptr - ini);
190  }
191 
195  int GetFree() const
196  {
197  return (end - ptr);
198  }
199 
205  DataBinWriter& WriteEOR(int reason)
206  {
207  if((ptr + 3) > end) eof = true;
208  else {
209  *ptr++ = 0;
210  *ptr++ = (char)reason;
211  *ptr++ = 0;
212  }
213 
214  return *this;
215  }
216 
221  operator bool() const
222  {
223  return !eof;
224  }
225 
226  virtual ~DataBinWriter()
227  {
228  }
229  };
230 
231 }
232 
233 
234 #endif /* _JPIP_DATABIN_WRITER_H_ */
int GetFree() const
Returns the number of bytes available.
Definition: databin_writer.h:195
int databin_class
Current data-bin class.
Definition: databin_writer.h:40
DataBinWriter & ClearPreviousIds()
Clears the previous identifiers of data-bin class and codestream index numbers.
Definition: databin_writer.h:116
char * end
Pointer to the end of the buffer.
Definition: databin_writer.h:38
Identifies a data segment of a file.
Definition: file_segment.h:20
Class used to generate data-bin segments and write them into a memory buffer.
Definition: databin_writer.h:28
Contains the information of a place-holder.
Definition: place_holder.h:18
virtual ~DataBinWriter()
Definition: databin_writer.h:226
DataBinWriter & WriteEOR(int reason)
Writes a EOR message into the buffer.
Definition: databin_writer.h:205
int prev_codestream_idx
Previous codestream index number.
Definition: databin_writer.h:43
char * ptr
Current position of the buffer.
Definition: databin_writer.h:37
int prev_databin_class
Previous data-bin class.
Definition: databin_writer.h:42
bool eof
true if the end of the buffer has been reached and the last value could not be written.
Definition: databin_writer.h:35
DataBinWriter & SetDataBinClass(int databin_class)
Sets the current data-bin class.
Definition: databin_writer.h:143
DataBinWriter & SetCodestream(int value)
Sets the current codestream.
Definition: databin_writer.h:131
int GetCount() const
Returns the number of bytes written.
Definition: databin_writer.h:187
char * ini
Pointer to the beginning of the buffer.
Definition: databin_writer.h:36
DataBinWriter & WriteValue(T value)
Writes a value into the buffer.
Definition: databin_writer.h:50
int codestream_idx
Current codestream index number.
Definition: databin_writer.h:41
DataBinWriter()
Initializes the object.
Definition: databin_writer.h:86
DataBinWriter & SetBuffer(char *buf, int buf_len)
Sets the associated memory buffer.
Definition: databin_writer.h:102