1f857971dSopenharmony_ci/*
2f857971dSopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3f857971dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f857971dSopenharmony_ci * you may not use this file except in compliance with the License.
5f857971dSopenharmony_ci * You may obtain a copy of the License at
6f857971dSopenharmony_ci *
7f857971dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f857971dSopenharmony_ci *
9f857971dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f857971dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f857971dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f857971dSopenharmony_ci * See the License for the specific language governing permissions and
13f857971dSopenharmony_ci * limitations under the License.
14f857971dSopenharmony_ci */
15f857971dSopenharmony_ci
16f857971dSopenharmony_ci#include "circle_stream_buffer.h"
17f857971dSopenharmony_ci
18f857971dSopenharmony_cinamespace OHOS {
19f857971dSopenharmony_cinamespace Msdp {
20f857971dSopenharmony_civoid CircleStreamBuffer::CopyDataToBegin()
21f857971dSopenharmony_ci{
22f857971dSopenharmony_ci    int32_t residualSize = ResidualSize();
23f857971dSopenharmony_ci    if (residualSize > 0 && rPos_ > 0) {
24f857971dSopenharmony_ci        int32_t pos = 0;
25f857971dSopenharmony_ci        for (int32_t i = rPos_; i <= wPos_;) {
26f857971dSopenharmony_ci            szBuff_[pos++] = szBuff_[i++];
27f857971dSopenharmony_ci        }
28f857971dSopenharmony_ci    }
29f857971dSopenharmony_ci    FI_HILOGD("ResidualSize:%{public}d rPos:%{public}d wPos:%{public}d", residualSize, rPos_, wPos_);
30f857971dSopenharmony_ci    rPos_ = 0;
31f857971dSopenharmony_ci    wPos_ = residualSize;
32f857971dSopenharmony_ci}
33f857971dSopenharmony_ci
34f857971dSopenharmony_cibool CircleStreamBuffer::CheckWrite(size_t size)
35f857971dSopenharmony_ci{
36f857971dSopenharmony_ci    int32_t bufferSize = static_cast<int32_t>(size);
37f857971dSopenharmony_ci    int32_t availableSize = GetAvailableBufSize();
38f857971dSopenharmony_ci    if (bufferSize > availableSize && rPos_ > 0) {
39f857971dSopenharmony_ci        CopyDataToBegin();
40f857971dSopenharmony_ci        availableSize = GetAvailableBufSize();
41f857971dSopenharmony_ci    }
42f857971dSopenharmony_ci    return (availableSize >= bufferSize);
43f857971dSopenharmony_ci}
44f857971dSopenharmony_ci
45f857971dSopenharmony_cibool CircleStreamBuffer::Write(const char *buf, size_t size)
46f857971dSopenharmony_ci{
47f857971dSopenharmony_ci    if (!CheckWrite(size)) {
48f857971dSopenharmony_ci        FI_HILOGE("Out of buffer memory, availableSize:%{public}d, size:%{public}zu,"
49f857971dSopenharmony_ci            "residualSize:%{public}d, rPos:%{public}d, wPos:%{public}d",
50f857971dSopenharmony_ci            GetAvailableBufSize(), size, ResidualSize(), rPos_, wPos_);
51f857971dSopenharmony_ci        return false;
52f857971dSopenharmony_ci    }
53f857971dSopenharmony_ci    return StreamBuffer::Write(buf, size);
54f857971dSopenharmony_ci}
55f857971dSopenharmony_ci} // namespace Msdp
56f857971dSopenharmony_ci} // namespace OHOS