• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 Google Inc. All Rights Reserved.
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 #include "sfntly/data/growable_memory_byte_array.h"
18 
19 #include <limits.h>
20 #include <string.h>
21 
22 #include <algorithm>
23 
24 namespace sfntly {
25 
GrowableMemoryByteArray()26 GrowableMemoryByteArray::GrowableMemoryByteArray()
27     : ByteArray(0, INT_MAX, true) {
28   // Note: We did not set an initial size of array like Java because STL
29   //       implementation will determine the best strategy.
30 }
31 
~GrowableMemoryByteArray()32 GrowableMemoryByteArray::~GrowableMemoryByteArray() {}
33 
CopyTo(OutputStream * os,int32_t offset,int32_t length)34 int32_t GrowableMemoryByteArray::CopyTo(OutputStream* os,
35                                         int32_t offset,
36                                         int32_t length) {
37   assert(os);
38   os->Write(&b_, offset, length);
39   return length;
40 }
41 
InternalPut(int32_t index,byte_t b)42 void GrowableMemoryByteArray::InternalPut(int32_t index, byte_t b) {
43   if ((size_t)index >= b_.size()) {
44     b_.resize((size_t)(index + 1));
45   }
46   b_[index] = b;
47 }
48 
InternalPut(int32_t index,byte_t * b,int32_t offset,int32_t length)49 int32_t GrowableMemoryByteArray::InternalPut(int32_t index,
50                                              byte_t* b,
51                                              int32_t offset,
52                                              int32_t length) {
53   if ((size_t)index + length >= b_.size()) {
54     // Note: We grow one byte more than Java version. VC debuggers shows
55     //       data better this way.
56     b_.resize((size_t)(index + length + 1));
57   }
58   std::copy(b + offset, b + offset + length, b_.begin() + index);
59   return length;
60 }
61 
InternalGet(int32_t index)62 byte_t GrowableMemoryByteArray::InternalGet(int32_t index) {
63   return b_[index];
64 }
65 
InternalGet(int32_t index,byte_t * b,int32_t offset,int32_t length)66 int32_t GrowableMemoryByteArray::InternalGet(int32_t index,
67                                              byte_t* b,
68                                              int32_t offset,
69                                              int32_t length) {
70   memcpy(b + offset, &(b_[0]) + index, length);
71   return length;
72 }
73 
Close()74 void GrowableMemoryByteArray::Close() {
75   b_.clear();
76 }
77 
Begin()78 byte_t* GrowableMemoryByteArray::Begin() {
79   return &(b_[0]);
80 }
81 
82 }  // namespace sfntly
83