Fix N_SE-32099 : zero length string may not save to setting.xml
[platform/framework/native/appfw.git] / src / base / FBaseByteBuffer.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FBaseByteBuffer.cpp
20  * @brief               This is the implementation for ByteBuffer class.
21  */
22
23 #include <new>
24 #include <FBaseByteBuffer.h>
25 #include <FBaseSysLog.h>
26 #include <unique_ptr.h>
27
28
29 static const byte DOUBLE_SIZE = sizeof(double);
30 static const byte FLOAT_SIZE = sizeof(float);
31 static const byte INT_SIZE = sizeof(int);
32 static const byte LONG_SIZE = sizeof(long);
33 static const byte LONGLONG_SIZE = sizeof(long long);
34 static const byte MCHAR_SIZE = sizeof(wchar_t);
35 static const byte SHORT_SIZE = sizeof(short);
36
37 namespace Tizen { namespace Base
38 {
39
40 ByteBuffer::ByteBuffer(void)
41         : __pByteBufferImpl(null)
42 {
43 }
44
45 ByteBuffer::~ByteBuffer(void)
46 {
47 }
48
49 result
50 ByteBuffer::Construct(const ByteBuffer& buffer)
51 {
52         SysTryReturn(NID_BASE, null != buffer._pData, E_INVALID_ARG, E_INVALID_ARG,
53                 "[%s] The source buffer is not constructed.", GetErrorMessage(E_INVALID_ARG));
54
55         _capacity = buffer._capacity;
56         _position = buffer._position;
57         _limit = buffer._limit;
58         _mark = buffer._mark;
59         _pData = buffer._pData;
60
61         AddRef();
62         __pArrayStart = buffer.__pArrayStart;
63
64         return E_SUCCESS;
65 }
66
67 result
68 ByteBuffer::Construct(int capacity)
69 {
70         result r = BufferBase::Construct(capacity);
71         if (r != E_SUCCESS)
72         {
73                 SysLogException(NID_BASE, r, "[%s] Propagated.", GetErrorMessage(r));
74         }
75
76         return r;
77 }
78
79 result
80 ByteBuffer::Construct(const byte *pBuffer, int index, int length, int capacity)
81 {
82         SysTryReturn(NID_BASE, pBuffer != null, E_INVALID_ARG, E_INVALID_ARG, "[%s] The pBuffer is null.",
83                 GetErrorMessage(E_INVALID_ARG));
84         SysTryReturn(NID_BASE, index >= 0 && length >= 0 && capacity >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
85                 "[%s] index(%d), length(%d) and capacity(%d) MUST be greater than or equal to 0.",
86                 GetErrorMessage(E_OUT_OF_RANGE), index, length, capacity);
87         SysTryReturn(NID_BASE, index < capacity && length <= capacity && index + length <= capacity,
88                 E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index(%d), length(%d) and capacity(%d) MUST be greater than or equal to 0.",
89                 GetErrorMessage(E_OUT_OF_RANGE), index, length, capacity);
90
91         void* pTemp = null;
92         int sizeofBufferData = sizeof(_BufferData);
93
94         __pArrayStart = const_cast<byte *> (pBuffer + index);
95
96         pTemp = malloc(sizeofBufferData);
97         SysTryReturn(NID_BASE, pTemp != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s]", GetErrorMessage(E_OUT_OF_MEMORY));
98
99         memset(pTemp, 0, sizeofBufferData);
100         _pData = static_cast <_BufferData*>(pTemp);
101
102         _pData->refCount = 1;
103         _pData->capacityInByte = capacity;
104
105         _capacity = capacity;
106         _limit = length;
107
108         return E_SUCCESS;
109 }
110
111 byte&
112 ByteBuffer::operator [](int index)
113 {
114         SysAssertf(index < _limit, "index out of range.\n");
115         SysAssertf(index >= 0, "index out of range.\n");
116
117         return __pArrayStart[index];
118 }
119
120 byte
121 ByteBuffer::operator [](int index) const
122 {
123         return const_cast<ByteBuffer&>(*this)[index];
124 }
125
126 bool
127 ByteBuffer::operator ==(const ByteBuffer& buffer) const
128 {
129         bool r = true;
130
131         if (this == &buffer)
132         {
133                 r = true;
134         }
135         else if (GetRemaining() != buffer.GetRemaining())
136         {
137                 r = false;
138         }
139         else
140         {
141                 for (int i = 0; i < GetRemaining(); ++i)
142                 {
143                         if (__pArrayStart[_position + i] != buffer.__pArrayStart[buffer._position + i])
144                         {
145                                 r = false;
146                                 break;
147                         }
148                 }
149         }
150
151         return r;
152 }
153
154 bool
155 ByteBuffer::operator !=(const ByteBuffer& buffer) const
156 {
157         return !(*this == buffer);
158 }
159
160 DoubleBuffer*
161 ByteBuffer::AsDoubleBufferN(void) const
162 {
163         std::unique_ptr<DoubleBuffer> pBuffer(new (std::nothrow) DoubleBuffer());
164         pBuffer->_pData = _pData;
165         AddRef();
166         pBuffer->_capacity = GetRemaining() / DOUBLE_SIZE;
167         pBuffer->_limit = pBuffer->_capacity;
168         if (pBuffer->_capacity > 0)
169         {
170                 pBuffer->__pArrayStart = &__pArrayStart[_position];
171         }
172
173         return pBuffer.release();
174 }
175
176 FloatBuffer*
177 ByteBuffer::AsFloatBufferN(void) const
178 {
179         std::unique_ptr<FloatBuffer> pBuffer(new (std::nothrow) FloatBuffer());
180         pBuffer->_pData = _pData;
181         AddRef();
182         pBuffer->_capacity = GetRemaining() / FLOAT_SIZE;
183         pBuffer->_limit = pBuffer->_capacity;
184         if (pBuffer->_capacity > 0)
185         {
186                 pBuffer->__pArrayStart = &__pArrayStart[_position];
187         }
188
189         return pBuffer.release();
190 }
191
192 IntBuffer*
193 ByteBuffer::AsIntBufferN(void) const
194 {
195         std::unique_ptr<IntBuffer> pBuffer(new (std::nothrow) IntBuffer());
196         pBuffer->_pData = _pData;
197         AddRef();
198         pBuffer->_capacity = GetRemaining() / INT_SIZE;
199         pBuffer->_limit = pBuffer->_capacity;
200         if (pBuffer->_capacity > 0)
201         {
202                 pBuffer->__pArrayStart = &__pArrayStart[_position];
203         }
204
205         return pBuffer.release();
206 }
207
208 LongBuffer*
209 ByteBuffer::AsLongBufferN(void) const
210 {
211         std::unique_ptr<LongBuffer> pBuffer(new (std::nothrow) LongBuffer());
212         pBuffer->_pData = _pData;
213         AddRef();
214         pBuffer->_capacity = GetRemaining() / LONG_SIZE;
215         pBuffer->_limit = pBuffer->_capacity;
216         if (pBuffer->_capacity > 0)
217         {
218                 pBuffer->__pArrayStart = &__pArrayStart[_position];
219         }
220
221         return pBuffer.release();
222 }
223
224 LongLongBuffer*
225 ByteBuffer::AsLongLongBufferN(void) const
226 {
227         std::unique_ptr<LongLongBuffer> pBuffer(new (std::nothrow) LongLongBuffer());
228         pBuffer->_pData = _pData;
229         AddRef();
230         pBuffer->_capacity = GetRemaining() / LONGLONG_SIZE;
231         pBuffer->_limit = pBuffer->_capacity;
232         if (pBuffer->_capacity > 0)
233         {
234                 pBuffer->__pArrayStart = &__pArrayStart[_position];
235         }
236
237         return pBuffer.release();
238 }
239
240 McharBuffer*
241 ByteBuffer::AsMcharBufferN(void) const
242 {
243         std::unique_ptr<McharBuffer> pBuffer(new (std::nothrow) McharBuffer());
244         pBuffer->_pData = _pData;
245         AddRef();
246         pBuffer->_capacity = GetRemaining() / MCHAR_SIZE;
247         pBuffer->_limit = pBuffer->_capacity;
248         if (pBuffer->_capacity > 0)
249         {
250                 pBuffer->__pArrayStart = &__pArrayStart[_position];
251         }
252
253         return pBuffer.release();
254 }
255
256 WcharBuffer*
257 ByteBuffer::AsWcharBufferN(void) const
258 {
259         std::unique_ptr<WcharBuffer> pBuffer(new (std::nothrow) WcharBuffer());
260         pBuffer->_pData = _pData;
261         AddRef();
262         pBuffer->_capacity = GetRemaining() / MCHAR_SIZE;
263         pBuffer->_limit = pBuffer->_capacity;
264         if (pBuffer->_capacity > 0)
265         {
266                 pBuffer->__pArrayStart = &__pArrayStart[_position];
267         }
268
269         return pBuffer.release();
270 }
271
272 ShortBuffer*
273 ByteBuffer::AsShortBufferN(void) const
274 {
275         std::unique_ptr<ShortBuffer> pBuffer(new (std::nothrow) ShortBuffer());
276         pBuffer->_pData = _pData;
277         AddRef();
278         pBuffer->_capacity = GetRemaining() / SHORT_SIZE;
279         pBuffer->_limit = pBuffer->_capacity;
280         if (pBuffer->_capacity > 0)
281         {
282                 pBuffer->__pArrayStart = &__pArrayStart[_position];
283         }
284
285         return pBuffer.release();
286 }
287
288 result
289 ByteBuffer::CopyFrom(ByteBuffer& buffer)
290 {
291         SysTryReturn(NID_BASE, this != static_cast <void*>(&buffer), E_INVALID_ARG, E_INVALID_ARG,
292                 "[%s] The source and target buffers are identical.", GetErrorMessage(E_INVALID_ARG));
293         int copyLength = buffer.GetRemaining();
294         SysTryReturn(NID_BASE,
295                 GetRemaining() >= copyLength, E_OVERFLOW, E_OVERFLOW,
296                 "[%s] The current buffer is smaller than the input buffer.", GetErrorMessage(E_OVERFLOW));
297
298         memcpy(__pArrayStart + _position, buffer.__pArrayStart + buffer._position, copyLength);
299         _position += copyLength;
300         buffer._position += copyLength;
301
302         return E_SUCCESS;
303 }
304
305 result
306 ByteBuffer::GetArray(byte* pArray, int index, int length)
307 {
308         SysTryReturn(NID_BASE, null != pArray, E_INVALID_ARG, E_INVALID_ARG, "[%s] The pArray is null.",
309                 GetErrorMessage(E_INVALID_ARG));
310         SysTryReturn(NID_BASE, index >= 0 && length >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
311                 "[%s] Both of index(%d) and length(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE),
312                 index, length);
313         SysTryReturn(NID_BASE, GetRemaining() >= length, E_UNDERFLOW, E_UNDERFLOW, "[%s]", GetErrorMessage(E_UNDERFLOW));
314
315         memcpy(pArray + index, __pArrayStart + _position, length);
316         _position += length;
317
318         return E_SUCCESS;
319 }
320
321 result
322 ByteBuffer::GetByte(byte& value)
323 {
324         SysTryReturn(NID_BASE, _position < _limit, E_UNDERFLOW, E_UNDERFLOW,
325                 "[%s] The current position is not smaller than the limit.", GetErrorMessage(E_UNDERFLOW));
326
327         value = __pArrayStart[_position++];
328
329         return E_SUCCESS;
330 }
331
332 result
333 ByteBuffer::GetByte(int index, byte& value) const
334 {
335         SysTryReturn(NID_BASE, index < _limit && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
336                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the current limit(%d).", GetErrorMessage(E_OUT_OF_RANGE),
337                 index, _limit);
338
339         value = __pArrayStart[index];
340
341         return E_SUCCESS;
342 }
343
344 result
345 ByteBuffer::GetDouble(double& value)
346 {
347         SysTryReturn(NID_BASE,
348                 GetRemaining() >= DOUBLE_SIZE, E_UNDERFLOW, E_UNDERFLOW,
349                 "[%s] The remaining bytes of this buffer are smaller than the size of double", GetErrorMessage(E_UNDERFLOW));
350
351         value = *reinterpret_cast <double*>(&__pArrayStart[_position]);
352         _position += DOUBLE_SIZE;
353
354         return E_SUCCESS;
355 }
356
357 result
358 ByteBuffer::GetDouble(int index, double& value) const
359 {
360         SysTryReturn(NID_BASE,
361                 index <= (_limit - DOUBLE_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
362                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of double(%d)'.",
363                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, DOUBLE_SIZE);
364
365         value = *reinterpret_cast <double*>(&__pArrayStart[index]);
366
367         return E_SUCCESS;
368 }
369
370 result
371 ByteBuffer::GetFloat(float& value)
372 {
373         SysTryReturn(NID_BASE,
374                 GetRemaining() >= FLOAT_SIZE, E_UNDERFLOW, E_UNDERFLOW,
375                 "[%s] The remaining bytes of this buffer are smaller than the size of float.", GetErrorMessage(E_UNDERFLOW));
376
377         value = *reinterpret_cast <float*>(&__pArrayStart[_position]);
378         _position += FLOAT_SIZE;
379
380         return E_SUCCESS;
381 }
382
383 result
384 ByteBuffer::GetFloat(int index, float& value) const
385 {
386         SysTryReturn(NID_BASE,
387                 index <= (_limit - FLOAT_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
388                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of float(%d)'.",
389                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, FLOAT_SIZE);
390
391         value = *reinterpret_cast <float*>(&__pArrayStart[index]);
392
393         return E_SUCCESS;
394 }
395
396 result
397 ByteBuffer::GetInt(int& value)
398 {
399         SysTryReturn(NID_BASE,
400                 GetRemaining() >= INT_SIZE, E_UNDERFLOW, E_UNDERFLOW,
401                 "[%s] The remaining bytes of this buffer are smaller than the size of int.", GetErrorMessage(E_UNDERFLOW));
402
403         value = *reinterpret_cast <int*>(&__pArrayStart[_position]);
404         _position += INT_SIZE;
405
406         return E_SUCCESS;
407 }
408
409 result
410 ByteBuffer::GetInt(int index, int& value) const
411 {
412         SysTryReturn(NID_BASE,
413                 index <= (_limit - INT_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
414                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of int(%d)'.",
415                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, INT_SIZE);
416
417         value = *reinterpret_cast <int*>(&__pArrayStart[index]);
418
419         return E_SUCCESS;
420 }
421
422 result
423 ByteBuffer::GetLong(long& value)
424 {
425         SysTryReturn(NID_BASE,
426                 GetRemaining() >= LONG_SIZE, E_UNDERFLOW, E_UNDERFLOW,
427                 "[%s] The remaining bytes of this buffer are smaller than the size of long.", GetErrorMessage(E_UNDERFLOW));
428
429         value = *reinterpret_cast <long*>(&__pArrayStart[_position]);
430         _position += LONG_SIZE;
431
432         return E_SUCCESS;
433 }
434
435 result
436 ByteBuffer::GetLong(int index, long& value) const
437 {
438         SysTryReturn(NID_BASE,
439                 index <= (_limit - LONG_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
440                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of long(%d)'.",
441                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, LONG_SIZE);
442
443         value = *reinterpret_cast <long*>(&__pArrayStart[index]);
444
445         return E_SUCCESS;
446 }
447
448 result
449 ByteBuffer::GetLongLong(long long& value)
450 {
451         SysTryReturn(NID_BASE,
452                 GetRemaining() >= LONGLONG_SIZE, E_UNDERFLOW, E_UNDERFLOW,
453                 "[%s] The remaining bytes of this buffer are smaller than the size of long long.", GetErrorMessage(E_UNDERFLOW));
454
455         value = *reinterpret_cast <long long*>(&__pArrayStart[_position]);
456         _position += LONGLONG_SIZE;
457
458         return E_SUCCESS;
459 }
460
461 result
462 ByteBuffer::GetLongLong(int index, long long& value) const
463 {
464         SysTryReturn(NID_BASE,
465                 index <= (_limit - LONGLONG_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
466                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of long long(%d)'.",
467                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, LONGLONG_SIZE);
468
469         value = *reinterpret_cast <long long*>(&__pArrayStart[index]);
470
471         return E_SUCCESS;
472 }
473
474 result
475 ByteBuffer::GetMchar(wchar_t& value)
476 {
477         SysTryReturn(NID_BASE,
478                 GetRemaining() >= MCHAR_SIZE, E_UNDERFLOW, E_UNDERFLOW,
479                 "[%s] The remaining bytes of this buffer are smaller than the size of wchar_t.", GetErrorMessage(E_UNDERFLOW));
480
481         value = *reinterpret_cast <wchar_t*>(&__pArrayStart[_position]);
482         _position += MCHAR_SIZE;
483
484         return E_SUCCESS;
485 }
486
487 result
488 ByteBuffer::GetWchar(wchar_t& value)
489 {
490         SysTryReturn(NID_BASE,
491                 GetRemaining() >= MCHAR_SIZE, E_UNDERFLOW, E_UNDERFLOW,
492                 "[%s] The remaining bytes of this buffer are smaller than the size of wchar_t.", GetErrorMessage(E_UNDERFLOW));
493
494         value = *reinterpret_cast <wchar_t*>(&__pArrayStart[_position]);
495         _position += MCHAR_SIZE;
496
497         return E_SUCCESS;
498 }
499
500 result
501 ByteBuffer::GetMchar(int index, wchar_t& value) const
502 {
503         SysTryReturn(NID_BASE,
504                 index <= (_limit - MCHAR_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
505                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of wchar_t(%d)'.",
506                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, MCHAR_SIZE);
507
508         value = *reinterpret_cast <wchar_t*>(&__pArrayStart[index]);
509
510         return E_SUCCESS;
511 }
512
513 result
514 ByteBuffer::GetWchar(int index, wchar_t& value) const
515 {
516         SysTryReturn(NID_BASE,
517                 index <= (_limit - MCHAR_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
518                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of wchar_t(%d)'.",
519                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, MCHAR_SIZE);
520
521         value = *reinterpret_cast <wchar_t*>(&__pArrayStart[index]);
522
523         return E_SUCCESS;
524 }
525
526 result
527 ByteBuffer::GetShort(short& value)
528 {
529         SysTryReturn(NID_BASE,
530                 GetRemaining() >= SHORT_SIZE, E_UNDERFLOW, E_UNDERFLOW,
531                 "[%s] The remaining bytes of this buffer are smaller than the size of short.", GetErrorMessage(E_UNDERFLOW));
532
533         value = *reinterpret_cast <short*>(&__pArrayStart[_position]);
534         _position += SHORT_SIZE;
535
536         return E_SUCCESS;
537 }
538
539 result
540 ByteBuffer::GetShort(int index, short& value) const
541 {
542         SysTryReturn(NID_BASE,
543                 index <= (_limit - SHORT_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
544                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of short(%d)'.",
545                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, SHORT_SIZE);
546
547         value = *reinterpret_cast <short*>(&__pArrayStart[index]);
548
549         return E_SUCCESS;
550 }
551
552 result
553 ByteBuffer::ReadFrom(ByteBuffer& buffer)
554 {
555         SysTryReturn(NID_BASE, this != static_cast <void*>(&buffer), E_INVALID_ARG, E_INVALID_ARG,
556                 "[%s] The source and target buffers are identical.", GetErrorMessage(E_INVALID_ARG));
557
558         int copyLength = (GetRemaining() < buffer.GetRemaining()) ? GetRemaining() : buffer.GetRemaining();
559         memcpy(__pArrayStart + _position, buffer.__pArrayStart + buffer._position, copyLength);
560         _position += copyLength;
561         buffer._position += copyLength;
562
563         return E_SUCCESS;
564 }
565
566 result
567 ByteBuffer::SetArray(const byte* pArray, int index, int length)
568 {
569         SysTryReturn(NID_BASE, null != pArray, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The pArray is null.");
570         SysTryReturn(NID_BASE, index >= 0 && length >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
571                 "[%s] Both of index(%d) and length(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE),
572                 index, length);
573         SysTryReturn(NID_BASE, GetRemaining() >= length, E_OVERFLOW, E_OVERFLOW, "[%s] The remaining bytes are smaller than length.",
574                 GetErrorMessage(E_OVERFLOW));
575
576         memcpy(__pArrayStart + _position, pArray + index, length);
577         _position += length;
578
579         return E_SUCCESS;
580 }
581
582 result
583 ByteBuffer::SetByte(byte value)
584 {
585         SysTryReturn(NID_BASE, _position < _limit, E_OVERFLOW, E_OVERFLOW,
586                 "[%s] The current position is not smaller than the limit.", GetErrorMessage(E_OVERFLOW));
587
588         __pArrayStart[_position++] = value;
589         return E_SUCCESS;
590 }
591
592 result
593 ByteBuffer::SetByte(int index, byte value)
594 {
595         SysTryReturn(NID_BASE, index < _limit && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
596                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the current limit(%d).",
597                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit);
598
599         __pArrayStart[index] = value;
600         return E_SUCCESS;
601 }
602
603 result
604 ByteBuffer::SetDouble(double value)
605 {
606         SysTryReturn(NID_BASE,
607                 GetRemaining() >= DOUBLE_SIZE, E_OVERFLOW, E_OVERFLOW,
608                 "[%s] The remaining bytes of this buffer are smaller than size of double.", GetErrorMessage(E_OVERFLOW));
609
610         *reinterpret_cast <double*>(&__pArrayStart[_position]) = value;
611         _position += DOUBLE_SIZE;
612
613         return E_SUCCESS;
614 }
615
616 result
617 ByteBuffer::SetDouble(int index, double value)
618 {
619         SysTryReturn(NID_BASE,
620                 index <= (_limit - DOUBLE_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
621                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of double(%d)'.",
622                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, DOUBLE_SIZE);
623
624         *reinterpret_cast <double*>(&__pArrayStart[index]) = value;
625
626         return E_SUCCESS;
627 }
628
629 result
630 ByteBuffer::SetFloat(float value)
631 {
632         SysTryReturn(NID_BASE,
633                 GetRemaining() >= FLOAT_SIZE, E_OVERFLOW, E_OVERFLOW,
634                 "[%s] The remaining bytes of this buffer are smaller than size of float.", GetErrorMessage(E_OVERFLOW));
635
636         *reinterpret_cast <float*>(&__pArrayStart[_position]) = value;
637         _position += FLOAT_SIZE;
638
639         return E_SUCCESS;
640 }
641
642 result
643 ByteBuffer::SetFloat(int index, float value)
644 {
645         SysTryReturn(NID_BASE,
646                 index <= (_limit - FLOAT_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
647                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of float(%d)'.",
648                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, FLOAT_SIZE);
649
650         *reinterpret_cast <float*>(&__pArrayStart[index]) = value;
651
652         return E_SUCCESS;
653 }
654
655 result
656 ByteBuffer::SetInt(int value)
657 {
658         SysTryReturn(NID_BASE,
659                 GetRemaining() >= INT_SIZE, E_OVERFLOW, E_OVERFLOW,
660                 "[%s] The remaining bytes of this buffer are smaller than size of int.", GetErrorMessage(E_OVERFLOW));
661
662         *reinterpret_cast <int*>(&__pArrayStart[_position]) = value;
663         _position += INT_SIZE;
664
665         return E_SUCCESS;
666 }
667
668 result
669 ByteBuffer::SetInt(int index, int value)
670 {
671         SysTryReturn(NID_BASE,
672                 index <= (_limit - INT_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
673                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of int(%d)'.",
674                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, INT_SIZE);
675
676         *reinterpret_cast <int*>(&__pArrayStart[index]) = value;
677
678         return E_SUCCESS;
679 }
680
681 result
682 ByteBuffer::SetLong(long value)
683 {
684         SysTryReturn(NID_BASE,
685                 GetRemaining() >= LONG_SIZE, E_OVERFLOW, E_OVERFLOW,
686                 "[%s] The remaining bytes of this buffer are smaller than size of long.", GetErrorMessage(E_OVERFLOW));
687
688         *reinterpret_cast <long*>(&__pArrayStart[_position]) = value;
689         _position += LONG_SIZE;
690
691         return E_SUCCESS;
692 }
693
694 result
695 ByteBuffer::SetLong(int index, long value)
696 {
697         SysTryReturn(NID_BASE,
698                 index <= (_limit - LONG_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
699                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of long(%d)'.",
700                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, LONG_SIZE);
701
702         *reinterpret_cast <long*>(&__pArrayStart[index]) = value;
703
704         return E_SUCCESS;
705 }
706
707 result
708 ByteBuffer::SetLongLong(long long value)
709 {
710         SysTryReturn(NID_BASE,
711                 GetRemaining() >= LONGLONG_SIZE, E_OVERFLOW, E_OVERFLOW,
712                 "[%s] The remaining bytes of this buffer are smaller than size of long long.", GetErrorMessage(E_OVERFLOW));
713
714         *reinterpret_cast <long long*>(&__pArrayStart[_position]) = value;
715         _position += LONGLONG_SIZE;
716
717         return E_SUCCESS;
718 }
719
720 result
721 ByteBuffer::SetLongLong(int index, long long value)
722 {
723         SysTryReturn(NID_BASE,
724                 index <= (_limit - LONGLONG_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
725                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of long long(%d)'.",
726                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, LONGLONG_SIZE);
727
728         *reinterpret_cast <long long*>(&__pArrayStart[index]) = value;
729
730         return E_SUCCESS;
731 }
732
733 result
734 ByteBuffer::SetMchar(wchar_t value)
735 {
736         SysTryReturn(NID_BASE,
737                 GetRemaining() >= MCHAR_SIZE, E_OVERFLOW, E_OVERFLOW,
738                 "[%s] The remaining bytes of this buffer are smaller than size of wchar_t.", GetErrorMessage(E_OVERFLOW));
739
740         *reinterpret_cast <wchar_t*>(&__pArrayStart[_position]) = value;
741         _position += MCHAR_SIZE;
742
743         return E_SUCCESS;
744 }
745
746 result
747 ByteBuffer::SetWchar(wchar_t value)
748 {
749         SysTryReturn(NID_BASE,
750                 GetRemaining() >= MCHAR_SIZE, E_OVERFLOW, E_OVERFLOW,
751                 "[%s] The remaining bytes of this buffer are smaller than size of wchar_t.", GetErrorMessage(E_OVERFLOW));
752
753         *reinterpret_cast <wchar_t*>(&__pArrayStart[_position]) = value;
754         _position += MCHAR_SIZE;
755
756         return E_SUCCESS;
757 }
758
759 result
760 ByteBuffer::SetMchar(int index, wchar_t value)
761 {
762         SysTryReturn(NID_BASE,
763                 index <= (_limit - MCHAR_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
764                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of wchar_t(%d)'.",
765                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, MCHAR_SIZE);
766
767         *reinterpret_cast <wchar_t*>(&__pArrayStart[index]) = value;
768
769         return E_SUCCESS;
770 }
771
772 result
773 ByteBuffer::SetWchar(int index, wchar_t value)
774 {
775         SysTryReturn(NID_BASE,
776                 index <= (_limit - MCHAR_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
777                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of wchar_t(%d)'.",
778                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, MCHAR_SIZE);
779
780         *reinterpret_cast <wchar_t*>(&__pArrayStart[index]) = value;
781
782         return E_SUCCESS;
783 }
784
785 result
786 ByteBuffer::SetShort(short value)
787 {
788         SysTryReturn(NID_BASE,
789                 GetRemaining() >= SHORT_SIZE, E_OVERFLOW, E_OVERFLOW,
790                 "[%s] The remaining bytes of this buffer are smaller than size of short.", GetErrorMessage(E_OVERFLOW));
791
792         *reinterpret_cast <short*>(&__pArrayStart[_position]) = value;
793         _position += SHORT_SIZE;
794
795         return E_SUCCESS;
796 }
797
798 result
799 ByteBuffer::SetShort(int index, short value)
800 {
801         SysTryReturn(NID_BASE,
802                 index <= (_limit - SHORT_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
803                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of short(%d)'.",
804                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, SHORT_SIZE);
805
806         *reinterpret_cast <short*>(&__pArrayStart[index]) = value;
807
808         return E_SUCCESS;
809 }
810
811 ByteBuffer*
812 ByteBuffer::SliceN(void) const
813 {
814         std::unique_ptr<ByteBuffer> pBuffer(new (std::nothrow) ByteBuffer());
815         pBuffer->_pData = _pData;
816         AddRef();
817         pBuffer->_capacity = GetRemaining();
818         pBuffer->_limit = pBuffer->_capacity;
819         if (pBuffer->_capacity > 0)
820         {
821                 pBuffer->__pArrayStart = &__pArrayStart[_position];
822         }
823
824         return pBuffer.release();
825 }
826
827 const byte*
828 ByteBuffer::GetPointer(void) const
829 {
830         return __pArrayStart;
831 }
832
833 byte*
834 ByteBuffer::GetPointer(void)
835 {
836         return __pArrayStart;
837 }
838
839 bool
840 ByteBuffer::Equals(const Tizen::Base::Object& obj) const
841 {
842         bool out = false;
843         const ByteBuffer* pOther = dynamic_cast <const ByteBuffer*>(&obj);
844         if (pOther == null)
845         {
846                 out = false;
847         }
848         else if ((pOther == this) || (*pOther == *this))
849         {
850                 out = true;
851         }
852
853         return out;
854 }
855
856 int
857 ByteBuffer::GetHashCode(void) const
858 {
859         int len = GetRemaining() / sizeof(len);
860
861         int hash = 0;
862         int offset = _position;
863         for (int i = 0; i < len; ++i)
864         {
865                 hash = (hash << 5) - hash + static_cast <int>(__pArrayStart[offset + (i * sizeof(hash))]);
866         }
867
868         return hash;
869 }
870
871 int
872 ByteBuffer::GetTypeSize(void) const
873 {
874         return 1;
875 }
876
877 }} // Tizen::Base