All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ByteArray.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/datatype/ByteArray.cpp
22 
23  \brief A class for representing binary data.
24 */
25 
26 // TerraLib
27 #include "../common/HexUtils.h"
28 #include "ByteArray.h"
29 
30 // STL
31 #include <cassert>
32 #include <cstring>
33 
35  : m_data(0),
36  m_capacity(0),
37  m_bytesOccupied(0)
38 {
39 }
40 
41 te::dt::ByteArray::ByteArray(std::size_t capacity)
42  : m_data(0),
43  m_capacity(capacity),
44  m_bytesOccupied(0)
45 {
46  m_data = new char[capacity];
47 }
48 
49 te::dt::ByteArray::ByteArray(char* data, std::size_t size)
50 : m_data(data),
51  m_capacity(size),
52  m_bytesOccupied(size)
53 {
54 }
55 
56 te::dt::ByteArray::ByteArray(char* data, std::size_t capacity, std::size_t usedBytes)
57  : m_data(data),
58  m_capacity(capacity),
59  m_bytesOccupied(usedBytes)
60 {
61 }
62 
64  : m_capacity(rhs.m_capacity),
65  m_bytesOccupied(rhs.m_bytesOccupied)
66 {
67  if(m_capacity)
68  {
69  m_data = new char[m_capacity];
70  memcpy(m_data, rhs.m_data, rhs.m_bytesOccupied);
71  }
72  else
73  {
74  m_data = 0;
75  }
76 }
77 
79 {
80  if(this != &rhs)
81  {
82  delete [] m_data;
83  m_capacity = rhs.m_capacity;
84  m_bytesOccupied = rhs.m_bytesOccupied;
85 
86  if(m_capacity)
87  {
88  m_data = new char[m_capacity];
89  memcpy(m_data, rhs.m_data, rhs.m_bytesOccupied);
90  }
91  else
92  {
93  m_data = 0;
94  }
95  }
96 
97  return *this;
98 }
99 
101 {
102  delete [] m_data;
103 }
104 
106 {
107  return m_data;
108 }
109 
110 void te::dt::ByteArray::take(char* data, std::size_t size)
111 {
112  clear();
113 
114  m_data = data;
115  m_capacity = size;
116  m_bytesOccupied = size;
117 }
118 
119 void te::dt::ByteArray::take(char* data, std::size_t capacity, std::size_t usedBytes)
120 {
121  clear();
122 
123  m_data = data;
124  m_capacity = capacity;
125  m_bytesOccupied = usedBytes;
126 }
127 
128 void te::dt::ByteArray::copy(char* data, std::size_t size)
129 {
130  if(size > m_capacity)
131  {
132  clear();
133  m_data = new char[size];
134  m_capacity = size;
135  }
136 
137  m_bytesOccupied = size;
138 
139  memcpy(m_data, data, size);
140 }
141 
142 void te::dt::ByteArray::copy(char* data, std::size_t size, std::size_t offset)
143 {
144  if((offset + size) > m_capacity)
145  {
146  std::size_t newCapacity = offset + size;
147 
148  char* newData = new char[newCapacity];
149 
150  memcpy(newData, m_data, offset);
151 
152  memcpy(newData + offset, data, size);
153 
154  take(newData, newCapacity);
155  }
156  else
157  {
158  memcpy(m_data + offset, data, size);
159 
160  m_bytesOccupied = offset + size;
161  }
162 }
163 
164 std::size_t te::dt::ByteArray::capacity() const
165 {
166  return m_capacity;
167 }
168 
169 std::size_t te::dt::ByteArray::bytesUsed() const
170 {
171  return m_bytesOccupied;
172 }
173 
174 void te::dt::ByteArray::setBytesUsed(std::size_t size)
175 {
176  m_bytesOccupied = size;
177 }
178 
180 {
181  delete [] m_data;
182  m_data = 0;
183  m_capacity = 0;
184  m_bytesOccupied = 0;
185 }
186 
188 {
189  return new ByteArray(*this);
190 }
191 
192 std::string te::dt::ByteArray::toString() const
193 {
194  if(m_bytesOccupied == 0)
195  return std::string("");
196 
197  char* hexStr = te::common::Binary2Hex(m_data, m_bytesOccupied);
198 
199  std::string result(hexStr);
200 
201  delete [] hexStr;
202 
203  return result;
204 }
std::size_t bytesUsed() const
It returns the number of used bytes in the internal buffer.
Definition: ByteArray.cpp:169
A class for representing binary data.
char * Binary2Hex(const char *s, std::size_t size)
Each char from the array of characters in binary format is converted into a pair of hex characters...
Definition: HexUtils.h:602
char * m_data
The buffer data.
Definition: ByteArray.h:211
std::string toString() const
It returns the byte array in an string notation.
Definition: ByteArray.cpp:192
char * getData() const
It returns the data array.
Definition: ByteArray.cpp:105
AbstractData * clone() const
It creates a new clone of the byte array.
Definition: ByteArray.cpp:187
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
ByteArray()
It creates a new empty byte array.
Definition: ByteArray.cpp:34
~ByteArray()
Destructor.
Definition: ByteArray.cpp:100
ByteArray & operator=(const ByteArray &rhs)
Assignment operator.
Definition: ByteArray.cpp:78
std::size_t capacity() const
It returns the size of the internal buffer.
Definition: ByteArray.cpp:164
std::size_t m_bytesOccupied
The number of bytes occupied in the buffer.
Definition: ByteArray.h:213
std::size_t m_capacity
The number of bytes allocated by the internal buffer.
Definition: ByteArray.h:212
void copy(char *data, std::size_t size)
It copies the data from the given pointer to the byte array.
Definition: ByteArray.cpp:128
void setBytesUsed(std::size_t size)
It sets the number of used bytes in the internal buffer.
Definition: ByteArray.cpp:174
void clear()
It clears the byte array.
Definition: ByteArray.cpp:179
A class for representing binary data.
Definition: ByteArray.h:51
void take(char *data, std::size_t size)
It takes the ownership of the external data buffer.
Definition: ByteArray.cpp:110