• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_FILE_SOURCE_STREAM_H
17 #define FRAMEWORKS_INNERKITSIMPL_STREAM_INCLUDE_FILE_SOURCE_STREAM_H
18 
19 #include <cstdio>
20 #include <memory>
21 #include <string>
22 #include "image/input_data_stream.h"
23 #include "source_stream.h"
24 
25 namespace OHOS {
26 namespace Media {
27 class FileSourceStream : public SourceStream {
28 public:
29     static std::unique_ptr<FileSourceStream> CreateSourceStream(const std::string &pathName);
30     static std::unique_ptr<FileSourceStream> CreateSourceStream(const int fd);
31     static std::unique_ptr<FileSourceStream> CreateSourceStream(
32         const int fd, int32_t offset, int32_t size);
33     static bool ShouldUseMmap(int fd);
34     FileSourceStream(std::FILE *file, size_t size, size_t offset, size_t original,
35                      bool useMmap = false, int originalFd = SOURCE_STREAM_INVALID_FD);
36     FileSourceStream(std::FILE *file, size_t size, size_t offset, size_t original,
37                      bool useMmap = false, const std::string &originalPath = SOURCE_STREAM_INVALID_PATH);
38     ~FileSourceStream() override;
39 
40     bool Read(uint32_t desiredSize, ImagePlugin::DataStreamBuffer &outData) override;
41     bool Read(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize, uint32_t &readSize) override;
42     bool Peek(uint32_t desiredSize, ImagePlugin::DataStreamBuffer &outData) override;
43     bool Peek(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize, uint32_t &readSize) override;
44     uint32_t Tell() override;
45     bool Seek(uint32_t position) override;
46     size_t GetStreamSize() override;
47     uint8_t *GetDataPtr() override;
48     uint8_t *GetDataPtr(bool populate) override;
49     uint32_t GetStreamType() override;
50     int GetMMapFd();
51 
52 private:
53     DISALLOW_COPY_AND_MOVE(FileSourceStream);
54     bool GetData(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize, uint32_t &readSize);
55     bool GetData(uint32_t desiredSize, ImagePlugin::DataStreamBuffer &outData);
56     void ResetReadBuffer();
57     std::FILE *filePtr_ = nullptr;
58     size_t fileSize_ = 0;
59     size_t fileOffset_ = 0;
60     size_t fileOriginalOffset_ = 0;
61     uint8_t *readBuffer_ = nullptr;
62     uint8_t *fileData_ = nullptr;
63     int mmapFd_ = -1;
64     bool mmapFdPassedOn_ = false;
65     bool useMmap_ = true;
66 };
67 } // namespace Media
68 } // namespace OHOS
69 
70 #endif // FRAMEWORKS_INNERKITSIMPL_STREAM_INCLUDE_FILE_SOURCE_STREAM_H
71