/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "circle_stream_buffer.h"

namespace OHOS {
namespace Sensors {
bool CircleStreamBuffer::CheckWrite(size_t size)
{
    size_t availSize = GetAvailableBufSize();
    if (size > availSize && rPos_ > 0) {
        CopyDataToBegin();
        availSize = GetAvailableBufSize();
    }
    return (availSize >= size);
}

bool CircleStreamBuffer::Write(const char *buf, size_t size)
{
    if (!CheckWrite(size)) {
        SEN_HILOGE("Buffer is overflow, availableSize:%{public}zu, size:%{public}zu,"
                   "unreadSize:%{public}zu, rPos:%{public}zu, wPos:%{public}zu",
                   GetAvailableBufSize(), size, UnreadSize(), rPos_, wPos_);
        return false;
    }
    return StreamBuffer::Write(buf, size);
}

void CircleStreamBuffer::CopyDataToBegin()
{
    size_t unreadSize = UnreadSize();
    if (unreadSize > 0 && rPos_ > 0) {
        size_t pos = 0;
        for (size_t i = rPos_; i <= wPos_;) {
            szBuff_[pos++] = szBuff_[i++];
        }
    }
    SEN_HILOGD("UnreadSize:%{public}zu, rPos:%{public}zu, wPos:%{public}zu", unreadSize, rPos_, wPos_);
    rPos_ = 0;
    wPos_ = unreadSize;
}
} // namespace Sensors
} // namespace OHOS