xref: /aosp_15_r20/art/libelffile/stream/vector_output_stream.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2013 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_LIBELFFILE_STREAM_VECTOR_OUTPUT_STREAM_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_LIBELFFILE_STREAM_VECTOR_OUTPUT_STREAM_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include "output_stream.h"
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include <string.h>
23*795d594fSAndroid Build Coastguard Worker #include <string>
24*795d594fSAndroid Build Coastguard Worker #include <vector>
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker namespace art {
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker class VectorOutputStream final : public OutputStream {
29*795d594fSAndroid Build Coastguard Worker  public:
30*795d594fSAndroid Build Coastguard Worker   VectorOutputStream(const std::string& location, std::vector<uint8_t>* vector);
31*795d594fSAndroid Build Coastguard Worker 
~VectorOutputStream()32*795d594fSAndroid Build Coastguard Worker   ~VectorOutputStream() override {}
33*795d594fSAndroid Build Coastguard Worker 
WriteFully(const void * buffer,size_t byte_count)34*795d594fSAndroid Build Coastguard Worker   bool WriteFully(const void* buffer, size_t byte_count) override {
35*795d594fSAndroid Build Coastguard Worker     if (static_cast<size_t>(offset_) == vector_->size()) {
36*795d594fSAndroid Build Coastguard Worker       const uint8_t* start = reinterpret_cast<const uint8_t*>(buffer);
37*795d594fSAndroid Build Coastguard Worker       vector_->insert(vector_->end(), &start[0], &start[byte_count]);
38*795d594fSAndroid Build Coastguard Worker       offset_ += byte_count;
39*795d594fSAndroid Build Coastguard Worker     } else {
40*795d594fSAndroid Build Coastguard Worker       off_t new_offset = offset_ + byte_count;
41*795d594fSAndroid Build Coastguard Worker       EnsureCapacity(new_offset);
42*795d594fSAndroid Build Coastguard Worker       memcpy(&(*vector_)[offset_], buffer, byte_count);
43*795d594fSAndroid Build Coastguard Worker       offset_ = new_offset;
44*795d594fSAndroid Build Coastguard Worker     }
45*795d594fSAndroid Build Coastguard Worker     return true;
46*795d594fSAndroid Build Coastguard Worker   }
47*795d594fSAndroid Build Coastguard Worker 
48*795d594fSAndroid Build Coastguard Worker   off_t Seek(off_t offset, Whence whence) override;
49*795d594fSAndroid Build Coastguard Worker 
Flush()50*795d594fSAndroid Build Coastguard Worker   bool Flush() override {
51*795d594fSAndroid Build Coastguard Worker     return true;
52*795d594fSAndroid Build Coastguard Worker   }
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker  private:
EnsureCapacity(off_t new_offset)55*795d594fSAndroid Build Coastguard Worker   void EnsureCapacity(off_t new_offset) {
56*795d594fSAndroid Build Coastguard Worker     if (new_offset > static_cast<off_t>(vector_->size())) {
57*795d594fSAndroid Build Coastguard Worker       vector_->resize(new_offset);
58*795d594fSAndroid Build Coastguard Worker     }
59*795d594fSAndroid Build Coastguard Worker   }
60*795d594fSAndroid Build Coastguard Worker 
61*795d594fSAndroid Build Coastguard Worker   off_t offset_;
62*795d594fSAndroid Build Coastguard Worker   std::vector<uint8_t>* const vector_;
63*795d594fSAndroid Build Coastguard Worker 
64*795d594fSAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(VectorOutputStream);
65*795d594fSAndroid Build Coastguard Worker };
66*795d594fSAndroid Build Coastguard Worker 
67*795d594fSAndroid Build Coastguard Worker }  // namespace art
68*795d594fSAndroid Build Coastguard Worker 
69*795d594fSAndroid Build Coastguard Worker #endif  // ART_LIBELFFILE_STREAM_VECTOR_OUTPUT_STREAM_H_
70