1 // Copyright 2017 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "android/base/files/StreamSerializing.h"
16 
17 namespace android {
18 namespace base {
19 
saveStream(Stream * stream,const MemStream & memStream)20 void saveStream(Stream* stream, const MemStream& memStream) {
21     memStream.save(stream);
22 }
23 
loadStream(Stream * stream,MemStream * memStream)24 void loadStream(Stream* stream, MemStream* memStream) {
25     memStream->load(stream);
26 }
27 
saveBufferRaw(Stream * stream,char * buffer,uint32_t len)28 void saveBufferRaw(Stream* stream, char* buffer, uint32_t len) {
29     stream->putBe32(len);
30     stream->write(buffer, len);
31 }
32 
loadBufferRaw(Stream * stream,char * buffer)33 bool loadBufferRaw(Stream* stream, char* buffer) {
34     auto len = stream->getBe32();
35     int ret = (int)stream->read(buffer, len);
36     return ret == (int)len;
37 }
38 
saveStringArray(Stream * stream,const char * const * strings,uint32_t count)39 void saveStringArray(Stream* stream, const char* const* strings, uint32_t count) {
40     stream->putBe32(count);
41     for (uint32_t i = 0; i < count; ++i) {
42         stream->putString(strings[i]);
43     }
44 }
45 
loadStringArray(Stream * stream)46 std::vector<std::string> loadStringArray(Stream* stream) {
47     uint32_t count = stream->getBe32();
48     std::vector<std::string> res;
49     for (uint32_t i = 0; i < count; ++i) {
50         res.push_back(stream->getString());
51     }
52     return res;
53 }
54 
55 }  // namespace base
56 }  // namespace android
57