1 // Copyright 2015 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 "aemu/base/utils/stream.h"
16 
17 #include "aemu/base/files/Stream.h"
18 
19 #include <stdlib.h>
20 #include <string.h>
21 
22 typedef android::base::Stream BaseStream;
23 
asBaseStream(Stream * stream)24 static BaseStream* asBaseStream(Stream* stream) {
25     return reinterpret_cast<BaseStream*>(stream);
26 }
27 
stream_read(Stream * stream,void * buffer,size_t len)28 ssize_t stream_read(Stream* stream, void* buffer, size_t len) {
29     return asBaseStream(stream)->read(buffer, len);
30 }
31 
stream_write(Stream * stream,const void * buffer,size_t len)32 ssize_t stream_write(Stream* stream, const void* buffer, size_t len) {
33     return asBaseStream(stream)->write(buffer, len);
34 }
35 
stream_put_byte(Stream * stream,int v)36 void stream_put_byte(Stream* stream, int v) {
37     asBaseStream(stream)->putByte(static_cast<uint8_t>(v));
38 }
39 
stream_put_be16(Stream * stream,uint16_t v)40 void stream_put_be16(Stream* stream, uint16_t v) {
41     asBaseStream(stream)->putBe16(v);
42 }
43 
stream_put_be32(Stream * stream,uint32_t v)44 void stream_put_be32(Stream* stream, uint32_t v) {
45     asBaseStream(stream)->putBe32(v);
46 }
47 
stream_put_be64(Stream * stream,uint64_t v)48 void stream_put_be64(Stream* stream, uint64_t v) {
49     asBaseStream(stream)->putBe64(v);
50 }
51 
stream_get_byte(Stream * stream)52 uint8_t stream_get_byte(Stream* stream) {
53     return asBaseStream(stream)->getByte();
54 }
55 
stream_get_be16(Stream * stream)56 uint16_t stream_get_be16(Stream* stream) {
57     return asBaseStream(stream)->getBe16();
58 }
59 
stream_get_be32(Stream * stream)60 uint32_t stream_get_be32(Stream* stream) {
61     return asBaseStream(stream)->getBe32();
62 }
63 
stream_get_be64(Stream * stream)64 uint64_t stream_get_be64(Stream* stream) {
65     return asBaseStream(stream)->getBe64();
66 }
67 
stream_put_float(Stream * stream,float v)68 void stream_put_float(Stream* stream, float v) {
69     asBaseStream(stream)->putFloat(v);
70 }
71 
stream_get_float(Stream * stream)72 float stream_get_float(Stream* stream) {
73     return asBaseStream(stream)->getFloat();
74 }
75 
stream_put_string(Stream * stream,const char * str)76 void stream_put_string(Stream* stream, const char* str) {
77     asBaseStream(stream)->putString(str);
78 }
79 
stream_get_string(Stream * stream)80 char* stream_get_string(Stream* stream) {
81     std::string ret = asBaseStream(stream)->getString();
82     if (ret.empty()) {
83         return NULL;
84     }
85     char* result = static_cast<char*>(::malloc(ret.size() + 1U));
86     ::memcpy(result, ret.c_str(), ret.size() + 1U);
87     return result;
88 }
89 
stream_free(Stream * stream)90 void stream_free(Stream* stream) {
91     delete asBaseStream(stream);
92 }
93