• 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/memory_byte_array.h"
18 
19 #include <string.h>
20 
21 namespace sfntly {
22 
MemoryByteArray(int32_t length)23 MemoryByteArray::MemoryByteArray(int32_t length)
24     : ByteArray(0, length), b_(NULL), allocated_(true) {
25 }
26 
MemoryByteArray(byte_t * b,int32_t filled_length)27 MemoryByteArray::MemoryByteArray(byte_t* b, int32_t filled_length)
28     : ByteArray(filled_length, filled_length), b_(b), allocated_(false) {
29   assert(b);
30 }
31 
~MemoryByteArray()32 MemoryByteArray::~MemoryByteArray() {
33   Close();
34 }
35 
CopyTo(OutputStream * os,int32_t offset,int32_t length)36 int32_t MemoryByteArray::CopyTo(OutputStream* os,
37                                 int32_t offset,
38                                 int32_t length) {
39   assert(os);
40   os->Write(b_, offset, length);
41   return length;
42 }
43 
Init()44 void MemoryByteArray::Init() {
45   if (allocated_ && b_ == NULL) {
46     b_ = new byte_t[Size()];
47     memset(b_, 0, Size());
48   }
49 }
50 
InternalPut(int32_t index,byte_t b)51 void MemoryByteArray::InternalPut(int32_t index, byte_t b) {
52   Init();
53   b_[index] = b;
54 }
55 
InternalPut(int32_t index,byte_t * b,int32_t offset,int32_t length)56 int32_t MemoryByteArray::InternalPut(int32_t index,
57                                      byte_t* b,
58                                      int32_t offset,
59                                      int32_t length) {
60   assert(b);
61   Init();
62   memcpy(b_ + index, b + offset, length);
63   return length;
64 }
65 
InternalGet(int32_t index)66 byte_t MemoryByteArray::InternalGet(int32_t index) {
67   Init();
68   return b_[index];
69 }
70 
InternalGet(int32_t index,byte_t * b,int32_t offset,int32_t length)71 int32_t MemoryByteArray::InternalGet(int32_t index,
72                                      byte_t* b,
73                                      int32_t offset,
74                                      int32_t length) {
75   assert(b);
76   Init();
77   memcpy(b + offset, b_ + index, length);
78   return length;
79 }
80 
Close()81 void MemoryByteArray::Close() {
82   if (allocated_ && b_) {
83     delete[] b_;
84   }
85   b_ = NULL;
86 }
87 
Begin()88 byte_t* MemoryByteArray::Begin() {
89   Init();
90   return b_;
91 }
92 
93 }  // namespace sfntly
94