1 /*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
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
16 #include "buffer_packer_stream.h"
17 #include "securec.h"
18
19 namespace OHOS {
20 namespace Media {
21 using namespace OHOS::HiviewDFX;
22
BufferPackerStream(uint8_t * outputData,uint32_t maxSize)23 BufferPackerStream::BufferPackerStream(uint8_t *outputData, uint32_t maxSize)
24 : outputData_(outputData), maxSize_(maxSize)
25 {}
26
Write(const uint8_t * buffer,uint32_t size)27 bool BufferPackerStream::Write(const uint8_t *buffer, uint32_t size)
28 {
29 if ((buffer == nullptr) || (size == 0)) {
30 HiLog::Error(LABEL, "input parameter invalid.");
31 return false;
32 }
33 if (outputData_ == nullptr) {
34 HiLog::Error(LABEL, "output stream is null.");
35 return false;
36 }
37 uint32_t leftSize = maxSize_ - offset_;
38 if (size > leftSize) {
39 HiLog::Error(LABEL, "write data:[%{public}lld] out of max size:[%{public}u].",
40 static_cast<long long>(size + offset_), maxSize_);
41 return false;
42 }
43 if (memcpy_s(outputData_ + offset_, leftSize, buffer, size) != EOK) {
44 HiLog::Error(LABEL, "memory copy failed.");
45 return false;
46 }
47 offset_ += size;
48 return true;
49 }
50
BytesWritten()51 int64_t BufferPackerStream::BytesWritten()
52 {
53 return offset_;
54 }
55 } // namespace Media
56 } // namespace OHOS
57