1 /*
2 * Copyright (C) 2021 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 "bmp_stream.h"
17
18 namespace OHOS {
19 namespace ImagePlugin {
20 using namespace OHOS::HiviewDFX;
21 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_TAG_DOMAIN_ID_PLUGIN, "BmpStream" };
BmpStream(InputDataStream * stream)22 BmpStream::BmpStream(InputDataStream *stream) : inputStream_(stream) {}
23
read(void * buffer,size_t size)24 size_t BmpStream::read(void *buffer, size_t size)
25 {
26 if (inputStream_ == nullptr) {
27 HiLog::Error(LABEL, "read failed, inputStream_ is null");
28 return 0;
29 }
30 if (buffer == nullptr) {
31 size_t curPosition = static_cast<size_t>(inputStream_->Tell());
32 if (!inputStream_->Seek(curPosition + size)) {
33 HiLog::Error(LABEL, "read failed, curpositon=%{public}zu, skip size=%{public}zu", curPosition, size);
34 return 0;
35 }
36 return size;
37 }
38 uint32_t desireSize = static_cast<uint32_t>(size);
39 uint32_t bufferSize = desireSize;
40 uint32_t readSize = desireSize;
41 if (!inputStream_->Read(desireSize, static_cast<uint8_t *>(buffer), bufferSize, readSize)) {
42 HiLog::Error(LABEL, "read failed, desire read size=%{public}u", desireSize);
43 return 0;
44 }
45 return readSize;
46 }
47
peek(void * buffer,size_t size) const48 size_t BmpStream::peek(void *buffer, size_t size) const
49 {
50 if (inputStream_ == nullptr) {
51 HiLog::Error(LABEL, "peek failed, inputStream_ is null");
52 return 0;
53 }
54 if (buffer == nullptr) {
55 HiLog::Error(LABEL, "peek failed, output buffer is null");
56 return 0;
57 }
58 uint32_t desireSize = static_cast<uint32_t>(size);
59 uint32_t bufferSize = desireSize;
60 uint32_t readSize = desireSize;
61 if (!inputStream_->Peek(desireSize, static_cast<uint8_t *>(buffer), bufferSize, readSize)) {
62 HiLog::Error(LABEL, "peek failed, desire peek size=%{public}u", desireSize);
63 return 0;
64 }
65 return readSize;
66 }
67
isAtEnd() const68 bool BmpStream::isAtEnd() const
69 {
70 if (inputStream_ == nullptr) {
71 HiLog::Error(LABEL, "get stream status failed, inputStream_ is null.");
72 return false;
73 }
74 size_t size = inputStream_->GetStreamSize();
75 return (inputStream_->Tell() == size);
76 }
77 } // namespace ImagePlugin
78 } // namespace OHOS
79