1464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com/*
2464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * Copyright 2011 Google Inc. All Rights Reserved.
3464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com *
4464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * Licensed under the Apache License, Version 2.0 (the "License");
5464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * you may not use this file except in compliance with the License.
6464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * You may obtain a copy of the License at
7464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com *
8464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com *      http://www.apache.org/licenses/LICENSE-2.0
9464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com *
10464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * Unless required by applicable law or agreed to in writing, software
11464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * distributed under the License is distributed on an "AS IS" BASIS,
12464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * See the License for the specific language governing permissions and
14464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com * limitations under the License.
15464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com */
16464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
175af34fd773f8cfee82321393504f558ddf67c628arthurhsu@google.com#ifndef SFNTLY_CPP_SRC_SFNTLY_DATA_MEMORY_BYTE_ARRAY_H_
185af34fd773f8cfee82321393504f558ddf67c628arthurhsu@google.com#define SFNTLY_CPP_SRC_SFNTLY_DATA_MEMORY_BYTE_ARRAY_H_
19464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
20464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include "sfntly/data/byte_array.h"
21464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
22464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comnamespace sfntly {
23464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
24464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comclass MemoryByteArray : public ByteArray, public RefCounted<MemoryByteArray> {
25464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com public:
2632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Construct a new MemoryByteArray with a new array of the size given. It is
2732a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // assumed that none of the array is filled and readable.
28464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  explicit MemoryByteArray(int32_t length);
2932a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
3032a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Note: not implemented due to dangerous operations in constructor.
3132a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  //explicit MemoryByteArray(ByteVector* b);
3232a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
3332a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Construct a new MemoryByteArray using byte array.
3432a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param b the byte array that provides the actual storage
3532a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // @param filled_length the index of the last byte in the array has data
3632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Note: This is different from Java version, it does not take over the
3732a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  //       ownership of b.  Caller is responsible for handling the lifetime
3832a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  //       of b.  C++ port also assumes filled_length is buffer_length since
3932a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  //       there is not a reliable way to identify the actual size of buffer.
4032a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  MemoryByteArray(byte_t* b, int32_t filled_length);
4132a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
42464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  virtual ~MemoryByteArray();
4332a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  virtual int32_t CopyTo(OutputStream* os, int32_t offset, int32_t length);
4432a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com
4532a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  // Make gcc -Woverloaded-virtual happy.
4632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  virtual int32_t CopyTo(ByteArray* array) { return ByteArray::CopyTo(array); }
4732a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  virtual int32_t CopyTo(ByteArray* array, int32_t offset, int32_t length) {
4832a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com    return ByteArray::CopyTo(array, offset, length);
4932a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  }
5032a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  virtual int32_t CopyTo(int32_t dst_offset,
5132a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com                         ByteArray* array,
5232a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com                         int32_t src_offset,
5332a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com                         int32_t length) {
5432a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com    return ByteArray::CopyTo(dst_offset, array, src_offset, length);
5532a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  }
5632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  virtual int32_t CopyTo(OutputStream* os) { return ByteArray::CopyTo(os); }
57464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
58464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com protected:
5932a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com  virtual void InternalPut(int32_t index, byte_t b);
60246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual int32_t InternalPut(int32_t index,
6132a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com                              byte_t* b,
62246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com                              int32_t offset,
63464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com                              int32_t length);
64246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual byte_t InternalGet(int32_t index);
65246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual int32_t InternalGet(int32_t index,
6632a01c7c6e7be46dda9bfc78de9ce32d99e4c8b7arthurhsu@google.com                              byte_t* b,
67246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com                              int32_t offset,
68464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com                              int32_t length);
69246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual void Close();
70246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  virtual byte_t* Begin();
71464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
72464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com private:
73246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void Init();  // C++ port only, used to allocate memory outside constructor.
74464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
75464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  byte_t* b_;
76464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  bool allocated_;
77464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com};
78464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
79464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com}  // namespace sfntly
80464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
815af34fd773f8cfee82321393504f558ddf67c628arthurhsu@google.com#endif  // SFNTLY_CPP_SRC_SFNTLY_DATA_MEMORY_BYTE_ARRAY_H_
82