vector_output_stream.cc revision 49a0f158ed11974fa2cc12014c9f55a31dabd8df
1cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom/* 2cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * Copyright (C) 2013 The Android Open Source Project 3cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * 4cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * Licensed under the Apache License, Version 2.0 (the "License"); 5cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * you may not use this file except in compliance with the License. 6cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * You may obtain a copy of the License at 7cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * 8cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * http://www.apache.org/licenses/LICENSE-2.0 9cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * 10cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * Unless required by applicable law or agreed to in writing, software 11cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * distributed under the License is distributed on an "AS IS" BASIS, 12cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * See the License for the specific language governing permissions and 14cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * limitations under the License. 15cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom */ 16cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom 17cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom#include "vector_output_stream.h" 18cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom 19cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom#include <string.h> 20cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom 21cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom#include "base/logging.h" 22cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom 23cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstromnamespace art { 24cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom 25cd60ac736bc7104785dc67671660d70fb434466fBrian CarlstromVectorOutputStream::VectorOutputStream(const std::string& location, std::vector<uint8_t>& vector) 2649a0f158ed11974fa2cc12014c9f55a31dabd8dfBrian Carlstrom : OutputStream(location), offset_(vector.size()), vector_(vector) {} 27cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom 28cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrombool VectorOutputStream::WriteFully(const void* buffer, int64_t byte_count) { 29cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom off_t new_offset = offset_ + byte_count; 30cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom EnsureCapacity(new_offset); 31cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom memcpy(&vector_[offset_], buffer, byte_count); 32cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom offset_ = new_offset; 33cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom return true; 34cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom} 35cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom 3649a0f158ed11974fa2cc12014c9f55a31dabd8dfBrian Carlstromoff_t VectorOutputStream::Seek(off_t offset, Whence whence) { 3749a0f158ed11974fa2cc12014c9f55a31dabd8dfBrian Carlstrom CHECK(whence == kSeekSet || whence == kSeekCurrent || whence == kSeekEnd) << whence; 3849a0f158ed11974fa2cc12014c9f55a31dabd8dfBrian Carlstrom off_t new_offset = 0; 39cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom switch (whence) { 4049a0f158ed11974fa2cc12014c9f55a31dabd8dfBrian Carlstrom case kSeekSet: { 41cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom new_offset = offset; 42cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom break; 43cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom } 4449a0f158ed11974fa2cc12014c9f55a31dabd8dfBrian Carlstrom case kSeekCurrent: { 45cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom new_offset = offset_ + offset; 46cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom break; 47cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom } 4849a0f158ed11974fa2cc12014c9f55a31dabd8dfBrian Carlstrom case kSeekEnd: { 49cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom new_offset = vector_.size() + offset; 50cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom break; 51cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom } 52cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom } 53cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom EnsureCapacity(new_offset); 54cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom offset_ = new_offset; 55cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom return offset_; 56cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom} 57cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom 58cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstromvoid VectorOutputStream::EnsureCapacity(off_t new_offset) { 59cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom if (new_offset > static_cast<off_t>(vector_.size())) { 60cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom vector_.resize(new_offset); 61cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom } 62cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom} 63cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom 64cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom} // namespace art 65