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 #ifndef FRAMEWORKS_INNERKITSIMPL_STREAM_INCLUDE_ISTREAM_SOURCE_STREAM_H
17 #define FRAMEWORKS_INNERKITSIMPL_STREAM_INCLUDE_ISTREAM_SOURCE_STREAM_H
18 
19 #include <cstdint>
20 #include <istream>
21 #include <memory>
22 #include "image/input_data_stream.h"
23 #include "source_stream.h"
24 
25 namespace OHOS {
26 namespace Media {
27 class IstreamSourceStream : public SourceStream {
28 public:
29     static std::unique_ptr<IstreamSourceStream> CreateSourceStream(std::unique_ptr<std::istream> inputStream);
30     IstreamSourceStream(std::unique_ptr<std::istream> inputStream, size_t size, size_t original, size_t offset);
31     ~IstreamSourceStream() override;
32     bool Read(uint32_t desiredSize, ImagePlugin::DataStreamBuffer &outData) override;
33     bool Read(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize, uint32_t &readSize) override;
34     bool Peek(uint32_t desiredSize, ImagePlugin::DataStreamBuffer &outData) override;
35     bool Peek(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize, uint32_t &readSize) override;
36     uint32_t Tell() override;
37     bool Seek(uint32_t position) override;
38     size_t GetStreamSize() override;
39     uint8_t *GetDataPtr() override;
40     uint32_t GetStreamType() override;
41 
42 private:
43     DISALLOW_COPY_AND_MOVE(IstreamSourceStream);
44     bool GetData(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize, uint32_t &readSize);
45     bool GetData(uint32_t desiredSize, ImagePlugin::DataStreamBuffer &outData);
46     void ResetReadBuffer();
47     std::unique_ptr<std::istream> inputStream_;
48     size_t streamSize_ = 0;
49     size_t streamOriginalOffset_ = 0;
50     size_t streamOffset_ = 0;
51     uint8_t *databuffer_ = nullptr;
52 };
53 } // namespace Media
54 } // namespace OHOS
55 
56 #endif // FRAMEWORKS_INNERKITSIMPL_STREAM_INCLUDE_ISTREAM_SOURCE_STREAM_H
57