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