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/port/memory_output_stream.h"
18 
19 namespace sfntly {
20 
MemoryOutputStream()21 MemoryOutputStream::MemoryOutputStream() {
22 }
23 
~MemoryOutputStream()24 MemoryOutputStream::~MemoryOutputStream() {
25 }
26 
Write(std::vector<uint8_t> * buffer)27 void MemoryOutputStream::Write(std::vector<uint8_t>* buffer) {
28   store_.insert(store_.end(), buffer->begin(), buffer->end());
29 }
30 
Write(std::vector<uint8_t> * buffer,int32_t offset,int32_t length)31 void MemoryOutputStream::Write(std::vector<uint8_t>* buffer,
32                                int32_t offset,
33                                int32_t length) {
34   assert(buffer);
35   if (offset >= 0 && length > 0) {
36     store_.insert(store_.end(),
37                   buffer->begin() + offset,
38                   buffer->begin() + offset + length);
39   } else {
40 #if !defined(SFNTLY_NO_EXCEPTION)
41     throw IndexOutOfBoundException();
42 #endif
43   }
44 }
45 
Write(uint8_t * buffer,int32_t offset,int32_t length)46 void MemoryOutputStream::Write(uint8_t* buffer, int32_t offset, int32_t length) {
47   assert(buffer);
48   if (offset >= 0 && length > 0) {
49     store_.insert(store_.end(), buffer + offset, buffer + offset + length);
50   } else {
51 #if !defined(SFNTLY_NO_EXCEPTION)
52     throw IndexOutOfBoundException();
53 #endif
54   }
55 }
56 
Write(uint8_t b)57 void MemoryOutputStream::Write(uint8_t b) {
58   store_.push_back(b);
59 }
60 
Get()61 uint8_t* MemoryOutputStream::Get() {
62   if (store_.empty()) {
63     return NULL;
64   }
65   return &(store_[0]);
66 }
67 
Size()68 size_t MemoryOutputStream::Size() {
69   return store_.size();
70 }
71 
72 }  // namespace sfntly
73