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 #include "raw_stream.h"
16 #include "hilog/log.h"
17 #include "log_tags.h"
18 namespace OHOS {
19 namespace ImagePlugin {
20 using namespace OHOS::HiviewDFX;
21 namespace {
22 constexpr HiLogLabel LABEL = { LOG_CORE, LOG_TAG_DOMAIN_ID_PLUGIN, "RawStream" };
23 }
24
RawStream(InputDataStream & sourceStream)25 RawStream::RawStream(InputDataStream &sourceStream)
26 {
27 HiLog::Debug(LABEL, "create IN");
28
29 inputStream_ = &sourceStream;
30
31 HiLog::Debug(LABEL, "create OUT");
32 }
33
~RawStream()34 RawStream::~RawStream()
35 {
36 HiLog::Debug(LABEL, "release IN");
37
38 inputStream_ = nullptr;
39
40 HiLog::Debug(LABEL, "release OUT");
41 }
42
43 // api for piex::StreamInterface
GetData(const size_t offset,const size_t length,uint8_t * data)44 piex::Error RawStream::GetData(const size_t offset, const size_t length, uint8_t* data)
45 {
46 if (inputStream_ == nullptr) {
47 HiLog::Error(LABEL, "GetData, InputStream is null");
48 return piex::kUnsupported;
49 }
50
51 uint32_t u32Offset = static_cast<uint32_t>(offset);
52 uint32_t u32Length = static_cast<uint32_t>(length);
53
54 if (inputStream_->Tell() != u32Offset) {
55 if (!inputStream_->Seek(u32Offset)) {
56 HiLog::Error(LABEL, "GetData, seek fail");
57 return piex::kFail;
58 }
59
60 if (inputStream_->Tell() != u32Offset) {
61 HiLog::Error(LABEL, "GetData, seeked fail");
62 return piex::kFail;
63 }
64 }
65
66 uint32_t readSize = 0;
67 if (!inputStream_->Read(u32Length, data, u32Length, readSize)) {
68 HiLog::Error(LABEL, "GetData, read fail");
69 return piex::kFail;
70 }
71
72 if (readSize != u32Length) {
73 HiLog::Error(LABEL, "GetData, read want:%{public}u, real:%{public}u", u32Length, readSize);
74 return piex::kFail;
75 }
76
77 return piex::kOk;
78 }
79 } // namespace ImagePlugin
80 } // namespace OHOS
81