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