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 #ifndef SFNTLY_CPP_SRC_SFNTLY_DATA_MEMORY_BYTE_ARRAY_H_
18 #define SFNTLY_CPP_SRC_SFNTLY_DATA_MEMORY_BYTE_ARRAY_H_
19 
20 #include "sfntly/data/byte_array.h"
21 
22 namespace sfntly {
23 
24 class MemoryByteArray : public ByteArray, public RefCounted<MemoryByteArray> {
25  public:
26   // Construct a new MemoryByteArray with a new array of the size given. It is
27   // assumed that none of the array is filled and readable.
28   explicit MemoryByteArray(int32_t length);
29 
30   // Note: not implemented due to dangerous operations in constructor.
31   //explicit MemoryByteArray(std::vector<uint8_t>* b);
32 
33   // Construct a new MemoryByteArray using byte array.
34   // @param b the byte array that provides the actual storage
35   // @param filled_length the index of the last byte in the array has data
36   // Note: This is different from Java version, it does not take over the
37   //       ownership of b.  Caller is responsible for handling the lifetime
38   //       of b.  C++ port also assumes filled_length is buffer_length since
39   //       there is not a reliable way to identify the actual size of buffer.
40   MemoryByteArray(uint8_t* b, int32_t filled_length);
41 
42   virtual ~MemoryByteArray();
43   virtual int32_t CopyTo(OutputStream* os, int32_t offset, int32_t length);
44 
45   // Make gcc -Woverloaded-virtual happy.
CopyTo(ByteArray * array)46   virtual int32_t CopyTo(ByteArray* array) { return ByteArray::CopyTo(array); }
CopyTo(ByteArray * array,int32_t offset,int32_t length)47   virtual int32_t CopyTo(ByteArray* array, int32_t offset, int32_t length) {
48     return ByteArray::CopyTo(array, offset, length);
49   }
CopyTo(int32_t dst_offset,ByteArray * array,int32_t src_offset,int32_t length)50   virtual int32_t CopyTo(int32_t dst_offset,
51                          ByteArray* array,
52                          int32_t src_offset,
53                          int32_t length) {
54     return ByteArray::CopyTo(dst_offset, array, src_offset, length);
55   }
CopyTo(OutputStream * os)56   virtual int32_t CopyTo(OutputStream* os) { return ByteArray::CopyTo(os); }
57 
58  protected:
59   virtual void InternalPut(int32_t index, uint8_t b);
60   virtual int32_t InternalPut(int32_t index,
61                               uint8_t* b,
62                               int32_t offset,
63                               int32_t length);
64   virtual uint8_t InternalGet(int32_t index);
65   virtual int32_t InternalGet(int32_t index,
66                               uint8_t* b,
67                               int32_t offset,
68                               int32_t length);
69   virtual void Close();
70   virtual uint8_t* Begin();
71 
72  private:
73   void Init();  // C++ port only, used to allocate memory outside constructor.
74 
75   uint8_t* b_;
76   bool allocated_;
77 };
78 
79 }  // namespace sfntly
80 
81 #endif  // SFNTLY_CPP_SRC_SFNTLY_DATA_MEMORY_BYTE_ARRAY_H_
82