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